Skip to content

fix(sheet): fall back to the default format for empty date patterns - #1271

Open
Sean-Walker0 wants to merge 2 commits into
apache:mainfrom
Sean-Walker0:fix/empty-date-format-fallback
Open

Sean-Walker0 wants to merge 2 commits into
apache:mainfrom
Sean-Walker0:fix/empty-date-format-fallback

Conversation

@Sean-Walker0

Copy link
Copy Markdown
Contributor

Purpose of the pull request

WorkBookUtil.fillDataFormat treated an empty pattern like a real one: a model field annotated with @DateTimeFormat("") had an empty custom format stored on the cell, which renders as General — so a LocalDateTime field showed the raw serial number 43832.12783564815 instead of a formatted date.

The OffsetDateTime converter already normalizes empty patterns (added with #1033), which made the identical annotation behave differently depending on the field type; the LocalDateTime/LocalDate/LocalTime/Date/SqlTime/Timestamp converters and the write-executor path all pass their format through unchanged and can hit the same hole.

No issue exists for this yet; found while auditing the converter families.

What's changed?

fillDataFormat now treats an empty pattern like an absent one and falls back to the supplied default format. One central change covers every date converter, the write-executor data-format path and future callers; the per-converter normalization #1033 introduced stays (now redundant but harmless).

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

EmptyDateTimeFormatWriteTest:

  • localDateTimeWithEmptyFormatFallsBackToDefaultDateFormat — red before (expected: <yyyy-MM-dd HH:mm:ss> but was: <General>), green after;

  • offsetDateTimeWithEmptyFormatFallsBackToDefaultDateFormat — pins the existing feat: add java.time.OffsetDateTime converters (#1017) #1033 normalization, green before and after;

  • explicitFormatIsStillApplied — an explicit yyyy/MM/dd pattern is applied as-is, green before and after.

  • ./mvnw clean package -B -Dmaven.test.skip=false -pl fesod-common,fesod-shaded,fesod-sheet: 1780 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 the fix. I checked the shared formatting change and its callers. An empty date format now correctly uses the default format, while an explicit format is still preserved.

I also ran the new XLSX integration tests and WorkBookUtilTest. All tests passed. Looks good to me!

Comment on lines +159 to +160
// An empty pattern carries no information: fall back to the default format instead of
// storing an empty custom format that renders as General.

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.

The code here is quite self-explanatory. We might not need this comment, or we could consider removing it to keep the code clean.

Comment on lines +131 to +133
try (Workbook workbook = WorkbookFactory.create(new FileInputStream(file))) {
return workbook.getSheetAt(0).getRow(1).getCell(0);
}

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.

  • new FileInputStream(file) is passed anonymously, so Java's try-with-resources won't automatically close it, and POI does not close caller-supplied streams. It is recommended to use WorkbookFactory#create(File) directly instead, it eliminates resource leak risks and is much more memory-efficient.
  • If the caller only needs cell.getCellStyle().getDataFormatString(), consider returning that String directly from this method. This prevents callers from holding a Cell object tied to an already-closed Workbook, which could lead to unexpected runtime exceptions.


@Test
public void offsetDateTimeWithEmptyFormatFallsBackToDefaultDateFormat() throws Exception {
File file = createTempFile("emptyFmtOffsetDateTime", org.apache.fesod.sheet.testkit.enums.ExcelFormat.XLSX);

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.

Consider importing org.apache.fesod.sheet.testkit.enums.ExcelFormat to avoid using the FQCN here.

@Sean-Walker0

Copy link
Copy Markdown
Contributor Author

All three points addressed and force-pushed:

  1. Removed the in-code comment in fillDataFormat.
  2. The test helper now uses WorkbookFactory.create(File) directly and returns the data-format String, so no Cell outlives the closed workbook and no caller-supplied stream is leaked.
  3. The test imports ExcelFormat instead of using the FQCN.

EmptyDateTimeFormatWriteTest 3/3 green, spotless clean.

@Sean-Walker0

Copy link
Copy Markdown
Contributor Author

Follow-up: the branch was updated via the merge from main in the meantime, so the three review fixes are pushed as a normal commit on top of it: 0503c49 (EmptyDateTimeFormatWriteTest 3/3 green, spotless clean).

fillDataFormat treated an empty pattern like a real one: a model
field annotated with @DateTimeFormat("") had an empty custom format
stored, which renders as General - so a LocalDateTime field showed
the raw serial number 43832.12783564815 instead of a formatted date.
The OffsetDateTime converter already normalized empty patterns after
apache#1033, which made the same annotation behave differently per field
type; NumberUtils-based and executor paths could hit the same hole.

Treat an empty pattern like an absent one in fillDataFormat itself,
so every date converter, the write executor and future callers fall
back to the default format consistently.

Verified with EmptyDateTimeFormatWriteTest (the LocalDateTime case
stored General before the change and falls back after; the
OffsetDateTime case pins the existing normalization; an explicit
pattern is applied as-is on both sides) plus the full fesod-sheet
suite (1780 tests) and spotless:check.
- drop the self-explanatory comment in fillDataFormat
- the test helper uses WorkbookFactory.create(File) and returns the
  data format string directly, so no Cell outlives the closed
  workbook
- import ExcelFormat instead of the fully qualified name in the test
@Sean-Walker0
Sean-Walker0 force-pushed the fix/empty-date-format-fallback branch from 0503c49 to 9d40a7f Compare October 29, 2026 22:52

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