Skip to content

fix(sheet): tolerate a null rounding mode in the decimal format cache - #1270

Open
Sean-Walker0 wants to merge 2 commits into
apache:mainfrom
Sean-Walker0:fix/number-format-null-rounding
Open

Sean-Walker0 wants to merge 2 commits into
apache:mainfrom
Sean-Walker0:fix/number-format-null-rounding

Conversation

@Sean-Walker0

Copy link
Copy Markdown
Contributor

Purpose of the pull request

NumberUtils.getCacheDecimalFormat called DecimalFormat.setRoundingMode(roundingMode) whenever the cached format's mode differed from the requested one. NumberFormatProperty is a public class whose constructor accepts a null RoundingMode, and the DecimalFormat default is HALF_EVEN — so a property built without an explicit mode always differed, setRoundingMode(null) threw NullPointerException, and both NumberUtils.format(...) and NumberUtils.parse(...) failed:

ExcelContentProperty property = new ExcelContentProperty();
property.setNumberFormatProperty(new NumberFormatProperty("0.00", null));
NumberUtils.format(new BigDecimal("1.005"), property); // NullPointerException

The annotation path was unaffected because @NumberFormat.roundingMode() defaults to HALF_UP; programmatic NumberFormatProperty use (public constructors, public utility methods) hit the NPE.

No issue exists for this yet; found while probing the number conversion utilities.

What's changed?

getCacheDecimalFormat skips the assignment when the requested mode is null, keeping the DecimalFormat default — the same shape as the existing empty-format fallbacks the annotation path already handles.

Checklist

  • I have read the Contributor Guide.
  • I have written the necessary doc or comment.
  • I have added the necessary unit tests and all cases have passed.

Verifying this change

  • NumberUtilsTest.test_format_nullRoundingModeDoesNotThrow — red before (NullPointerException), green after, and pins the HALF_EVEN rendering (1.005 with 0.00 -> 1.00).
  • The other 34 NumberUtilsTest cases (explicit modes, cache locale tracking, range rejection) pass unchanged.
  • ./mvnw clean package -B -Dmaven.test.skip=false -pl fesod-common,fesod-shaded,fesod-sheet: 1777 tests, 0 failures, 0 errors.
  • ./mvnw -pl fesod-sheet spotless:check: clean.

Notes

@skytin1004 skytin1004 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Sean-Walker0, thanks for working on this fix. I think there may still be one issue.

Because the same formatter is reused, a previous call with RoundingMode.UP can affect a later call with a null rounding mode. For example, 1.001 is formatted as 1.01 instead of the expected default result, 1.00.

Could you reset the rounding mode to the default when it is null? Please also add a test that first uses RoundingMode.UP and then uses a null rounding mode. Thank you!

@Sean-Walker0
Sean-Walker0 force-pushed the fix/number-format-null-rounding branch from 0683ed6 to b1ad771 Compare October 29, 2026 22:45
@Sean-Walker0

Copy link
Copy Markdown
Contributor Author

Good catch — the cached format is shared per pattern, so a mode set by an earlier call did leak into later null-mode calls. Fixed and pushed as b1ad771: a null rounding mode now restores RoundingMode.HALF_EVEN (the documented DecimalFormat default) before the format is returned.

The requested regression test is in as NumberUtilsTest.test_format_nullRoundingModeResetsPreviousModeFromCache — UP first, then null; it reproduced 1.01 on the previous fix and expects 1.00 now. The branch was also rebased onto the current main while at it. Full fesod-sheet suite: 1791 tests green, spotless clean.

@Mikkey-f Mikkey-f left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Sean-Walker0 — verified the cache-leak fix on Windows/JDK 17.

Verification

  • PR head: NumberUtilsTest 36/36 green.
  • At 292c583 (first commit: null check but no reset): test_format_nullRoundingModeResetsPreviousModeFromCache fails with expected: <1.00> but was: <1.01> — the second commit is necessary.
  • At current main (no change): both new tests fail with NullPointerException in getCacheDecimalFormat.

The reset to RoundingMode.HALF_EVEN matches DecimalFormat's documented default, and the comparison avoids setting the mode when it is already correct. LGTM from my side.

@Sean-Walker0
Sean-Walker0 force-pushed the fix/number-format-null-rounding branch from b1ad771 to 42e1189 Compare November 8, 2026 06:25
@Sean-Walker0

Copy link
Copy Markdown
Contributor Author

Thanks @Mikkey-f for the independent verification on Windows — glad the regression test pins exactly the leak skytin1004 described.

@Sean-Walker0
Sean-Walker0 force-pushed the fix/number-format-null-rounding branch 3 times, most recently from 76536da to 6223cca Compare November 11, 2026 11:24
NumberUtils.getCacheDecimalFormat compared the cached format's
rounding mode against the requested one and called setRoundingMode
whenever they differed. A NumberFormatProperty built without a
rounding mode therefore always differed (the DecimalFormat default is
HALF_EVEN), and DecimalFormat.setRoundingMode(null) answered with a
NullPointerException - so NumberUtils.format and NumberUtils.parse
blew up on any content property whose number format carries no
explicit mode, while the annotation default (HALF_UP) worked.

Skip the assignment when the requested mode is null, keeping the
DecimalFormat default, exactly like the empty-format fallbacks the
annotation path already handles.

Verified with NumberUtilsTest.test_format_nullRoundingModeDoesNotThrow
(NPE before the change, green after) plus the full fesod-sheet suite
(1777 tests) and spotless:check.
The decimal format cache shares one DecimalFormat per pattern, so an
explicit mode from an earlier call survived into later calls that
request no mode: formatting 1.001 with RoundingMode.UP and then with
a null mode yielded 1.01 instead of the HALF_EVEN default 1.00.

A null rounding mode now restores RoundingMode.HALF_EVEN (the
documented DecimalFormat default) before the format is returned.

Verified with
NumberUtilsTest.test_format_nullRoundingModeResetsPreviousModeFromCache
(1.01 before the change, 1.00 after) plus the full fesod-sheet suite
(1791 tests) and spotless:check.
@Sean-Walker0
Sean-Walker0 force-pushed the fix/number-format-null-rounding branch from 6223cca to 0f3654a Compare November 23, 2026 07:36

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants