fix: ensure converter isolation across ExcelWriter and ExcelReader#740
fix: ensure converter isolation across ExcelWriter and ExcelReader#740YangSiJun528 wants to merge 6 commits intoapache:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This pull request fixes a converter isolation bug where custom converters registered in one ExcelWriter or ExcelReader instance would leak to other instances due to shared mutable static maps.
Key Changes:
- Wrapped default converter maps with
Collections.unmodifiableMap()inDefaultConverterLoaderto prevent direct modification - Modified holder initialization to create defensive mutable copies of converter maps for each instance
- Added test case to verify converter isolation between
ExcelWriterinstances
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
fesod/src/main/java/org/apache/fesod/sheet/converters/DefaultConverterLoader.java |
Returns unmodifiable maps from loadDefaultWriteConverter() and loadAllConverter() methods to protect static converter maps |
fesod/src/main/java/org/apache/fesod/sheet/write/metadata/holder/AbstractWriteHolder.java |
Creates mutable HashMap copy from unmodifiable default converter map during holder initialization |
fesod/src/main/java/org/apache/fesod/sheet/read/metadata/holder/AbstractReadHolder.java |
Creates mutable HashMap copy from unmodifiable default converter map during holder initialization |
fesod/src/test/java/org/apache/fesod/sheet/converter/ConverterIsolationTest.java |
Adds new test to validate custom converters do not leak between ExcelWriter instances |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| @Test | ||
| public void testConverterIsolation() { | ||
| ExcelWriter writer1 = FesodSheet.write(new File(TestFileUtil.getPath() + "writer1.xlsx"), TestData.class) | ||
| .registerConverter(new ConverterA()) | ||
| .build(); | ||
|
|
||
| ExcelWriter writer2 = FesodSheet.write(new File(TestFileUtil.getPath() + "writer2.xlsx"), TestData.class) | ||
| .build(); | ||
|
|
||
| boolean writer2HasConverterA = writer2.writeContext().currentWriteHolder().converterMap().values().stream() | ||
| .anyMatch(c -> c instanceof ConverterA); | ||
|
|
||
| writer1.finish(); | ||
| writer2.finish(); | ||
|
|
||
| assertFalse(writer2HasConverterA, "Custom converter should not leak between ExcelWriter instances"); | ||
| } |
There was a problem hiding this comment.
The test only validates converter isolation for ExcelWriter instances, but the PR description and code changes indicate that the fix also applies to ExcelReader instances. Consider adding a corresponding test case for ExcelReader to ensure complete coverage of the bug fix and prevent regression in the read path.
Purpose of the pull request
Closes #729
Fixes converter isolation bug where custom converters registered in one
ExcelWriterorExcelReaderinstance would leak to other instances.What's changed?
DefaultConverterLoader.loadDefaultWriteConverter()andDefaultConverterLoader.loadAllConverter()to return unmodifiable mapAbstractWriteHolderandAbstractReadHolderto create mutable copies from the unmodifiable mapsConverterIsolationTestto verify converters are properly isolated between instancesChecklist