Repository navigation
fix(sheet): fall back to the default format for empty date patterns - #1271
Sean-Walker0 wants to merge 2 commits into
Conversation
skytin1004
left a comment
There was a problem hiding this comment.
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!
| // An empty pattern carries no information: fall back to the default format instead of | ||
| // storing an empty custom format that renders as General. |
There was a problem hiding this comment.
The code here is quite self-explanatory. We might not need this comment, or we could consider removing it to keep the code clean.
| try (Workbook workbook = WorkbookFactory.create(new FileInputStream(file))) { | ||
| return workbook.getSheetAt(0).getRow(1).getCell(0); | ||
| } |
There was a problem hiding this comment.
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 useWorkbookFactory#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 aCellobject tied to an already-closedWorkbook, 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); |
There was a problem hiding this comment.
Consider importing org.apache.fesod.sheet.testkit.enums.ExcelFormat to avoid using the FQCN here.
|
All three points addressed and force-pushed:
|
|
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
0503c49 to
9d40a7f
Compare
Purpose of the pull request
WorkBookUtil.fillDataFormattreated an empty pattern like a real one: a model field annotated with@DateTimeFormat("")had an empty custom format stored on the cell, which renders asGeneral— so aLocalDateTimefield showed the raw serial number43832.12783564815instead of a formatted date.The
OffsetDateTimeconverter already normalizes empty patterns (added with #1033), which made the identical annotation behave differently depending on the field type; theLocalDateTime/LocalDate/LocalTime/Date/SqlTime/Timestampconverters 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?
fillDataFormatnow 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
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 explicityyyy/MM/ddpattern 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
@DateTimeFormat("")(and any other empty-pattern source) now falls back to the default date format instead of storing an empty custom format that renders as General. Explicit patterns are untouched.WorkBookUtilor the date converters; my open fix(sheet): escape hex sequences in head and multi-variable fill cells #1226/fix(sheet): escape hex sequences in in-memory XLSX writes too #1223 (escape handling) and fix(sheet): tolerate a null rounding mode in the decimal format cache #1270 (rounding-mode NPE) touch unrelated regions.