test: jsonbTransient tests on record Co-authored-by-AI: IBM Bob (1.126.0+bob2.1.0)
diff --git a/src/test/java/org/eclipse/yasson/customization/transients/JsonbTransientTest.java b/src/test/java/org/eclipse/yasson/customization/transients/JsonbTransientTest.java index 5d2f053..c1d36a0 100644 --- a/src/test/java/org/eclipse/yasson/customization/transients/JsonbTransientTest.java +++ b/src/test/java/org/eclipse/yasson/customization/transients/JsonbTransientTest.java
@@ -19,9 +19,6 @@ import org.eclipse.yasson.customization.transients.models.*; import jakarta.json.bind.JsonbException; -import jakarta.json.bind.Jsonb; -import jakarta.json.bind.JsonbBuilder; -import jakarta.json.bind.JsonbConfig; import jakarta.json.bind.annotation.JsonbTransient; /** @@ -167,6 +164,57 @@ assertEquals("{\"instance\":\"INSTANCE\"}", defaultJsonb.toJson(pojo)); } + // ------------------------------------------------------------------------- + // Record variants + // ------------------------------------------------------------------------- + + /** + * Serialization: verifies that {@link JsonbTransient} on a record component, + * on an accessor method, on both together, and on a virtual (non-component) + * accessor method each suppress the property from the JSON output, while an + * unannotated component remains visible. + */ + @Test + public void testJsonbTransientRecordSerialize() { + JsonbTransientRecord record = new JsonbTransientRecord( + "non transient", + "component transient value", + "accessor transient value", + "component and accessor transient value" + ); + + assertEquals("{\"plainProperty\":\"non transient\"}", defaultJsonb.toJson(record)); + } + + /** + * Deserialization: verifies the correct transient semantics for records: + * <ul> + * <li>{@link JsonbTransient} on a record <b>component</b> (backing field) suppresses + * both serialization and deserialization — the JSON value is not bound.</li> + * <li>{@link JsonbTransient} on the <b>accessor method</b> only suppresses serialization; + * the component is still populated during deserialization because the accessor + * annotation is the equivalent of a getter annotation on a regular class.</li> + * <li>When present on both component and accessor, the component annotation governs + * and the value is not bound.</li> + * </ul> + */ + @Test + public void testJsonbTransientRecordDeserialize() { + JsonbTransientRecord result = defaultJsonb.fromJson( + "{\"plainProperty\":\"plainProperty value\"," + + "\"componentTransient\":\"component transient value\"," + + "\"accessorTransient\":\"accessor transient value\"," + + "\"componentAndAccessorTransient\":\"component and accessor transient value\"," + + "\"virtualAttributeTransient\":\"virtual transient value\"" + + "}", + JsonbTransientRecord.class); + + assertEquals("plainProperty value", result.plainProperty()); + assertNull(result.componentTransient()); + assertEquals("accessor transient value", result.accessorTransient()); + assertNull(result.componentAndAccessorTransient()); + } + @Test public void testTransientGetterNoField() { TransientGetterNoField pojo = new TransientGetterNoField();
diff --git a/src/test/java/org/eclipse/yasson/customization/transients/models/JsonbTransientRecord.java b/src/test/java/org/eclipse/yasson/customization/transients/models/JsonbTransientRecord.java new file mode 100644 index 0000000..13f6b08 --- /dev/null +++ b/src/test/java/org/eclipse/yasson/customization/transients/models/JsonbTransientRecord.java
@@ -0,0 +1,58 @@ +/* + * 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.customization.transients.models; + +import jakarta.json.bind.annotation.JsonbTransient; + +/** + * Record variant of {@link JsonbTransientValue} for testing {@link JsonbTransient} on record + * components and record accessor methods. + * + * <ul> + * <li>{@code plainProperty} – not transient, always visible</li> + * <li>{@code componentTransient} – {@code @JsonbTransient} on the record component</li> + * <li>{@code accessorTransient} – {@code @JsonbTransient} on the accessor method</li> + * <li>{@code componentAndAccessorTransient} – {@code @JsonbTransient} on both</li> + * </ul> + */ +public record JsonbTransientRecord( + String plainProperty, + + @JsonbTransient + String componentTransient, + + String accessorTransient, + + @JsonbTransient + String componentAndAccessorTransient +) { + + /** Accessor annotated with {@link JsonbTransient} to suppress serialization. */ + @JsonbTransient + @Override + public String accessorTransient() { + return accessorTransient; + } + + /** Accessor annotated with {@link JsonbTransient} alongside the component annotation. */ + @JsonbTransient + @Override + public String componentAndAccessorTransient() { + return componentAndAccessorTransient; + } + + @JsonbTransient + public String virtualAttributeTransient() { + return "a string"; + } +}