-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Fix improper (De)Serialization given both @JsonTypeInfo and @JsonValue #5850
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
blacelle
wants to merge
3
commits into
FasterXML:3.x
Choose a base branch
from
solven-eu:jsoninfotype/jsonvalue
base: 3.x
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.
+283
−2
Open
Changes from all commits
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
266 changes: 266 additions & 0 deletions
266
src/test/java/tools/jackson/databind/jsontype/TestJsonTypeInfoJsonValue.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,266 @@ | ||
| package tools.jackson.databind.jsontype; | ||
|
|
||
| import java.util.Locale; | ||
| import java.util.Map; | ||
|
|
||
| import org.assertj.core.api.Assertions; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonCreator; | ||
| import com.fasterxml.jackson.annotation.JsonTypeInfo; | ||
| import com.fasterxml.jackson.annotation.JsonValue; | ||
|
|
||
| import tools.jackson.databind.ObjectMapper; | ||
|
|
||
| // Investigate around AsPropertyTypeSerializer | ||
| // tools.jackson.databind.ser.jackson.JsonValueSerializer.serializeWithType(Object, JsonGenerator, SerializationContext, TypeSerializer) | ||
| // tools.jackson.databind.ser.BasicSerializerFactory.findSerializerByAnnotations(SerializationContext, JavaType, Supplier) | ||
| public class TestJsonTypeInfoJsonValue { | ||
|
|
||
| @JsonTypeInfo(use = JsonTypeInfo.Id.MINIMAL_CLASS, | ||
| include = JsonTypeInfo.As.PROPERTY, | ||
| property = "type", | ||
| defaultImpl = AroundString.class) | ||
| public interface AroundSomething { | ||
| Object getInner(); | ||
| } | ||
|
|
||
| public static class AroundString implements AroundSomething { | ||
| @JsonValue | ||
| String inner; | ||
|
|
||
| @JsonCreator | ||
| public AroundString(String inner) { | ||
| this.inner = inner; | ||
| } | ||
|
|
||
| @Override | ||
| public Object getInner() { | ||
| return inner; | ||
| } | ||
|
|
||
| public void setInner(String inner) { | ||
| this.inner = inner; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| public static class AroundObject implements AroundSomething { | ||
| @JsonValue | ||
| Object inner; | ||
|
|
||
| @JsonCreator | ||
| public AroundObject(Object inner) { | ||
| this.inner = inner; | ||
| } | ||
|
|
||
| @Override | ||
| public Object getInner() { | ||
| return inner; | ||
| } | ||
|
|
||
| public void setInner(String inner) { | ||
| this.inner = inner; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| public static class AroundObject_NotJsonValue implements AroundSomething { | ||
| Object inner; | ||
|
|
||
| @Override | ||
| public Object getInner() { | ||
| return inner; | ||
| } | ||
|
|
||
| public void setInner(String inner) { | ||
| this.inner = inner; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| public static class HasAround { | ||
| AroundSomething wrapped; | ||
|
|
||
| public AroundSomething getWrapped() { | ||
| return wrapped; | ||
| } | ||
|
|
||
| public void setC(AroundSomething wrapped) { | ||
| this.wrapped = wrapped; | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void aroundString() { | ||
| AroundString matcher = new AroundString("foo"); | ||
|
|
||
| HasAround wrapper = new HasAround(); | ||
| wrapper.setC(matcher); | ||
|
|
||
| ObjectMapper objectMapper = new ObjectMapper(); | ||
|
|
||
| String asString = objectMapper.writeValueAsString(wrapper); | ||
| Assertions.assertThat(asString).isEqualTo("{\"wrapped\":\"foo\"}"); | ||
|
|
||
| HasAround fromString = objectMapper.readValue(asString, HasAround.class); | ||
| Assertions.assertThat(fromString.getWrapped().getInner()).isEqualTo("foo"); | ||
| } | ||
|
|
||
| @Test | ||
| public void aroundObject_simpleType() { | ||
| AroundObject matcher = new AroundObject("foo"); | ||
|
|
||
| HasAround wrapper = new HasAround(); | ||
| wrapper.setC(matcher); | ||
|
|
||
| ObjectMapper objectMapper = new ObjectMapper(); | ||
|
|
||
| String asString = objectMapper.writeValueAsString(wrapper); | ||
| Assertions.assertThat(asString).isEqualTo("{\"wrapped\":\"foo\"}"); | ||
|
|
||
| HasAround fromString = objectMapper.readValue(asString, HasAround.class); | ||
| Assertions.assertThat(fromString.getWrapped().getInner()).isEqualTo("foo"); | ||
| } | ||
|
|
||
| @Test | ||
| public void aroundObject_complexType() { | ||
| AroundObject matcher = new AroundObject(Map.of("foo", "bar")); | ||
|
|
||
| HasAround wrapper = new HasAround(); | ||
| wrapper.setC(matcher); | ||
|
|
||
| ObjectMapper objectMapper = new ObjectMapper(); | ||
|
|
||
| String asString = objectMapper.writeValueAsString(wrapper); | ||
| Assertions.assertThat(asString) | ||
| .isEqualTo("{\"wrapped\":{\"type\":\".TestJsonTypeInfoJsonValue$AroundObject\",\"foo\":\"bar\"}}"); | ||
|
|
||
| HasAround fromString = objectMapper.readValue(asString, HasAround.class); | ||
| Assertions.assertThat(fromString.getWrapped().getInner()).isEqualTo(Map.of("foo", "bar")); | ||
| } | ||
|
|
||
| @Test | ||
| public void aroundObjectNotJsonValue() { | ||
| AroundObject_NotJsonValue matcher = new AroundObject_NotJsonValue(); | ||
| matcher.setInner("foo"); | ||
|
|
||
| HasAround wrapper = new HasAround(); | ||
| wrapper.setC(matcher); | ||
|
|
||
| ObjectMapper objectMapper = new ObjectMapper(); | ||
|
|
||
| String asString = objectMapper.writeValueAsString(wrapper); | ||
| Assertions.assertThat(asString) | ||
| .isEqualTo( | ||
| "{\"wrapped\":{\"type\":\".TestJsonTypeInfoJsonValue$AroundObject_NotJsonValue\",\"inner\":\"foo\"}}"); | ||
| } | ||
|
|
||
| @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, | ||
| include = JsonTypeInfo.As.PROPERTY, | ||
| property = "type", | ||
| defaultImpl = NativeOption.class) | ||
| public interface SomeOption { | ||
| } | ||
|
|
||
| public static enum NativeOption implements SomeOption { | ||
| A, B; | ||
|
|
||
| @JsonValue | ||
| public String toString() { | ||
| return this.name(); | ||
| } | ||
|
|
||
| @JsonCreator | ||
| public static NativeOption forValue(String value) { | ||
| return NativeOption.valueOf(value.toUpperCase(Locale.US)); | ||
| } | ||
| } | ||
|
|
||
| public static enum CustomOption implements SomeOption { | ||
| C, D; | ||
|
|
||
| @JsonCreator | ||
| public static CustomOption forValue(String value) { | ||
| return CustomOption.valueOf(value.toUpperCase(Locale.US)); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| public static enum CustomOption_WithJsonValue implements SomeOption { | ||
| C, D; | ||
|
|
||
| @JsonValue | ||
| public String asString() { | ||
| return this.name(); | ||
| } | ||
|
|
||
| @JsonCreator | ||
| public static CustomOption forValue(String value) { | ||
| return CustomOption.valueOf(value.toUpperCase(Locale.US)); | ||
| } | ||
| } | ||
|
|
||
| public static enum OptionWithoutJsonTypeInfo { | ||
| E, F; | ||
|
|
||
| @JsonCreator | ||
| public static OptionWithoutJsonTypeInfo forValue(String value) { | ||
| return OptionWithoutJsonTypeInfo.valueOf(value.toUpperCase(Locale.US)); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| @Test | ||
| public void testEnum_Native() { | ||
| NativeOption matcher = NativeOption.A; | ||
|
|
||
| ObjectMapper objectMapper = new ObjectMapper(); | ||
|
|
||
| String asString = objectMapper.writeValueAsString(matcher); | ||
| Assertions.assertThat(asString).isEqualTo("\"A\""); | ||
|
|
||
| SomeOption fromString = objectMapper.readValue(asString, SomeOption.class); | ||
| Assertions.assertThat(fromString).isSameAs(matcher); | ||
| } | ||
|
|
||
| @Test | ||
| public void testEnum_Custom() { | ||
| CustomOption matcher = CustomOption.C; | ||
|
|
||
| ObjectMapper objectMapper = new ObjectMapper(); | ||
|
|
||
| String asString = objectMapper.writeValueAsString(matcher); | ||
| Assertions.assertThat(asString).isEqualTo("[\"TestJsonTypeInfoJsonValue$CustomOption\",\"C\"]"); | ||
|
|
||
| SomeOption fromString = objectMapper.readValue(asString, SomeOption.class); | ||
| Assertions.assertThat(fromString).isSameAs(matcher); | ||
| } | ||
|
|
||
| @Test | ||
| public void testEnum_Custom_jsonValue() { | ||
| CustomOption_WithJsonValue matcher = CustomOption_WithJsonValue.C; | ||
|
|
||
| ObjectMapper objectMapper = new ObjectMapper(); | ||
|
|
||
| String asString = objectMapper.writeValueAsString(matcher); | ||
| Assertions.assertThat(asString).isEqualTo("[\"TestJsonTypeInfoJsonValue$CustomOption\",\"C\"]"); | ||
|
|
||
| SomeOption fromString = objectMapper.readValue(asString, SomeOption.class); | ||
| Assertions.assertThat(fromString).isSameAs(matcher); | ||
| } | ||
|
|
||
| // To be removed, just to help debugging a standard scenario | ||
| @Test | ||
| public void testEnum_noJsonTypeInfo() { | ||
| OptionWithoutJsonTypeInfo matcher = OptionWithoutJsonTypeInfo.E; | ||
|
|
||
| ObjectMapper objectMapper = new ObjectMapper(); | ||
|
|
||
| String asString = objectMapper.writeValueAsString(matcher); | ||
| Assertions.assertThat(asString).isEqualTo("\"E\""); | ||
|
|
||
| OptionWithoutJsonTypeInfo fromString = objectMapper.readValue(asString, OptionWithoutJsonTypeInfo.class); | ||
| Assertions.assertThat(fromString).isSameAs(matcher); | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree that sounds odd. It must be due to fundamental problem with
@JsonValuehandling usually changing type of value being serialized (POJO) into intermediate type; so assumption here being former is not "natural", but if latter is, we want to determine type serialization requirement on former.Like
@JsonValueannotated method returningString, for some POJO.