Skip to content

Commit b0e4e3e

Browse files
committed
fix cascading of Map - cascade auto-register the value type of Map property
This assumes the key type of the Map is String in the json sense
1 parent 12365b7 commit b0e4e3e

File tree

5 files changed

+24
-9
lines changed

5 files changed

+24
-9
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package org.example.customer.cascade;
2+
3+
public record MCChild2(int myValue) {
4+
}

blackbox-test/src/main/java/org/example/customer/cascade/MCTop.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22

33
import io.avaje.jsonb.Json;
44

5+
import java.util.Collections;
56
import java.util.List;
7+
import java.util.Map;
68

79
@Json
8-
public record MCTop(int id, MCOther other, List<MCChild> children) {
10+
public record MCTop(int id, MCOther other, List<MCChild> children, Map<String,MCChild2> childMap) {
11+
12+
public MCTop(int id, MCOther foo, List<MCChild> children) {
13+
this(id, foo, children, Collections.emptyMap());
14+
}
915
}

blackbox-test/src/test/java/org/example/customer/cascade/MCTopTest.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
import io.avaje.jsonb.Jsonb;
55
import org.junit.jupiter.api.Test;
66

7-
import java.util.Collections;
8-
import java.util.List;
7+
import java.util.*;
98

109
import static org.assertj.core.api.Assertions.assertThat;
1110

@@ -18,9 +17,12 @@ void toJson() {
1817
JsonType<MCTop> type = jsonb.type(MCTop.class);
1918

2019
String asJson = type.toJson(new MCTop(42, new MCOther("foo"), Collections.emptyList()));
21-
assertThat(asJson).isEqualTo("{\"id\":42,\"other\":{\"other\":\"foo\"}}");
20+
assertThat(asJson).isEqualTo("{\"id\":42,\"other\":{\"other\":\"foo\"},\"childMap\":{}}");
2221

2322
String asJson2 = type.toJson(new MCTop(42, new MCOther("foo"), List.of(new MCChild(23), new MCChild(24))));
24-
assertThat(asJson2).isEqualTo("{\"id\":42,\"other\":{\"other\":\"foo\"},\"children\":[{\"myValue\":23},{\"myValue\":24}]}");
23+
assertThat(asJson2).isEqualTo("{\"id\":42,\"other\":{\"other\":\"foo\"},\"children\":[{\"myValue\":23},{\"myValue\":24}],\"childMap\":{}}");
24+
25+
String asJsonWithMap = type.toJson(new MCTop(42, new MCOther("foo"), Collections.emptyList(), new TreeMap<>(Map.of("k0", new MCChild2(98), "k1", new MCChild2(99)))));
26+
assertThat(asJsonWithMap).isEqualTo("{\"id\":42,\"other\":{\"other\":\"foo\"},\"childMap\":{\"k0\":{\"myValue\":98},\"k1\":{\"myValue\":99}}}");
2527
}
2628
}

jsonb-generator/src/main/java/io/avaje/jsonb/generator/FieldReader.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ void cascadeTypes(Set<String> types) {
108108
String topType = genericType.topType();
109109
if (topType.equals("java.util.List") || topType.equals("java.util.Set")) {
110110
types.add(genericType.firstParamType());
111+
} else if (topType.equals("java.util.Map")) {
112+
types.add(genericType.secondParamType());
111113
} else {
112114
types.add(topType);
113115
}

jsonb-generator/src/main/java/io/avaje/jsonb/generator/GenericType.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,10 @@ private String asTypeContainer() {
181181
}
182182

183183
String firstParamType() {
184-
if (params.isEmpty()) {
185-
return "java.lang.Object";
186-
}
187-
return params.get(0).topType();
184+
return params.isEmpty() ? "java.lang.Object" : params.get(0).topType();
185+
}
186+
187+
String secondParamType() {
188+
return params.size() != 2 ? "java.lang.Object" : params.get(1).topType();
188189
}
189190
}

0 commit comments

Comments
 (0)