Skip to content

Commit

Permalink
Merge pull request wiremock#2706 from wiremock/fix-handlebars-math-he…
Browse files Browse the repository at this point in the history
…lper

fix: handlebars math helper handles large negative integers
  • Loading branch information
dieppa authored May 7, 2024
2 parents 5a51ac9 + 237d711 commit e8c78fa
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2021-2022 Thomas Akehurst
* Copyright (C) 2021-2024 Thomas Akehurst
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -88,11 +88,12 @@ private static Object reduceToPrimitiveNumber(BigDecimal value) {
}

if (value.scale() == 0) {
if (value.longValue() <= Integer.MAX_VALUE) {
long longValue = value.longValue();
if (longValue <= Integer.MAX_VALUE && longValue >= Integer.MIN_VALUE) {
return value.intValue();
}

return value.longValue();
return longValue;
}

return value.doubleValue();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2021-2022 Thomas Akehurst
* Copyright (C) 2021-2024 Thomas Akehurst
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -110,4 +110,9 @@ void coercesEpochFormattedRenderableDateParameterCorrectly() throws Exception {
renderHelperValue(helper, new RenderableDate(date, "epoch", null), "+", 0),
is(1663258226792L));
}

@Test
void handlesLargeNegativeIntegerResults() throws Exception {
assertThat(renderHelperValue(helper, Long.MIN_VALUE, "+", 1), is(Long.MIN_VALUE + 1));
}
}

0 comments on commit e8c78fa

Please sign in to comment.