-
Notifications
You must be signed in to change notification settings - Fork 597
fix: ClassFormatError when generating writers for array-typed fields (#4009) #4014
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+163
−4
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
core/src/test/java/com/alibaba/fastjson2/issues_4000/Issue4009Test.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| package com.alibaba.fastjson2.issues_4000; | ||
|
|
||
| import com.alibaba.fastjson2.JSON; | ||
| import com.alibaba.fastjson2.JSONReader; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertArrayEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| public class Issue4009Test { | ||
| @Test | ||
| public void testClassArrayField() { | ||
| // Test that a class with a Class[] field can be serialized without ClassFormatError | ||
| BeanWithClassArray bean = new BeanWithClassArray(); | ||
| bean.setClasses(new Class[]{String.class, Integer.class}); | ||
| bean.setName("test"); | ||
|
|
||
| // This should not throw ClassFormatError | ||
| Object json = JSON.toJSON(bean); | ||
| assertNotNull(json); | ||
|
|
||
| String jsonString = JSON.toJSONString(bean); | ||
| assertNotNull(jsonString); | ||
| assertTrue(jsonString.contains("test")); | ||
|
|
||
| // the reader generates class names the same way, so deserialization must work too | ||
| // (SupportClassForName is required only because the field type is Class[]) | ||
| BeanWithClassArray parsed = JSON.parseObject( | ||
| jsonString, BeanWithClassArray.class, JSONReader.Feature.SupportClassForName); | ||
| assertNotNull(parsed); | ||
| assertEquals("test", parsed.getName()); | ||
| assertArrayEquals(new Class[]{String.class, Integer.class}, parsed.getClasses()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testClassArrayFieldNull() { | ||
| BeanWithClassArray bean = new BeanWithClassArray(); | ||
| bean.setClasses(null); | ||
| bean.setName("test"); | ||
|
|
||
| Object json = JSON.toJSON(bean); | ||
| assertNotNull(json); | ||
| } | ||
|
|
||
| @Test | ||
| public void test2DArrayField() { | ||
| // Test multi-dimensional arrays | ||
| BeanWith2DArray bean = new BeanWith2DArray(); | ||
| bean.setMatrix(new int[][]{{1, 2}, {3, 4}}); | ||
|
|
||
| Object json = JSON.toJSON(bean); | ||
| assertNotNull(json); | ||
|
|
||
| String jsonString = JSON.toJSONString(bean); | ||
| assertNotNull(jsonString); | ||
|
|
||
| BeanWith2DArray parsed = JSON.parseObject(jsonString, BeanWith2DArray.class); | ||
| assertNotNull(parsed); | ||
| assertArrayEquals(new int[][]{{1, 2}, {3, 4}}, parsed.getMatrix()); | ||
| } | ||
|
|
||
| public static class BeanWithClassArray { | ||
| private String name; | ||
| private Class<?>[] classes; | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public void setName(String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| public Class<?>[] getClasses() { | ||
| return classes; | ||
| } | ||
|
|
||
| public void setClasses(Class<?>[] classes) { | ||
| this.classes = classes; | ||
| } | ||
| } | ||
|
|
||
| public static class BeanWith2DArray { | ||
| private int[][] matrix; | ||
|
|
||
| public int[][] getMatrix() { | ||
| return matrix; | ||
| } | ||
|
|
||
| public void setMatrix(int[][] matrix) { | ||
| this.matrix = matrix; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.