| 
 | 1 | +/*  | 
 | 2 | + * Copyright (c) 2025 IBM and/or its affiliates. All rights reserved.  | 
 | 3 | + *  | 
 | 4 | + * This program and the accompanying materials are made available under the  | 
 | 5 | + * terms of the Eclipse Public License v. 2.0 which is available at  | 
 | 6 | + * http://www.eclipse.org/legal/epl-2.0,  | 
 | 7 | + * or the Eclipse Distribution License v. 1.0 which is available at  | 
 | 8 | + * http://www.eclipse.org/org/documents/edl-v10.php.  | 
 | 9 | + *  | 
 | 10 | + * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause  | 
 | 11 | + */  | 
 | 12 | + | 
 | 13 | +package org.eclipse.yasson.serializers;  | 
 | 14 | + | 
 | 15 | +import java.lang.reflect.Type;  | 
 | 16 | +import java.util.Arrays;  | 
 | 17 | +import java.util.List;  | 
 | 18 | +import java.util.Map;  | 
 | 19 | +import java.util.Optional;  | 
 | 20 | + | 
 | 21 | +import jakarta.json.bind.Jsonb;  | 
 | 22 | +import jakarta.json.bind.JsonbBuilder;  | 
 | 23 | +import jakarta.json.bind.JsonbConfig;  | 
 | 24 | +import jakarta.json.bind.annotation.JsonbTypeDeserializer;  | 
 | 25 | +import jakarta.json.bind.config.BinaryDataStrategy;  | 
 | 26 | +import jakarta.json.bind.serializer.DeserializationContext;  | 
 | 27 | +import jakarta.json.bind.serializer.JsonbDeserializer;  | 
 | 28 | +import jakarta.json.stream.JsonParser;  | 
 | 29 | +import org.junit.jupiter.api.AfterEach;  | 
 | 30 | +import org.junit.jupiter.api.Assertions;  | 
 | 31 | +import org.junit.jupiter.api.BeforeEach;  | 
 | 32 | +import org.junit.jupiter.api.Test;  | 
 | 33 | + | 
 | 34 | +/**  | 
 | 35 | + * Tests that {@link jakarta.json.bind.annotation.JsonbTypeDeserializer @JsonbTypeDeserializer} annotated types are  | 
 | 36 | + * properly detected and used when those types are used as elements/values in containers (Maps, Collections,  | 
 | 37 | + * Arrays, Optionals).  | 
 | 38 | + *  | 
 | 39 | + * @author <a href="mailto:[email protected]">James R. Perkins</a>  | 
 | 40 | + */  | 
 | 41 | +public class TypeDeserializerOnContainersTest {  | 
 | 42 | + | 
 | 43 | +    // Test interface with type-level deserializer annotation  | 
 | 44 | +    @JsonbTypeDeserializer(TestInterfaceDeserializer.class)  | 
 | 45 | +    public interface TestInterface {  | 
 | 46 | +        String getValue();  | 
 | 47 | +    }  | 
 | 48 | + | 
 | 49 | +    // Implementation of the test interface  | 
 | 50 | +    public static class TestImpl implements TestInterface {  | 
 | 51 | +        private final String value;  | 
 | 52 | + | 
 | 53 | +        public TestImpl(final String value) {  | 
 | 54 | +            this.value = value;  | 
 | 55 | +        }  | 
 | 56 | + | 
 | 57 | +        @Override  | 
 | 58 | +        public String getValue() {  | 
 | 59 | +            return value;  | 
 | 60 | +        }  | 
 | 61 | +    }  | 
 | 62 | + | 
 | 63 | +    // Custom deserializer for TestInterface  | 
 | 64 | +    public static class TestInterfaceDeserializer implements JsonbDeserializer<TestInterface> {  | 
 | 65 | +        @Override  | 
 | 66 | +        public TestInterface deserialize(final JsonParser parser, final DeserializationContext ctx, final Type rtType) {  | 
 | 67 | +            // Parse the JSON object to get the value field  | 
 | 68 | +            Assertions.assertTrue(parser.hasNext(), "Expected the key name");  | 
 | 69 | +            parser.next();  | 
 | 70 | +            Assertions.assertTrue(parser.hasNext(), "Expected the value");  | 
 | 71 | +            parser.next();  | 
 | 72 | +            final String value = parser.getString();  | 
 | 73 | +            Assertions.assertTrue(parser.hasNext(), "Expected the end of an object");  | 
 | 74 | +            parser.next();  | 
 | 75 | +            return new TestImpl("DESERIALIZED:" + value);  | 
 | 76 | +        }  | 
 | 77 | +    }  | 
 | 78 | + | 
 | 79 | +    // Container classes for testing  | 
 | 80 | +    public static class MapContainer {  | 
 | 81 | +        public Map<String, TestInterface> map;  | 
 | 82 | +    }  | 
 | 83 | + | 
 | 84 | +    public static class ListContainer {  | 
 | 85 | +        public List<TestInterface> list;  | 
 | 86 | +    }  | 
 | 87 | + | 
 | 88 | +    public static class ArrayContainer {  | 
 | 89 | +        public TestInterface[] array;  | 
 | 90 | +    }  | 
 | 91 | + | 
 | 92 | +    public static class OptionalContainer {  | 
 | 93 | +        @SuppressWarnings("OptionalUsedAsFieldOrParameterType")  | 
 | 94 | +        public Optional<TestInterface> optional;  | 
 | 95 | +    }  | 
 | 96 | + | 
 | 97 | +    public static class ByteArrayContainer {  | 
 | 98 | +        public byte[] data;  | 
 | 99 | +    }  | 
 | 100 | + | 
 | 101 | +    private Jsonb jsonb;  | 
 | 102 | + | 
 | 103 | +    @BeforeEach  | 
 | 104 | +    public void createJsonb() {  | 
 | 105 | +        // Create a new Jsonb for each test to avoid type caching  | 
 | 106 | +        jsonb = JsonbBuilder.create();  | 
 | 107 | +    }  | 
 | 108 | + | 
 | 109 | +    @AfterEach  | 
 | 110 | +    public void closeJsonb() throws Exception {  | 
 | 111 | +        if (jsonb != null) {  | 
 | 112 | +            jsonb.close();  | 
 | 113 | +        }  | 
 | 114 | +    }  | 
 | 115 | + | 
 | 116 | +    @Test  | 
 | 117 | +    public void testTypeDeserializerOnMapValues() {  | 
 | 118 | +        final String json = "{\"map\":{\"key1\":{\"value\":\"value1\"},\"key2\":{\"value\":\"value2\"}}}";  | 
 | 119 | + | 
 | 120 | +        MapContainer result = jsonb.fromJson(json, MapContainer.class);  | 
 | 121 | + | 
 | 122 | +        Assertions.assertNotNull(result.map);  | 
 | 123 | +        Assertions.assertEquals(2, result.map.size(), () -> String.format("Expected two entries got %s", result.map));  | 
 | 124 | +        Assertions.assertEquals("DESERIALIZED:value1", result.map.get("key1").getValue());  | 
 | 125 | +        Assertions.assertEquals("DESERIALIZED:value2", result.map.get("key2").getValue());  | 
 | 126 | +    }  | 
 | 127 | + | 
 | 128 | +    @Test  | 
 | 129 | +    public void testTypeDeserializerOnListElements() {  | 
 | 130 | +        String json = "{\"list\":[{\"value\":\"value1\"},{\"value\":\"value2\"}]}";  | 
 | 131 | + | 
 | 132 | +        ListContainer result = jsonb.fromJson(json, ListContainer.class);  | 
 | 133 | + | 
 | 134 | +        Assertions.assertNotNull(result.list);  | 
 | 135 | +        Assertions.assertEquals(2, result.list.size(), () -> String.format("Expected two entries got %s", result.list));  | 
 | 136 | +        Assertions.assertEquals("DESERIALIZED:value1", result.list.get(0).getValue());  | 
 | 137 | +        Assertions.assertEquals("DESERIALIZED:value2", result.list.get(1).getValue());  | 
 | 138 | +    }  | 
 | 139 | + | 
 | 140 | +    @Test  | 
 | 141 | +    public void testTypeDeserializerOnArrayElements() {  | 
 | 142 | +        String json = "{\"array\":[{\"value\":\"value1\"},{\"value\":\"value2\"}]}";  | 
 | 143 | + | 
 | 144 | +        ArrayContainer result = jsonb.fromJson(json, ArrayContainer.class);  | 
 | 145 | + | 
 | 146 | +        Assertions.assertNotNull(result.array);  | 
 | 147 | +        Assertions.assertEquals(2, result.array.length, () -> String.format("Expected two entries got %s", Arrays.toString(result.array)));  | 
 | 148 | +        Assertions.assertEquals("DESERIALIZED:value1", result.array[0].getValue());  | 
 | 149 | +        Assertions.assertEquals("DESERIALIZED:value2", result.array[1].getValue());  | 
 | 150 | +    }  | 
 | 151 | + | 
 | 152 | +    @Test  | 
 | 153 | +    public void testTypeDeserializerOnOptionalValue() {  | 
 | 154 | +        String json = "{\"optional\":{\"value\":\"value1\"}}";  | 
 | 155 | + | 
 | 156 | +        OptionalContainer result = jsonb.fromJson(json, OptionalContainer.class);  | 
 | 157 | + | 
 | 158 | +        Assertions.assertNotNull(result.optional);  | 
 | 159 | +        Assertions.assertTrue(result.optional.isPresent(), "Expected value to be present, but the optional was empty.");  | 
 | 160 | +        Assertions.assertEquals("DESERIALIZED:value1", result.optional.get().getValue());  | 
 | 161 | +    }  | 
 | 162 | + | 
 | 163 | +    @Test  | 
 | 164 | +    public void testTypeDeserializerOnByteArray() {  | 
 | 165 | +        String json = "{\"data\":[1,2,3,4,5]}";  | 
 | 166 | + | 
 | 167 | +        ByteArrayContainer result = jsonb.fromJson(json, ByteArrayContainer.class);  | 
 | 168 | + | 
 | 169 | +        Assertions.assertNotNull(result.data);  | 
 | 170 | +        Assertions.assertEquals(5, result.data.length);  | 
 | 171 | +        Assertions.assertArrayEquals(new byte[]{1, 2, 3, 4, 5}, result.data);  | 
 | 172 | +    }  | 
 | 173 | + | 
 | 174 | +    @Test  | 
 | 175 | +    public void testTypeDeserializerOnByteArrayWithBase64() throws Exception {  | 
 | 176 | +        try (Jsonb base64Jsonb = JsonbBuilder.create(new JsonbConfig()  | 
 | 177 | +                .withBinaryDataStrategy(BinaryDataStrategy.BASE_64))) {  | 
 | 178 | + | 
 | 179 | +            // "SGVsbG8=" is "Hello" in base64  | 
 | 180 | +            String json = "{\"data\":\"SGVsbG8=\"}";  | 
 | 181 | + | 
 | 182 | +            ByteArrayContainer result = base64Jsonb.fromJson(json, ByteArrayContainer.class);  | 
 | 183 | + | 
 | 184 | +            Assertions.assertNotNull(result.data);  | 
 | 185 | +            Assertions.assertArrayEquals("Hello".getBytes(), result.data);  | 
 | 186 | +        }  | 
 | 187 | +    }  | 
 | 188 | +}  | 
0 commit comments