-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Investigate @JsonTypeInfo with @JsonValue #5366
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
Draft
blacelle
wants to merge
1
commit into
FasterXML:2.x
Choose a base branch
from
solven-eu:bug/JsonSubTypeWithJsonValue
base: 2.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.
Draft
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
61 changes: 61 additions & 0 deletions
61
...com/fasterxml/jackson/databind/ser/benoit/TestIntConstructor_Enum_WeirdSerialization.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,61 @@ | ||
| package com.fasterxml.jackson.databind.ser.benoit; | ||
|
|
||
| import java.util.Locale; | ||
|
|
||
| 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.core.JsonProcessingException; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
|
||
| public class TestIntConstructor_Enum_WeirdSerialization { | ||
|
|
||
| @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; | ||
|
|
||
| @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 NativeOption forValue(String value) { | ||
| return NativeOption.valueOf(value.toUpperCase(Locale.US)); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| @Test | ||
| public void testNative_string() throws JsonProcessingException { | ||
| NativeOption matcher = NativeOption.A; | ||
|
|
||
| ObjectMapper objectMapper = new ObjectMapper(); | ||
|
|
||
| String asString = objectMapper.writeValueAsString(matcher); | ||
| Assertions.assertThat(asString).isEqualTo("A"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCustom_string() throws JsonProcessingException { | ||
| CustomOption matcher = CustomOption.C; | ||
|
|
||
| ObjectMapper objectMapper = new ObjectMapper(); | ||
|
|
||
| String asString = objectMapper.writeValueAsString(matcher); | ||
| Assertions.assertThat(asString).isEqualTo("type: CustomOption, value: A"); | ||
| } | ||
| } |
124 changes: 124 additions & 0 deletions
124
...java/com/fasterxml/jackson/databind/ser/benoit/TestIntConstructor_WeirdSerialization.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,124 @@ | ||
| package com.fasterxml.jackson.databind.ser.benoit; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| import org.assertj.core.api.Assertions; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonSubTypes; | ||
| import com.fasterxml.jackson.annotation.JsonTypeInfo; | ||
| import com.fasterxml.jackson.annotation.JsonValue; | ||
| import com.fasterxml.jackson.core.JsonProcessingException; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
|
||
| // https://github.com/FasterXML/jackson-databind/issues/5035 | ||
| public class TestIntConstructor_WeirdSerialization { | ||
|
|
||
| @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, | ||
| include = JsonTypeInfo.As.PROPERTY, | ||
| property = "type", | ||
| defaultImpl = AroundString.class) | ||
| @JsonSubTypes({ @JsonSubTypes.Type(value = AroundString.class, name = "string"), | ||
| @JsonSubTypes.Type(value = AroundObject.class, name = "object") }) | ||
| public interface AroundSomething { | ||
| Object getInner(); | ||
| } | ||
|
|
||
| public static class AroundString implements AroundSomething { | ||
| @JsonValue | ||
| String inner; // <-- Turning this to Object will make the test passes | ||
|
|
||
| @Override | ||
| public Object getInner() { | ||
| return inner; | ||
| } | ||
|
|
||
| public void setInner(String inner) { | ||
| this.inner = inner; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| public static class AroundObject implements AroundSomething { | ||
| @JsonValue | ||
| Object inner; // <-- Turning this to Object will make the test passes | ||
|
|
||
| @Override | ||
| public Object getInner() { | ||
| return inner; | ||
| } | ||
|
|
||
| public void setInner(String inner) { | ||
| this.inner = inner; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| public static class HasFromObject { | ||
| AroundSomething c; | ||
|
|
||
| public AroundSomething getC() { | ||
| return c; | ||
| } | ||
|
|
||
| public void setC(AroundSomething c) { | ||
| this.c = c; | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void test_aroundString_convertValue() throws JsonProcessingException { | ||
| AroundString matcher = new AroundString(); | ||
| matcher.setInner("foo"); | ||
|
|
||
| HasFromObject wrapper = new HasFromObject(); | ||
| wrapper.setC(matcher); | ||
|
|
||
| ObjectMapper objectMapper = new ObjectMapper(); | ||
|
|
||
| Map asMap = objectMapper.convertValue(wrapper, Map.class); | ||
| Assertions.assertThat(asMap.toString()).isEqualTo("{c=foo}"); | ||
| } | ||
|
|
||
| @Test | ||
| public void test_aroundString_writeValueAsString() throws JsonProcessingException { | ||
| AroundString matcher = new AroundString(); | ||
| matcher.setInner("foo"); | ||
|
|
||
| HasFromObject wrapper = new HasFromObject(); | ||
| wrapper.setC(matcher); | ||
|
|
||
| ObjectMapper objectMapper = new ObjectMapper(); | ||
|
|
||
| String asString = objectMapper.writeValueAsString(wrapper); | ||
| Assertions.assertThat(asString).isEqualTo("{\"c\":\"foo\"}"); // FAILs with `"{"c":["from","foo"]}"` | ||
| } | ||
|
|
||
| @Test | ||
| public void test_aroundObject_convertValue() throws JsonProcessingException { | ||
| AroundObject matcher = new AroundObject(); | ||
| matcher.setInner("foo"); | ||
|
|
||
| HasFromObject wrapper = new HasFromObject(); | ||
| wrapper.setC(matcher); | ||
|
|
||
| ObjectMapper objectMapper = new ObjectMapper(); | ||
|
|
||
| Map asMap = objectMapper.convertValue(wrapper, Map.class); | ||
| Assertions.assertThat(asMap.toString()).isEqualTo("{c=foo}"); | ||
| } | ||
|
|
||
| @Test | ||
| public void test_aroundObject_writeValueAsString() throws JsonProcessingException { | ||
| AroundObject matcher = new AroundObject(); | ||
| matcher.setInner("foo"); | ||
|
|
||
| HasFromObject wrapper = new HasFromObject(); | ||
| wrapper.setC(matcher); | ||
|
|
||
| ObjectMapper objectMapper = new ObjectMapper(); | ||
|
|
||
| String asString = objectMapper.writeValueAsString(wrapper); | ||
| Assertions.assertThat(asString).isEqualTo("{\"c\":\"foo\"}"); // FAILs with `"{"c":["from","foo"]}"` | ||
| } | ||
| } | ||
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.
This already declared object?