Skip to content

Commit 90b7099

Browse files
authored
Fix record nested generics (#421)
* fix record nested generics * Update FieldProperty.java * Update MyListWrapperRecordTest.java
1 parent 4a04ac8 commit 90b7099

File tree

3 files changed

+58
-6
lines changed

3 files changed

+58
-6
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.example.customer.generics;
2+
3+
import java.util.List;
4+
5+
import io.avaje.jsonb.Json;
6+
7+
@Json
8+
public record MyListWrapperRecord<T>(List<T> list) {}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.example.customer.generics;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.util.List;
6+
7+
import org.example.customer.Address;
8+
import org.junit.jupiter.api.Test;
9+
10+
import io.avaje.jsonb.Jsonb;
11+
import io.avaje.jsonb.Types;
12+
13+
class MyListWrapperRecordTest {
14+
15+
Jsonb jsonb = Jsonb.instance();
16+
17+
private static MyListWrapperRecord<Address> createData() {
18+
return new MyListWrapperRecord<>(List.of(new Address(90L, "one"), new Address(91L, "two")));
19+
}
20+
21+
@SuppressWarnings("unchecked")
22+
@Test
23+
void toJson() {
24+
var bean = createData();
25+
26+
var type = jsonb.type(MyListWrapperRecord.class);
27+
28+
String asJson = type.toJson(bean);
29+
assertThat(asJson)
30+
.isEqualTo("{\"list\":[{\"id\":90,\"street\":\"one\"},{\"id\":91,\"street\":\"two\"}]}");
31+
32+
var jsonOfParams =
33+
jsonb.type(Types.newParameterizedType(MyListWrapperRecord.class, Address.class));
34+
35+
var wrapper = (MyListWrapperRecord<Address>) jsonOfParams.fromJson(asJson);
36+
assertThat(wrapper.list()).hasSize(2);
37+
assertThat(wrapper.list().get(0)).isInstanceOf(Address.class);
38+
assertThat(wrapper.list().get(1)).isInstanceOf(Address.class);
39+
}
40+
}

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package io.avaje.jsonb.generator;
22

3-
import javax.lang.model.type.TypeMirror;
43
import java.util.ArrayList;
54
import java.util.List;
65
import java.util.Optional;
76
import java.util.Set;
87

8+
import javax.lang.model.type.TypeMirror;
9+
910
final class FieldProperty {
1011

1112
private final boolean raw;
@@ -159,16 +160,19 @@ private String initShortName() {
159160
return Util.initLower(genericType.shortName()) + "JsonAdapter";
160161
}
161162

162-
String typeParamToObject(String shortType) {
163+
String typeParamToObject() {
164+
var shortType = genericType.shortType();
163165
for (final String typeParam : genericTypeParams) {
166+
if (shortType.equals(typeParam)) {
167+
return "Object";
168+
}
164169
if (shortType.contains("<" + typeParam + ">")) {
165170
shortType = shortType.replace("<" + typeParam + ">", "<Object>");
166171
}
167172
}
168173
return shortType;
169174
}
170175

171-
172176
boolean typeObjectBooleanWithIsPrefix() {
173177
return nameHasIsPrefix() && isObjectBoolean();
174178
}
@@ -182,7 +186,7 @@ private boolean isObjectBoolean() {
182186
}
183187

184188
private boolean isBoolean() {
185-
return ("boolean".equals(genericType.topType()) || "java.lang.Boolean".equals(genericType.topType()));
189+
return "boolean".equals(genericType.topType()) || "java.lang.Boolean".equals(genericType.topType());
186190
}
187191

188192
private boolean nameHasIsPrefix() {
@@ -279,7 +283,7 @@ void writeFromJsonVariables(Append writer, String num) {
279283
if (unmapped) {
280284
return;
281285
}
282-
final String shortType = typeParamToObject(genericType.shortType());
286+
final String shortType = typeParamToObject();
283287
writer.append(" %s _val$%s = %s;", pad(shortType), fieldName + num, defaultValue);
284288
if (!constructorParam && !optional) {
285289
writer.append(" boolean _set$%s = false;", fieldName + num);
@@ -288,7 +292,7 @@ void writeFromJsonVariables(Append writer, String num) {
288292
}
289293

290294
void writeFromJsonVariablesRecord(Append writer, String num) {
291-
final String type = genericTypeParameter ? "Object" : genericType.shortType();
295+
final String type = typeParamToObject();
292296
writer.append(" %s _val$%s = %s;", pad(type), fieldName + num, defaultValue).eol();
293297
}
294298

0 commit comments

Comments
 (0)