Skip to content

Commit 4bfa0d5

Browse files
committed
fix record nested generics
1 parent 4a04ac8 commit 4bfa0d5

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
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: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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 = jsonb.type(Types.newParameterizedType(MyListWrapper.class, Address.class));
33+
34+
var wrapper = (MyListWrapperRecord<Address>) jsonOfParams.fromJson(asJson);
35+
assertThat(wrapper.list()).hasSize(2);
36+
assertThat(wrapper.list().get(0)).isInstanceOf(Address.class);
37+
assertThat(wrapper.list().get(1)).isInstanceOf(Address.class);
38+
}
39+
}

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

Lines changed: 4 additions & 3 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;
@@ -182,7 +183,7 @@ private boolean isObjectBoolean() {
182183
}
183184

184185
private boolean isBoolean() {
185-
return ("boolean".equals(genericType.topType()) || "java.lang.Boolean".equals(genericType.topType()));
186+
return "boolean".equals(genericType.topType()) || "java.lang.Boolean".equals(genericType.topType());
186187
}
187188

188189
private boolean nameHasIsPrefix() {
@@ -288,7 +289,7 @@ void writeFromJsonVariables(Append writer, String num) {
288289
}
289290

290291
void writeFromJsonVariablesRecord(Append writer, String num) {
291-
final String type = genericTypeParameter ? "Object" : genericType.shortType();
292+
final String type = typeParamToObject(genericType.shortType());
292293
writer.append(" %s _val$%s = %s;", pad(type), fieldName + num, defaultValue).eol();
293294
}
294295

0 commit comments

Comments
 (0)