-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Create Issue4752Test.java #4765
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
Closed
Closed
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
125 changes: 125 additions & 0 deletions
125
src/test/java/com/fasterxml/jackson/databind/ser/Issue4752Test.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,125 @@ | ||
package com.fasterxml.jackson.databind.ser; | ||
|
||
import com.fasterxml.jackson.annotation.JsonAnyGetter; | ||
import com.fasterxml.jackson.annotation.JsonAnySetter; | ||
import com.fasterxml.jackson.annotation.JsonAutoDetect; | ||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class Issue4752Test extends DatabindTestUtil { | ||
|
||
private final ObjectMapper objectMapper = newJsonMapper(); | ||
|
||
@JsonAutoDetect( | ||
fieldVisibility = JsonAutoDetect.Visibility.ANY, | ||
getterVisibility = JsonAutoDetect.Visibility.NONE | ||
) | ||
static class FirstObject { | ||
|
||
@JsonAnySetter | ||
@JsonAnyGetter | ||
private Map<String, Object> data = new LinkedHashMap<>(); | ||
|
||
public String getTransactionId() { | ||
return (String) data.get("transactionId"); | ||
} | ||
} | ||
|
||
@JsonAutoDetect( | ||
fieldVisibility = JsonAutoDetect.Visibility.ANY, | ||
getterVisibility = JsonAutoDetect.Visibility.NONE | ||
) | ||
static class SecondObject { | ||
|
||
private final String transactionId; | ||
@JsonAnySetter | ||
@JsonAnyGetter | ||
private final Map<String, Object> data; | ||
|
||
|
||
@JsonCreator | ||
public SecondObject(@JsonProperty("transactionId") String transactionId) { | ||
this.transactionId = transactionId; | ||
this.data = new LinkedHashMap<>(); | ||
} | ||
|
||
public String getTransactionId() { | ||
return this.transactionId; | ||
} | ||
|
||
public Map<String, Object> getData() { | ||
return this.data; | ||
} | ||
} | ||
|
||
@JsonAutoDetect( | ||
fieldVisibility = JsonAutoDetect.Visibility.ANY, | ||
getterVisibility = JsonAutoDetect.Visibility.NONE | ||
) | ||
static class ThirdObject { | ||
|
||
private final String transactionId; | ||
@JsonAnySetter | ||
@JsonAnyGetter | ||
private final Map<String, Object> data; | ||
|
||
|
||
@JsonCreator | ||
public ThirdObject( | ||
@JsonProperty("transactionId") String transactionId, | ||
@JsonProperty("data") Map<String, Object> data | ||
) { | ||
this.transactionId = transactionId; | ||
this.data = data; | ||
} | ||
|
||
public String getTransactionId() { | ||
return this.transactionId; | ||
} | ||
|
||
public Map<String, Object> getData() { | ||
return this.data; | ||
} | ||
} | ||
|
||
private final String INPUT_JSON = a2q("{'b': 2,'a': 1,'transactionId': 'test'," + | ||
"'c': [{'id': '3','value': 'c'},{'id': '1','value': 'a'},{'id': '2','value': 'b'}]}"); | ||
|
||
private final String SECOND_UNEXPECTED_JSON_OUTPUT = a2q("{'transactionId': 'test'," + | ||
"'c': [{'id': '3','value': 'c'},{'id': '1','value': 'a'},{'id': '2','value': 'b'}]}"); | ||
|
||
private final String THIRD_UNEXPECTED_JSON_OUTPUT = a2q("{'transactionId': 'test'}"); | ||
|
||
private <T> void testSerializationDeserialization(String outputResult, Class<T> clazz) throws Exception { | ||
T deserializedObject = objectMapper.readValue(INPUT_JSON, clazz); | ||
String serializedJson = objectMapper.writeValueAsString(deserializedObject); | ||
|
||
String expectedJson = objectMapper.readTree(outputResult).toPrettyString(); | ||
String actualJson = objectMapper.readTree(serializedJson).toPrettyString(); | ||
|
||
assertEquals(expectedJson, actualJson); | ||
} | ||
|
||
@Test | ||
public void testSerializationAndDeserializationForFirstObject() throws Exception { | ||
testSerializationDeserialization(INPUT_JSON, FirstObject.class); | ||
} | ||
|
||
@Test | ||
public void testSerializationAndDeserializationForSecondObject() throws Exception { | ||
testSerializationDeserialization(SECOND_UNEXPECTED_JSON_OUTPUT, SecondObject.class); | ||
} | ||
|
||
@Test | ||
public void testSerializationAndDeserializationForThirdObject() throws Exception { | ||
testSerializationDeserialization(THIRD_UNEXPECTED_JSON_OUTPUT, ThirdObject.class); | ||
} | ||
} |
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.
Would adding
@JsonAnySetter
in here make any difference?In fact,
@JsonAnySetter
SHOULD NOT be added to field in this case as the intent is to propagate any properties via constructor arguments.And
@JsonAnyGetter
should be added ongetData()
similarly, not in field.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.
Do you mean put
@JsonAnySetter
near to@JsonProperty("data")
?Sounds reasonable.
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.
Yes, exactly. FWTW ideally usage as shown should also work, but trying to think of possible work arounds.
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.
I created class:
When running the test for jackson-databind 2.18.1-20241024.043825-21 I got:
Expected:
Actual:
For 2.17.2 I got: