Merge pull request #770 from KyleAure/607-jsonbtypeadapter-records test: jsonb type adapater on records
diff --git a/src/test/java/org/eclipse/yasson/records/RecordTest.java b/src/test/java/org/eclipse/yasson/records/RecordTest.java index 526b0d0..813120d 100644 --- a/src/test/java/org/eclipse/yasson/records/RecordTest.java +++ b/src/test/java/org/eclipse/yasson/records/RecordTest.java
@@ -21,6 +21,8 @@ import org.eclipse.yasson.internal.properties.Messages; import org.junit.jupiter.api.Test; +import java.util.Set; + import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.is; @@ -208,4 +210,31 @@ assertThat(json, containsString("\"getDisplayName\":\"skoda (green)\"")); assertThat(json, not(containsString("\"displayName\""))); } + + // ----------------------------------------------------------------- + // Issue #607 — @JsonbTypeAdapter on record components + // ----------------------------------------------------------------- + + /** + * Serialising a record whose component is annotated with {@code @JsonbTypeAdapter} + * must invoke the adapter and produce the adapted JSON value. + */ + @Test + public void testRecordWithTypeAdapterSerialisation() { + RecordWithTypeAdapter record = new RecordWithTypeAdapter(Set.of("b", "a", "c")); + // SetToStringAdapter.adaptToJson sorts entries before joining + String json = Jsonbs.defaultJsonb.toJson(record); + assertThat(json, is("{\"entries\":\"a,b,c\"}")); + } + + /** + * Deserialising into a record whose component is annotated with {@code @JsonbTypeAdapter} + * must invoke the adapter and reconstruct the original collection value. + */ + @Test + public void testRecordWithTypeAdapterDeserialisation() { + String json = "{\"entries\":\"a,b,c\"}"; + RecordWithTypeAdapter record = Jsonbs.defaultJsonb.fromJson(json, RecordWithTypeAdapter.class); + assertThat(record.entries(), is(Set.of("a", "b", "c"))); + } }
diff --git a/src/test/java/org/eclipse/yasson/records/RecordWithTypeAdapter.java b/src/test/java/org/eclipse/yasson/records/RecordWithTypeAdapter.java new file mode 100644 index 0000000..5b90e78 --- /dev/null +++ b/src/test/java/org/eclipse/yasson/records/RecordWithTypeAdapter.java
@@ -0,0 +1,28 @@ +/* + * Copyright (c) 2026 Contributors to the Eclipse Foundation + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0, + * or the Eclipse Distribution License v. 1.0 which is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause + */ + +package org.eclipse.yasson.records; + +import jakarta.json.bind.annotation.JsonbProperty; +import jakarta.json.bind.annotation.JsonbTypeAdapter; + +import java.util.Set; + +/** + * Reproducer record for issue #607: {@code @JsonbTypeAdapter} annotated on a record component + * must be recognised and applied during both serialisation and deserialisation. + */ +public record RecordWithTypeAdapter( + @JsonbProperty("entries") + @JsonbTypeAdapter(SetToStringAdapter.class) + Set<String> entries) { +}
diff --git a/src/test/java/org/eclipse/yasson/records/SetToStringAdapter.java b/src/test/java/org/eclipse/yasson/records/SetToStringAdapter.java new file mode 100644 index 0000000..ceb453d --- /dev/null +++ b/src/test/java/org/eclipse/yasson/records/SetToStringAdapter.java
@@ -0,0 +1,39 @@ +/* + * Copyright (c) 2026 Contributors to the Eclipse Foundation + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0, + * or the Eclipse Distribution License v. 1.0 which is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause + */ + +package org.eclipse.yasson.records; + +import jakarta.json.bind.adapter.JsonbAdapter; + +import java.util.Arrays; +import java.util.LinkedHashSet; +import java.util.Set; +import java.util.stream.Collectors; + +/** + * Adapter used to reproduce issue #607: {@code @JsonbTypeAdapter} on a record component + * must be recognised and invoked for both serialisation and deserialisation. + * <p> + * Converts a {@code Set<String>} to/from a comma-separated {@code String}. + */ +public class SetToStringAdapter implements JsonbAdapter<Set<String>, String> { + + @Override + public String adaptToJson(Set<String> obj) { + return obj.stream().sorted().collect(Collectors.joining(",")); + } + + @Override + public Set<String> adaptFromJson(String obj) { + return new LinkedHashSet<>(Arrays.asList(obj.split(","))); + } +}