Skip to content

[Bug]POJO Export with custom converter StackOverflow Repro #889

Closed as not planned

Description

@fyeeme

Search before asking

  • I searched in the issues and found nothing similar.

Fesod version

2.0.1-incubating

JDK version

21

Operating system

macos 26.4

Steps To Reproduce

Summary

Using the native POJO write path in org.apache.fesod:fesod-sheet:2.0.1-incubating
can trigger a StackOverflowError when:

  • the DTO contains a ZonedDateTime field
  • a custom converter is registered for ZonedDateTime
  • the DTO also uses normal field metadata such as:
    • @DateTimeFormat
    • @NumberFormat
    • @ExcelProperty(converter = ...)

In our project this blocks switching back to the pure native Fesod POJO export path
for ZonedDateTime-based exports.

Environment

  • Java: project currently runs on Java 21
  • Library: org.apache.fesod:fesod-sheet:2.0.1-incubating
  • Output format: XLSX

Minimal Reproduction

Repository repro artifact:

  • Test file:
package com.linzi.pitpat.excel;

import lombok.Data;
import org.apache.fesod.sheet.FesodSheet;
import org.apache.fesod.sheet.annotation.ExcelProperty;
import org.apache.fesod.sheet.annotation.format.DateTimeFormat;
import org.apache.fesod.sheet.annotation.format.NumberFormat;
import org.apache.fesod.sheet.converters.Converter;
import org.apache.fesod.sheet.enums.CellDataTypeEnum;
import org.apache.fesod.sheet.metadata.GlobalConfiguration;
import org.apache.fesod.sheet.metadata.data.WriteCellData;
import org.apache.fesod.sheet.metadata.property.ExcelContentProperty;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayOutputStream;
import java.math.BigDecimal;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertThrows;

/**
 * Documents an upstream Fesod bug we can reproduce reliably.
 *
 * <p>When using the native POJO write path with a DTO containing ZonedDateTime,
 * Fesod 2.0.1-incubating triggers a StackOverflowError inside its write holder hashCode chain.
 * We keep this test disabled because enabling it would break CI, but it provides a stable
 * reproduction if we need to validate an upstream fix or a local dependency patch.</p>
 */
class FesodNativeMetadataRegressionTest {

    @Test
    @Disabled("Stable repro for upstream Fesod StackOverflowError on native POJO write path")
    @DisplayName("native Fesod POJO export should reproduce StackOverflowError with ZonedDateTime metadata")
    void nativePojoExport_shouldReproduceStackOverflowError() {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        
        assertThrows(StackOverflowError.class, () -> FesodSheet.write(outputStream, NativeMixedMetadataRow.class)
                .registerConverter(new NativeZonedDateStringConverter())
                .sheet("NativeMixedMetadata")
                .doWrite(List.of(createRow())));
    }

    private NativeMixedMetadataRow createRow() {
        NativeMixedMetadataRow row = new NativeMixedMetadataRow();
        row.setStatus("ACTIVE");
        row.setAmount(new BigDecimal("1234.5"));
        row.setCreatedAt(ZonedDateTime.of(2026, 3, 24, 15, 30, 45, 0, ZoneId.of("Asia/Shanghai")));
        return row;
    }

    @Data
    public static class NativeMixedMetadataRow {
        @ExcelProperty(value = "Status", converter = NativeStatusLabelConverter.class)
        private String status;

        @ExcelProperty("Amount")
        @NumberFormat("¥#,##0.00")
        private BigDecimal amount;

        @ExcelProperty("Created At")
        @DateTimeFormat("yyyy-MM-dd HH:mm:ss")
        private ZonedDateTime createdAt;
    }

    public static class NativeStatusLabelConverter implements Converter<String> {
        @Override
        public Class<?> supportJavaTypeKey() {
            return String.class;
        }

        @Override
        public CellDataTypeEnum supportExcelTypeKey() {
            return CellDataTypeEnum.STRING;
        }

        @Override
        public WriteCellData<?> convertToExcelData(
                String value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
            return new WriteCellData<>("STATUS:" + value);
        }
    }

    public static class NativeZonedDateStringConverter implements Converter<ZonedDateTime> {
        @Override
        public Class<?> supportJavaTypeKey() {
            return ZonedDateTime.class;
        }

        @Override
        public CellDataTypeEnum supportExcelTypeKey() {
            return CellDataTypeEnum.STRING;
        }

        @Override
        public WriteCellData<?> convertToExcelData(
                ZonedDateTime value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
            String format = contentProperty != null && contentProperty.getDateTimeFormatProperty() != null
                    ? contentProperty.getDateTimeFormatProperty().getFormat()
                    : "yyyy-MM-dd HH:mm:ss";
            return new WriteCellData<>(value.toLocalDateTime().format(java.time.format.DateTimeFormatter.ofPattern(format)));
        }
    }
}

The test is intentionally marked @Disabled so CI stays green, but it is a stable reproducer.

Repro DTO

@Data
public static class NativeMixedMetadataRow {
    @ExcelProperty(value = "Status", converter = NativeStatusLabelConverter.class)
    private String status;

    @ExcelProperty("Amount")
    @NumberFormat("¥#,##0.00")
    private BigDecimal amount;

    @ExcelProperty("Created At")
    @DateTimeFormat("yyyy-MM-dd HH:mm:ss")
    private ZonedDateTime createdAt;
}

Repro Export Code

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

FesodSheet.write(outputStream, NativeMixedMetadataRow.class)
        .registerConverter(new NativeZonedDateStringConverter())
        .sheet("NativeMixedMetadata")
        .doWrite(List.of(row));

Expected Result

Workbook is exported successfully, with native Fesod metadata handling preserved:

  • @ExcelProperty(converter = ...) is applied
  • @NumberFormat is applied
  • @DateTimeFormat is applied

Actual Result

StackOverflowError

Observed Stack Pattern

The stack repeatedly cycles through hashCode() calls on Fesod write holder objects:

java.lang.StackOverflowError
  at java.util.ArrayList.hashCode(...)
  at org.apache.fesod.sheet.metadata.Head.hashCode(...)
  at org.apache.fesod.sheet.metadata.property.ExcelHeadProperty.hashCode(...)
  at org.apache.fesod.sheet.write.property.ExcelWriteHeadProperty.hashCode(...)
  at org.apache.fesod.sheet.write.metadata.holder.AbstractWriteHolder.hashCode(...)
  at org.apache.fesod.sheet.write.metadata.holder.WriteWorkbookHolder.hashCode(...)
  at org.apache.fesod.sheet.write.metadata.holder.WriteSheetHolder.hashCode(...)
  ...

Suspected Root Cause

This is an inference based on source inspection plus stable reproduction.

WriteContextImpl.initSheet() stores WriteSheetHolder into maps owned by WriteWorkbookHolder:

writeWorkbookHolder.getHasBeenInitializedSheetIndexMap().put(writeSheetHolder.getSheetNo(), writeSheetHolder);
writeWorkbookHolder.getHasBeenInitializedSheetNameMap().put(writeSheetHolder.getSheetName(), writeSheetHolder);

At the same time:

  • WriteSheetHolder holds parentWriteWorkbookHolder
  • WriteWorkbookHolder holds maps of WriteSheetHolder
  • AbstractWriteHolder, WriteWorkbookHolder, and WriteSheetHolder use Lombok @EqualsAndHashCode

That appears to create a recursive object graph for structural hashCode() evaluation:

WriteWorkbookHolder
  -> hasBeenInitializedSheetIndexMap / hasBeenInitializedSheetNameMap
  -> WriteSheetHolder
  -> parentWriteWorkbookHolder
  -> WriteWorkbookHolder
  -> ...

If Fesod evaluates structural hashCode() on these holder objects, recursion does not terminate.

Project Impact

In our project:

  • the plain native POJO path would be the preferred implementation
  • but this Fesod issue prevents using it safely for ZonedDateTime
  • we currently use a compatibility path that still reuses Fesod field metadata instead of fully hardcoding formatting logic

Suggested Fix Direction

Likely fix directions in Fesod:

  • exclude recursive holder references from equals/hashCode
  • or avoid structural equals/hashCode for runtime write holders entirely
  • or replace the relevant holder equality semantics with identity-based handling

Local Verification

Project-level verification after documenting the repro:

mvn -q -pl pitpat-common/pitpat-excel test
mvn -q -pl demo-service -am -DskipTests compile

Current Behavior

StackOverflowError

Expected Behavior

work normally

Anything else?

No response

Are you willing to submit a PR?

  • I'm willing to submit a PR!

Activity

  1. added
    bug
    on Mar 25, 2026
  2. bengbengbalabalabeng commented on Mar 25, 2026

    @bengbengbalabalabeng
    Contributor

    Hi, @fyeeme

    I reproduced your minimal example locally (outside of a unit‑test environment) and confirmed that the StackOverflowError may not the actual root cause. The real issue is that Fesod fails to correctly resolve a matching Converter for the ZonedDateTime field.

    I validated the behavior using the following test code (essentially identical to your original repro):

    @Slf4j
    class FesodNativeMetadataRegression {
    
        public static void main(String[] args) throws Throwable {
            String sheetName = "NativeMixedMetadata";
            ByteArrayOutputStream output = new ByteArrayOutputStream();
    
            FesodSheet.write(output, NativeMixedMetadataRow.class)
                    .registerConverter(new NativeZonedDateStringConverter())
                    .sheet(sheetName)
                    .doWrite(List.of(createRow()));
    
            ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());
            Workbook workbook = WorkbookFactory.create(input);
            Sheet sheet = workbook.getSheet(sheetName);
            Row dataRow = sheet.getRow(1);
    
            Cell statusCell = dataRow.getCell(0);
            Validate.isTrue(Objects.equals("STATUS:ACTIVE", statusCell.getStringCellValue()), "status does not match");
    
            Cell amountCell = dataRow.getCell(1);
            Validate.isTrue(Objects.equals(1234.5, amountCell.getNumericCellValue()), "amount does not match");
    
            Cell createdAtCell = dataRow.getCell(2);
            Validate.isTrue(Objects.equals("2026-03-24 15:30:45", createdAtCell.getStringCellValue()), "createdAt does not match");
    
            log.info("done...");
        }
    }

    Error log:

    Exception in thread "main" org.apache.fesod.sheet.exception.ExcelWriteDataConvertException:
     Can not find 'Converter' support class ZonedDateTime.
    

    For the reason of this error, please refer to issue #648.


    Solution (temporary)

    Please try to modify the custom Converter, override the supportExcelTypeKey method and return null.

    public static class NativeZonedDateStringConverter2 implements Converter<ZonedDateTime> {
        @Override
        public Class<?> supportJavaTypeKey() {
            return ZonedDateTime.class;
        }
    
        @Override
        public CellDataTypeEnum supportExcelTypeKey() {
            return null;
        }
    
        @Override
        public WriteCellData<?> convertToExcelData(
                ZonedDateTime value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
            String format = contentProperty != null && contentProperty.getDateTimeFormatProperty() != null
                    ? contentProperty.getDateTimeFormatProperty().getFormat()
                    : "yyyy-MM-dd HH:mm:ss";
            return new WriteCellData<>(value.toLocalDateTime().format(java.time.format.DateTimeFormatter.ofPattern(format)));
        }
    }

    With this change applied, both the main method and the unit test pass successfully.

    Unit Test

    @Test
    void nativePojoExport_shouldSuccess() throws Throwable {
        String sheetName = "NativeMixedMetadata";
    
        ByteArrayOutputStream output = new ByteArrayOutputStream();
    
        assertDoesNotThrow(() -> {
            FesodSheet.write(output, NativeMixedMetadataRow.class)
                    .registerConverter(new NativeZonedDateStringConverter2())
                    .sheet(sheetName)
                    .doWrite(List.of(createRow()));
        });
    
        ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());
        Workbook workbook = WorkbookFactory.create(input);
        Sheet sheet = workbook.getSheet(sheetName);
        Row dataRow = sheet.getRow(1);
    
        Cell statusCell = dataRow.getCell(0);
        assertEquals("STATUS:ACTIVE", statusCell.getStringCellValue());
    
        Cell amountCell = dataRow.getCell(1);
        assertEquals(1234.5, amountCell.getNumericCellValue());
    
        Cell createdAtCell = dataRow.getCell(2);
        assertEquals("2026-03-24 15:30:45", createdAtCell.getStringCellValue());
    }
  3. fyeeme commented on Mar 25, 2026

    @fyeeme
    Author

    ths for your response, I'll have a try and confirm that.

  4. skytin1004 commented on Apr 17, 2026

    @skytin1004
    Contributor

    Hi, I also tried to reproduce this locally on current main.

    My results are consistent with what @bengbengbalabalabeng mentioned.

    Using a similar setup with:

    • @ExcelProperty(converter = ...) on a String field
    • @NumberFormat on a BigDecimal field
    • @DateTimeFormat on a ZonedDateTime field
    • and a globally registered Converter<ZonedDateTime>

    I saw the following behavior:

    • when supportExcelTypeKey() returns CellDataTypeEnum.STRING, it fails with ExcelWriteDataConvertException rather than StackOverflowError
    • the error is: Can not find 'Converter' support class ZonedDateTime
    • when supportExcelTypeKey() returns null, the export works fine

    I also checked the output values and they look correct:

    • STATUS:ACTIVE
    • 1234.5
    • 2026-03-24 15:30:45

    So it seems more like a converter resolution issue than a recursive hashCode() / StackOverflowError problem.

  5. added a commit that references this issue on Apr 17, 2026
    e087d65
  6. added
    waiting for feedback
    on Aug 16, 2026
  7. github-actions commented on Sep 16, 2026

    @github-actions

    This issue has been automatically marked as stale because it has not had recent activity for 30 days. It will be closed in next 7 days if no further activity occurs.

  8. github-actions commented on Sep 24, 2026

    @github-actions

    This issue has been closed because it has not received response for too long time. You could reopen it if you encountered similar problems in the future.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugstalewaiting for feedback

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions