-
Notifications
You must be signed in to change notification settings - Fork 105
[685] Ensure the value type of a container being deserialized uses th… #686
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jamezp
wants to merge
2
commits into
eclipse-ee4j:master
Choose a base branch
from
jamezp:issue685
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
188 changes: 188 additions & 0 deletions
188
src/test/java/org/eclipse/yasson/serializers/TypeDeserializerOnContainersTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| /* | ||
| * Copyright (c) 2025 IBM and/or its affiliates. All rights reserved. | ||
| * | ||
| * 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.serializers; | ||
|
|
||
| import java.lang.reflect.Type; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
|
|
||
| import jakarta.json.bind.Jsonb; | ||
| import jakarta.json.bind.JsonbBuilder; | ||
| import jakarta.json.bind.JsonbConfig; | ||
| import jakarta.json.bind.annotation.JsonbTypeDeserializer; | ||
| import jakarta.json.bind.config.BinaryDataStrategy; | ||
| import jakarta.json.bind.serializer.DeserializationContext; | ||
| import jakarta.json.bind.serializer.JsonbDeserializer; | ||
| import jakarta.json.stream.JsonParser; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| /** | ||
| * Tests that {@link jakarta.json.bind.annotation.JsonbTypeDeserializer @JsonbTypeDeserializer} annotated types are | ||
| * properly detected and used when those types are used as elements/values in containers (Maps, Collections, | ||
| * Arrays, Optionals). | ||
| * | ||
| * @author <a href="mailto:jperkins@ibm.com">James R. Perkins</a> | ||
| */ | ||
| public class TypeDeserializerOnContainersTest { | ||
|
|
||
| // Test interface with type-level deserializer annotation | ||
| @JsonbTypeDeserializer(TestInterfaceDeserializer.class) | ||
| public interface TestInterface { | ||
| String getValue(); | ||
| } | ||
|
|
||
| // Implementation of the test interface | ||
| public static class TestImpl implements TestInterface { | ||
| private final String value; | ||
|
|
||
| public TestImpl(final String value) { | ||
| this.value = value; | ||
| } | ||
|
|
||
| @Override | ||
| public String getValue() { | ||
| return value; | ||
| } | ||
| } | ||
|
|
||
| // Custom deserializer for TestInterface | ||
| public static class TestInterfaceDeserializer implements JsonbDeserializer<TestInterface> { | ||
| @Override | ||
| public TestInterface deserialize(final JsonParser parser, final DeserializationContext ctx, final Type rtType) { | ||
| // Parse the JSON object to get the value field | ||
| Assertions.assertTrue(parser.hasNext(), "Expected the key name"); | ||
| parser.next(); | ||
| Assertions.assertTrue(parser.hasNext(), "Expected the value"); | ||
| parser.next(); | ||
| final String value = parser.getString(); | ||
| Assertions.assertTrue(parser.hasNext(), "Expected the end of an object"); | ||
| parser.next(); | ||
| return new TestImpl("DESERIALIZED:" + value); | ||
| } | ||
| } | ||
|
|
||
| // Container classes for testing | ||
| public static class MapContainer { | ||
| public Map<String, TestInterface> map; | ||
| } | ||
|
|
||
| public static class ListContainer { | ||
| public List<TestInterface> list; | ||
| } | ||
|
|
||
| public static class ArrayContainer { | ||
| public TestInterface[] array; | ||
| } | ||
|
|
||
| public static class OptionalContainer { | ||
| @SuppressWarnings("OptionalUsedAsFieldOrParameterType") | ||
| public Optional<TestInterface> optional; | ||
| } | ||
|
|
||
| public static class ByteArrayContainer { | ||
| public byte[] data; | ||
| } | ||
|
|
||
| private Jsonb jsonb; | ||
|
|
||
| @BeforeEach | ||
| public void createJsonb() { | ||
| // Create a new Jsonb for each test to avoid type caching | ||
| jsonb = JsonbBuilder.create(); | ||
| } | ||
|
|
||
| @AfterEach | ||
| public void closeJsonb() throws Exception { | ||
| if (jsonb != null) { | ||
| jsonb.close(); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testTypeDeserializerOnMapValues() { | ||
| final String json = "{\"map\":{\"key1\":{\"value\":\"value1\"},\"key2\":{\"value\":\"value2\"}}}"; | ||
|
|
||
| final MapContainer result = jsonb.fromJson(json, MapContainer.class); | ||
|
|
||
| Assertions.assertNotNull(result.map); | ||
| Assertions.assertEquals(2, result.map.size(), () -> String.format("Expected two entries got %s", result.map)); | ||
| Assertions.assertEquals("DESERIALIZED:value1", result.map.get("key1").getValue()); | ||
| Assertions.assertEquals("DESERIALIZED:value2", result.map.get("key2").getValue()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testTypeDeserializerOnListElements() { | ||
| final String json = "{\"list\":[{\"value\":\"value1\"},{\"value\":\"value2\"}]}"; | ||
|
|
||
| final ListContainer result = jsonb.fromJson(json, ListContainer.class); | ||
|
|
||
| Assertions.assertNotNull(result.list); | ||
| Assertions.assertEquals(2, result.list.size(), () -> String.format("Expected two entries got %s", result.list)); | ||
| Assertions.assertEquals("DESERIALIZED:value1", result.list.get(0).getValue()); | ||
| Assertions.assertEquals("DESERIALIZED:value2", result.list.get(1).getValue()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testTypeDeserializerOnArrayElements() { | ||
| final String json = "{\"array\":[{\"value\":\"value1\"},{\"value\":\"value2\"}]}"; | ||
|
|
||
| final ArrayContainer result = jsonb.fromJson(json, ArrayContainer.class); | ||
|
|
||
| Assertions.assertNotNull(result.array); | ||
| Assertions.assertEquals(2, result.array.length, () -> String.format("Expected two entries got %s", Arrays.toString(result.array))); | ||
| Assertions.assertEquals("DESERIALIZED:value1", result.array[0].getValue()); | ||
| Assertions.assertEquals("DESERIALIZED:value2", result.array[1].getValue()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testTypeDeserializerOnOptionalValue() { | ||
| final String json = "{\"optional\":{\"value\":\"value1\"}}"; | ||
|
|
||
| final OptionalContainer result = jsonb.fromJson(json, OptionalContainer.class); | ||
|
|
||
| Assertions.assertNotNull(result.optional); | ||
| Assertions.assertTrue(result.optional.isPresent(), "Expected value to be present, but the optional was empty."); | ||
| Assertions.assertEquals("DESERIALIZED:value1", result.optional.get().getValue()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testTypeDeserializerOnByteArray() { | ||
| final String json = "{\"data\":[1,2,3,4,5]}"; | ||
|
|
||
| final ByteArrayContainer result = jsonb.fromJson(json, ByteArrayContainer.class); | ||
|
|
||
| Assertions.assertNotNull(result.data); | ||
| Assertions.assertEquals(5, result.data.length); | ||
| Assertions.assertArrayEquals(new byte[]{1, 2, 3, 4, 5}, result.data); | ||
| } | ||
|
|
||
| @Test | ||
| public void testTypeDeserializerOnByteArrayWithBase64() throws Exception { | ||
| try (Jsonb base64Jsonb = JsonbBuilder.create(new JsonbConfig() | ||
| .withBinaryDataStrategy(BinaryDataStrategy.BASE_64))) { | ||
|
|
||
| // "SGVsbG8=" is "Hello" in base64 | ||
| final String json = "{\"data\":\"SGVsbG8=\"}"; | ||
|
|
||
| final ByteArrayContainer result = base64Jsonb.fromJson(json, ByteArrayContainer.class); | ||
|
|
||
| Assertions.assertNotNull(result.data); | ||
| Assertions.assertArrayEquals("Hello".getBytes(), result.data); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.