Skip to content

Commit 7640dbb

Browse files
authored
fix(java): fix map nested array type serialization codegen (#2352)
## What does this PR do? <!-- Describe the purpose of this PR. --> ## Related issues Closes #2348 ## Does this PR introduce any user-facing change? <!-- If any user-facing interface changes, please [open an issue](https://github.com/apache/fory/issues/new/choose) describing the need to do so and update the document if necessary. --> - [ ] Does this PR introduce any public API change? - [ ] Does this PR introduce any binary protocol compatibility change? ## Benchmark <!-- When the PR has an impact on performance (if you don't know whether the PR will have an impact on performance, you can submit the PR first, and if it will have impact on performance, the code reviewer will explain it), be sure to attach a benchmark data here. -->
1 parent f97d9dc commit 7640dbb

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

java/fory-core/src/main/java/org/apache/fory/builder/BaseObjectCodecBuilder.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -666,9 +666,17 @@ protected Expression getGenericTypeField(TypeRef<?> typeRef) {
666666
String name =
667667
namesForSharedGenericTypeFields.computeIfAbsent(
668668
typeRef,
669-
k ->
670-
StringUtils.uncapitalize(typeRef.getRawType().getSimpleName())
671-
+ namesForSharedGenericTypeFields.size());
669+
k -> {
670+
String prefix;
671+
if (typeRef.getRawType().isArray()) {
672+
Tuple2<Class<?>, Integer> info =
673+
TypeUtils.getArrayComponentInfo(typeRef.getRawType());
674+
prefix = info.f0.getSimpleName() + "_arr" + info.f1;
675+
} else {
676+
prefix = typeRef.getRawType().getSimpleName();
677+
}
678+
return StringUtils.uncapitalize(prefix) + namesForSharedGenericTypeFields.size();
679+
});
672680

673681
return getOrCreateField(
674682
false,

java/fory-core/src/test/java/org/apache/fory/serializer/collection/MapSerializersTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,4 +1028,24 @@ public void testNullChunkGeneric() {
10281028
Object object = fory2.deserialize(bytes);
10291029
assertEquals(object, o);
10301030
}
1031+
1032+
@Data
1033+
@AllArgsConstructor
1034+
public static class State<K extends Comparable<K>, V> {
1035+
Map<K, V[]> map;
1036+
}
1037+
1038+
@Test
1039+
public void testChunkArrayGeneric() {
1040+
Fory fory = Fory.builder().withLanguage(Language.JAVA).requireClassRegistration(false).build();
1041+
State original = new State(ofHashMap("foo", new String[] {"bar"}));
1042+
State state = serDe(fory, original);
1043+
Assert.assertEquals(state.map.get("foo"), new String[] {"bar"});
1044+
1045+
State state1 = new State(ofHashMap("foo", null, "bar", new String[] {"bar"}));
1046+
byte[] bytes = fory.serialize(state1);
1047+
Fory fory2 = builder().withCodegen(false).build();
1048+
State state2 = (State) fory2.deserialize(bytes);
1049+
Assert.assertEquals(state2.map.get("bar"), new String[] {"bar"});
1050+
}
10311051
}

0 commit comments

Comments
 (0)