Skip to content

Commit

Permalink
fix read only field deserialize error, for issue #2532
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed May 9, 2024
1 parent 30190df commit f866b92
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,9 @@ public T readObject(JSONReader jsonReader, Type fieldType, Object fieldName, lon

Object fieldValue = valueMap.get(fieldReader.fieldNameHash);
if (fieldValue != null) {
if (paramReader != null) {
continue;
}
fieldReader.accept(object, fieldValue);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.alibaba.fastjson2.issues_2500;

import com.alibaba.fastjson2.JSON;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;

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

public class Issue2532 {
String jsonString = "{\"collectionField\": [1, 2, 3]}";

class ImmutableCollectionExample {
private final Collection<Integer> collectionField;

public ImmutableCollectionExample(Collection<Integer> collectionField) {
this.collectionField = Collections.unmodifiableCollection(new ArrayList<>(collectionField));
}

public Collection<Integer> getCollectionField() {
return collectionField;
}
}

@Test
public void testParseObjectWithImmutableCollectionField() throws Exception {
ImmutableCollectionExample result = JSON.parseObject(jsonString, ImmutableCollectionExample.class);
assertEquals(Arrays.asList(1, 2, 3), new ArrayList<>(result.getCollectionField()));
}
}

0 comments on commit f866b92

Please sign in to comment.