diff --git a/blackbox-test/src/main/java/org/example/customer/generics/MyListWrapperRecord.java b/blackbox-test/src/main/java/org/example/customer/generics/MyListWrapperRecord.java new file mode 100644 index 00000000..d598890e --- /dev/null +++ b/blackbox-test/src/main/java/org/example/customer/generics/MyListWrapperRecord.java @@ -0,0 +1,8 @@ +package org.example.customer.generics; + +import java.util.List; + +import io.avaje.jsonb.Json; + +@Json +public record MyListWrapperRecord(List list) {} diff --git a/blackbox-test/src/test/java/org/example/customer/generics/MyListWrapperRecordTest.java b/blackbox-test/src/test/java/org/example/customer/generics/MyListWrapperRecordTest.java new file mode 100644 index 00000000..ae587272 --- /dev/null +++ b/blackbox-test/src/test/java/org/example/customer/generics/MyListWrapperRecordTest.java @@ -0,0 +1,40 @@ +package org.example.customer.generics; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.List; + +import org.example.customer.Address; +import org.junit.jupiter.api.Test; + +import io.avaje.jsonb.Jsonb; +import io.avaje.jsonb.Types; + +class MyListWrapperRecordTest { + + Jsonb jsonb = Jsonb.instance(); + + private static MyListWrapperRecord
createData() { + return new MyListWrapperRecord<>(List.of(new Address(90L, "one"), new Address(91L, "two"))); + } + + @SuppressWarnings("unchecked") + @Test + void toJson() { + var bean = createData(); + + var type = jsonb.type(MyListWrapperRecord.class); + + String asJson = type.toJson(bean); + assertThat(asJson) + .isEqualTo("{\"list\":[{\"id\":90,\"street\":\"one\"},{\"id\":91,\"street\":\"two\"}]}"); + + var jsonOfParams = + jsonb.type(Types.newParameterizedType(MyListWrapperRecord.class, Address.class)); + + var wrapper = (MyListWrapperRecord
) jsonOfParams.fromJson(asJson); + assertThat(wrapper.list()).hasSize(2); + assertThat(wrapper.list().get(0)).isInstanceOf(Address.class); + assertThat(wrapper.list().get(1)).isInstanceOf(Address.class); + } +} diff --git a/jsonb-generator/src/main/java/io/avaje/jsonb/generator/FieldProperty.java b/jsonb-generator/src/main/java/io/avaje/jsonb/generator/FieldProperty.java index 6abc72bf..efd8207c 100644 --- a/jsonb-generator/src/main/java/io/avaje/jsonb/generator/FieldProperty.java +++ b/jsonb-generator/src/main/java/io/avaje/jsonb/generator/FieldProperty.java @@ -1,11 +1,12 @@ package io.avaje.jsonb.generator; -import javax.lang.model.type.TypeMirror; import java.util.ArrayList; import java.util.List; import java.util.Optional; import java.util.Set; +import javax.lang.model.type.TypeMirror; + final class FieldProperty { private final boolean raw; @@ -159,8 +160,12 @@ private String initShortName() { return Util.initLower(genericType.shortName()) + "JsonAdapter"; } - String typeParamToObject(String shortType) { + String typeParamToObject() { + var shortType = genericType.shortType(); for (final String typeParam : genericTypeParams) { + if (shortType.equals(typeParam)) { + return "Object"; + } if (shortType.contains("<" + typeParam + ">")) { shortType = shortType.replace("<" + typeParam + ">", ""); } @@ -168,7 +173,6 @@ String typeParamToObject(String shortType) { return shortType; } - boolean typeObjectBooleanWithIsPrefix() { return nameHasIsPrefix() && isObjectBoolean(); } @@ -182,7 +186,7 @@ private boolean isObjectBoolean() { } private boolean isBoolean() { - return ("boolean".equals(genericType.topType()) || "java.lang.Boolean".equals(genericType.topType())); + return "boolean".equals(genericType.topType()) || "java.lang.Boolean".equals(genericType.topType()); } private boolean nameHasIsPrefix() { @@ -279,7 +283,7 @@ void writeFromJsonVariables(Append writer, String num) { if (unmapped) { return; } - final String shortType = typeParamToObject(genericType.shortType()); + final String shortType = typeParamToObject(); writer.append(" %s _val$%s = %s;", pad(shortType), fieldName + num, defaultValue); if (!constructorParam && !optional) { writer.append(" boolean _set$%s = false;", fieldName + num); @@ -288,7 +292,7 @@ void writeFromJsonVariables(Append writer, String num) { } void writeFromJsonVariablesRecord(Append writer, String num) { - final String type = genericTypeParameter ? "Object" : genericType.shortType(); + final String type = typeParamToObject(); writer.append(" %s _val$%s = %s;", pad(type), fieldName + num, defaultValue).eol(); }