From 1659d2b1747ce352a7a4a75875a7e7ce60905921 Mon Sep 17 00:00:00 2001 From: Sean-Walker0 <309789691+Sean-Walker0@users.noreply.github.com> Date: Wed, 28 Oct 2026 11:04:48 +0800 Subject: [PATCH 1/2] fix(sheet): fall back to the default format for empty date patterns 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 #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. --- .../apache/fesod/sheet/util/WorkBookUtil.java | 4 +- .../EmptyDateTimeFormatWriteTest.java | 135 ++++++++++++++++++ 2 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 fesod-sheet/src/test/java/org/apache/fesod/sheet/converter/EmptyDateTimeFormatWriteTest.java diff --git a/fesod-sheet/src/main/java/org/apache/fesod/sheet/util/WorkBookUtil.java b/fesod-sheet/src/main/java/org/apache/fesod/sheet/util/WorkBookUtil.java index 2443842052..47933c5c11 100644 --- a/fesod-sheet/src/main/java/org/apache/fesod/sheet/util/WorkBookUtil.java +++ b/fesod-sheet/src/main/java/org/apache/fesod/sheet/util/WorkBookUtil.java @@ -156,7 +156,9 @@ public static void fillDataFormat(WriteCellData cellData, String format, Stri cellData.getWriteCellStyle().setDataFormatData(new DataFormatData()); } if (cellData.getWriteCellStyle().getDataFormatData().getFormat() == null) { - if (format == null) { + // An empty pattern carries no information: fall back to the default format instead of + // storing an empty custom format that renders as General. + if (format == null || format.isEmpty()) { cellData.getWriteCellStyle().getDataFormatData().setFormat(defaultFormat); } else { cellData.getWriteCellStyle().getDataFormatData().setFormat(format); diff --git a/fesod-sheet/src/test/java/org/apache/fesod/sheet/converter/EmptyDateTimeFormatWriteTest.java b/fesod-sheet/src/test/java/org/apache/fesod/sheet/converter/EmptyDateTimeFormatWriteTest.java new file mode 100644 index 0000000000..84ba7c1354 --- /dev/null +++ b/fesod-sheet/src/test/java/org/apache/fesod/sheet/converter/EmptyDateTimeFormatWriteTest.java @@ -0,0 +1,135 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.fesod.sheet.converter; + +import java.io.File; +import java.io.FileInputStream; +import java.time.LocalDateTime; +import java.time.OffsetDateTime; +import java.time.ZoneOffset; +import java.util.Collections; +import org.apache.fesod.sheet.FesodSheet; +import org.apache.fesod.sheet.annotation.format.DateTimeFormat; +import org.apache.fesod.sheet.testkit.Tags; +import org.apache.fesod.sheet.testkit.base.AbstractExcelTest; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.ss.usermodel.WorkbookFactory; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; + +/** + * An empty {@code @DateTimeFormat} value means "no explicit pattern": date fields must fall back to the default date + * format instead of writing an empty custom format that renders the raw serial number. + */ +@Tag(Tags.WRITE) +public class EmptyDateTimeFormatWriteTest extends AbstractExcelTest { + + private static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss"; + + public static class LocalDateTimeModel { + @DateTimeFormat("") + private LocalDateTime when; + + public LocalDateTime getWhen() { + return when; + } + + public void setWhen(LocalDateTime when) { + this.when = when; + } + } + + public static class OffsetDateTimeModel { + @DateTimeFormat("") + private OffsetDateTime when; + + public OffsetDateTime getWhen() { + return when; + } + + public void setWhen(OffsetDateTime when) { + this.when = when; + } + } + + @Test + public void localDateTimeWithEmptyFormatFallsBackToDefaultDateFormat() throws Exception { + File file = createTempFile("emptyFmtLocalDateTime", org.apache.fesod.sheet.testkit.enums.ExcelFormat.XLSX); + LocalDateTimeModel model = new LocalDateTimeModel(); + model.setWhen(LocalDateTime.of(2020, 1, 2, 3, 4, 5)); + + FesodSheet.write(file, LocalDateTimeModel.class).sheet().doWrite(Collections.singletonList(model)); + + Cell cell = readCell(file); + Assertions.assertEquals( + DEFAULT_DATE_TIME_FORMAT, + cell.getCellStyle().getDataFormatString(), + "an empty pattern must fall back to the default date format, not an empty one"); + } + + @Test + public void offsetDateTimeWithEmptyFormatFallsBackToDefaultDateFormat() throws Exception { + File file = createTempFile("emptyFmtOffsetDateTime", org.apache.fesod.sheet.testkit.enums.ExcelFormat.XLSX); + OffsetDateTimeModel model = new OffsetDateTimeModel(); + model.setWhen(OffsetDateTime.of(2020, 1, 2, 3, 4, 5, 0, ZoneOffset.UTC)); + + FesodSheet.write(file, OffsetDateTimeModel.class).sheet().doWrite(Collections.singletonList(model)); + + Cell cell = readCell(file); + Assertions.assertEquals( + DEFAULT_DATE_TIME_FORMAT, + cell.getCellStyle().getDataFormatString(), + "the offset converter already normalizes empty patterns"); + } + + @Test + public void explicitFormatIsStillApplied() throws Exception { + File file = createTempFile("explicitFmt", org.apache.fesod.sheet.testkit.enums.ExcelFormat.XLSX); + ExplicitFormatModel model = new ExplicitFormatModel(); + model.setWhen(LocalDateTime.of(2020, 1, 2, 3, 4, 5)); + + FesodSheet.write(file, ExplicitFormatModel.class).sheet().doWrite(Collections.singletonList(model)); + + Cell cell = readCell(file); + Assertions.assertEquals( + "yyyy/MM/dd", cell.getCellStyle().getDataFormatString(), "an explicit pattern is applied as-is"); + } + + public static class ExplicitFormatModel { + @DateTimeFormat("yyyy/MM/dd") + private LocalDateTime when; + + public LocalDateTime getWhen() { + return when; + } + + public void setWhen(LocalDateTime when) { + this.when = when; + } + } + + private Cell readCell(File file) throws Exception { + try (Workbook workbook = WorkbookFactory.create(new FileInputStream(file))) { + return workbook.getSheetAt(0).getRow(1).getCell(0); + } + } +} From 9d40a7f330baa2b51368507e1d7fc5fd2f911276 Mon Sep 17 00:00:00 2001 From: Sean-Walker0 <309789691+Sean-Walker0@users.noreply.github.com> Date: Thu, 29 Oct 2026 20:39:27 +0800 Subject: [PATCH 2/2] fix(sheet): apply review cleanup for empty date pattern fallback - 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 --- .../apache/fesod/sheet/util/WorkBookUtil.java | 2 -- .../EmptyDateTimeFormatWriteTest.java | 25 ++++++++----------- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/fesod-sheet/src/main/java/org/apache/fesod/sheet/util/WorkBookUtil.java b/fesod-sheet/src/main/java/org/apache/fesod/sheet/util/WorkBookUtil.java index 47933c5c11..5a680a996c 100644 --- a/fesod-sheet/src/main/java/org/apache/fesod/sheet/util/WorkBookUtil.java +++ b/fesod-sheet/src/main/java/org/apache/fesod/sheet/util/WorkBookUtil.java @@ -156,8 +156,6 @@ public static void fillDataFormat(WriteCellData cellData, String format, Stri cellData.getWriteCellStyle().setDataFormatData(new DataFormatData()); } if (cellData.getWriteCellStyle().getDataFormatData().getFormat() == null) { - // An empty pattern carries no information: fall back to the default format instead of - // storing an empty custom format that renders as General. if (format == null || format.isEmpty()) { cellData.getWriteCellStyle().getDataFormatData().setFormat(defaultFormat); } else { diff --git a/fesod-sheet/src/test/java/org/apache/fesod/sheet/converter/EmptyDateTimeFormatWriteTest.java b/fesod-sheet/src/test/java/org/apache/fesod/sheet/converter/EmptyDateTimeFormatWriteTest.java index 84ba7c1354..dc7c6378eb 100644 --- a/fesod-sheet/src/test/java/org/apache/fesod/sheet/converter/EmptyDateTimeFormatWriteTest.java +++ b/fesod-sheet/src/test/java/org/apache/fesod/sheet/converter/EmptyDateTimeFormatWriteTest.java @@ -20,7 +20,6 @@ package org.apache.fesod.sheet.converter; import java.io.File; -import java.io.FileInputStream; import java.time.LocalDateTime; import java.time.OffsetDateTime; import java.time.ZoneOffset; @@ -29,6 +28,7 @@ import org.apache.fesod.sheet.annotation.format.DateTimeFormat; import org.apache.fesod.sheet.testkit.Tags; import org.apache.fesod.sheet.testkit.base.AbstractExcelTest; +import org.apache.fesod.sheet.testkit.enums.ExcelFormat; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; @@ -73,45 +73,41 @@ public void setWhen(OffsetDateTime when) { @Test public void localDateTimeWithEmptyFormatFallsBackToDefaultDateFormat() throws Exception { - File file = createTempFile("emptyFmtLocalDateTime", org.apache.fesod.sheet.testkit.enums.ExcelFormat.XLSX); + File file = createTempFile("emptyFmtLocalDateTime", ExcelFormat.XLSX); LocalDateTimeModel model = new LocalDateTimeModel(); model.setWhen(LocalDateTime.of(2020, 1, 2, 3, 4, 5)); FesodSheet.write(file, LocalDateTimeModel.class).sheet().doWrite(Collections.singletonList(model)); - Cell cell = readCell(file); Assertions.assertEquals( DEFAULT_DATE_TIME_FORMAT, - cell.getCellStyle().getDataFormatString(), + readCellDataFormat(file), "an empty pattern must fall back to the default date format, not an empty one"); } @Test public void offsetDateTimeWithEmptyFormatFallsBackToDefaultDateFormat() throws Exception { - File file = createTempFile("emptyFmtOffsetDateTime", org.apache.fesod.sheet.testkit.enums.ExcelFormat.XLSX); + File file = createTempFile("emptyFmtOffsetDateTime", ExcelFormat.XLSX); OffsetDateTimeModel model = new OffsetDateTimeModel(); model.setWhen(OffsetDateTime.of(2020, 1, 2, 3, 4, 5, 0, ZoneOffset.UTC)); FesodSheet.write(file, OffsetDateTimeModel.class).sheet().doWrite(Collections.singletonList(model)); - Cell cell = readCell(file); Assertions.assertEquals( DEFAULT_DATE_TIME_FORMAT, - cell.getCellStyle().getDataFormatString(), + readCellDataFormat(file), "the offset converter already normalizes empty patterns"); } @Test public void explicitFormatIsStillApplied() throws Exception { - File file = createTempFile("explicitFmt", org.apache.fesod.sheet.testkit.enums.ExcelFormat.XLSX); + File file = createTempFile("explicitFmt", ExcelFormat.XLSX); ExplicitFormatModel model = new ExplicitFormatModel(); model.setWhen(LocalDateTime.of(2020, 1, 2, 3, 4, 5)); FesodSheet.write(file, ExplicitFormatModel.class).sheet().doWrite(Collections.singletonList(model)); - Cell cell = readCell(file); - Assertions.assertEquals( - "yyyy/MM/dd", cell.getCellStyle().getDataFormatString(), "an explicit pattern is applied as-is"); + Assertions.assertEquals("yyyy/MM/dd", readCellDataFormat(file), "an explicit pattern is applied as-is"); } public static class ExplicitFormatModel { @@ -127,9 +123,10 @@ public void setWhen(LocalDateTime when) { } } - private Cell readCell(File file) throws Exception { - try (Workbook workbook = WorkbookFactory.create(new FileInputStream(file))) { - return workbook.getSheetAt(0).getRow(1).getCell(0); + private String readCellDataFormat(File file) throws Exception { + try (Workbook workbook = WorkbookFactory.create(file)) { + Cell cell = workbook.getSheetAt(0).getRow(1).getCell(0); + return cell.getCellStyle().getDataFormatString(); } } }