-
Notifications
You must be signed in to change notification settings - Fork 105
issue-673 fix #694
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
redmitry
wants to merge
2
commits into
eclipse-ee4j:master
Choose a base branch
from
redmitry:master
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
issue-673 fix #694
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
135 changes: 135 additions & 0 deletions
135
src/test/java/org/eclipse/yasson/jsonstructure/Issue673.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,135 @@ | ||
| /* | ||
| * 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.jsonstructure; | ||
|
|
||
| import jakarta.json.Json; | ||
| import jakarta.json.JsonArray; | ||
| import jakarta.json.JsonObject; | ||
| import jakarta.json.JsonString; | ||
| import jakarta.json.JsonValue; | ||
| import jakarta.json.bind.annotation.JsonbSubtype; | ||
| import jakarta.json.bind.annotation.JsonbTypeDeserializer; | ||
| import jakarta.json.bind.annotation.JsonbTypeInfo; | ||
| import jakarta.json.bind.serializer.DeserializationContext; | ||
| import jakarta.json.bind.serializer.JsonbDeserializer; | ||
| import jakarta.json.stream.JsonParser; | ||
| import java.lang.reflect.Type; | ||
| import java.util.Collections; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class Issue673 { | ||
|
|
||
| public static interface Referenceable { | ||
|
|
||
| } | ||
|
|
||
| public static class Reference implements Referenceable { | ||
|
|
||
| private String description; | ||
|
|
||
| public String getDescription() { | ||
| return description; | ||
| } | ||
|
|
||
| public void setDescription(String description) { | ||
| this.description = description; | ||
| } | ||
| } | ||
|
|
||
| public static class IRIReference implements Referenceable { | ||
|
|
||
| private String value; | ||
|
|
||
| public IRIReference() {} | ||
|
|
||
| public IRIReference(String value) { | ||
| this.value = value; | ||
| } | ||
|
|
||
| public String getValue() { | ||
| return value; | ||
| } | ||
|
|
||
| public void setValue(String value) { | ||
| this.value = value; | ||
| } | ||
| } | ||
|
|
||
| @JsonbTypeInfo(key = "type", value = { | ||
| @JsonbSubtype(alias = Location.TYPE, | ||
| type = Location.class) | ||
| }) | ||
| public static interface LocationInterface { | ||
|
|
||
| } | ||
|
|
||
| public static class Location implements LocationInterface { | ||
|
|
||
| public final static String TYPE = "Location"; | ||
|
|
||
| private String tags; | ||
| private Referenceable referenceable; | ||
|
|
||
| @JsonbTypeDeserializer(TagsDeserializer.class) | ||
| public String getTags() { | ||
| return tags; | ||
| } | ||
|
|
||
| public void setTags(String tags) { | ||
| this.tags = tags; | ||
| } | ||
|
|
||
| @JsonbTypeDeserializer(ReferenceableDeserializer.class) | ||
| public Referenceable getReference() { | ||
| return referenceable; | ||
| } | ||
|
|
||
| public void setReference(Referenceable referenceable) { | ||
| this.referenceable = referenceable; | ||
| } | ||
| } | ||
|
|
||
| public static class TagsDeserializer implements JsonbDeserializer<String> { | ||
| @Override | ||
| public String deserialize(JsonParser jp, DeserializationContext dc, Type type) { | ||
| final JsonValue v = jp.getArray(); | ||
| if (v instanceof JsonArray arr) { | ||
| return arr.stream() | ||
| .filter(JsonString.class::isInstance) | ||
| .map(JsonString.class::cast) | ||
| .map(JsonString::getString) | ||
| .collect(Collectors.joining(", ")); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| public static class ReferenceableDeserializer implements JsonbDeserializer<Referenceable> { | ||
|
|
||
| @Override | ||
| public Referenceable deserialize(JsonParser jp, DeserializationContext dc, Type type) { | ||
| final JsonValue v = jp.getValue(); | ||
| if (v instanceof JsonString str) { | ||
| return new IRIReference(str.getString()); | ||
| } | ||
| if (v instanceof JsonObject obj) { | ||
| dc.deserialize(Reference.class, | ||
| Json.createParserFactory(Collections.EMPTY_MAP) | ||
| .createParser(obj)); | ||
| } | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| } | ||
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 | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,5 +1,6 @@ | ||||||||||||||||||||||||||||||
| /* | ||||||||||||||||||||||||||||||
| * Copyright (c) 2019, 2020 Oracle and/or its affiliates. All rights reserved. | ||||||||||||||||||||||||||||||
| * 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 | ||||||||||||||||||||||||||||||
|
|
@@ -23,6 +24,7 @@ | |||||||||||||||||||||||||||||
| import jakarta.json.JsonArrayBuilder; | ||||||||||||||||||||||||||||||
| import jakarta.json.JsonObject; | ||||||||||||||||||||||||||||||
| import jakarta.json.JsonObjectBuilder; | ||||||||||||||||||||||||||||||
| import jakarta.json.bind.Jsonb; | ||||||||||||||||||||||||||||||
| import jakarta.json.bind.JsonbBuilder; | ||||||||||||||||||||||||||||||
| import jakarta.json.bind.JsonbConfig; | ||||||||||||||||||||||||||||||
| import jakarta.json.spi.JsonProvider; | ||||||||||||||||||||||||||||||
|
|
@@ -270,4 +272,35 @@ public void testCustomJsonbDeserializer() { | |||||||||||||||||||||||||||||
| assertEquals("String value 1", result.getInner().getInnerFirst()); | ||||||||||||||||||||||||||||||
| assertEquals("String value 2", result.getInner().getInnerSecond()); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| @Test | ||||||||||||||||||||||||||||||
| public void testGetValue() { | ||||||||||||||||||||||||||||||
| final String json = | ||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||
| "type": "Location", | ||||||||||||||||||||||||||||||
| "reference": "dummy reference" | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| """; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| Jsonb jsonb = JsonbBuilder.create( | ||||||||||||||||||||||||||||||
| new JsonbConfig().withDeserializers(new Issue673.ReferenceableDeserializer())); | ||||||||||||||||||||||||||||||
| jsonb.fromJson(json, Issue673.LocationInterface.class); | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
| jsonb.fromJson(json, Issue673.LocationInterface.class); | |
| LocationInterface result = jsonb.fromJson(json, Issue673.LocationInterface.class); | |
| assertNotNull(result); | |
| assertTrue(result instanceof Location); | |
| Location location = (Location) result; | |
| Referenceable refAble = location.getReference(); | |
| assertNotNull(refAble); | |
| assertFalse(refAble instanceof Reference); | |
| assertTrue(refAble instanceof IRIReference); | |
| IRIReference ref = (IRIReference) refAble; | |
| assertEquals("dummy reference", ref.getValue()); |
suggestion: this test just verifiers that no exception is thrown, ensure the correct object is returned.
redmitry marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
redmitry marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
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.