-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add failing test for #4047 #4049
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
JooHyukKim
wants to merge
6
commits into
FasterXML:2.16
from
JooHyukKim:4047-ObjectMapper.valueToTree-will-ignore-the-configuration-SerializationFeature.WRAP_ROOT_VALUE-
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cd75cce
Add failing test
JooHyukKim 0bfe09b
Refactor existing
JooHyukKim c84165d
Add test case for `UNWRAP_ROOT_VALUE`
JooHyukKim 86684a5
Update TestRootType4047Test.java
JooHyukKim 41e3ad9
Update TestRootType4047Test.java
JooHyukKim 5a541ef
Merge remote-tracking branch 'upstream/2.16' into 4047-ObjectMapper.v…
JooHyukKim 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
59 changes: 59 additions & 0 deletions
59
src/test/java/com/fasterxml/jackson/failing/TestRootType4047Test.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,59 @@ | ||
package com.fasterxml.jackson.failing; | ||
|
||
import com.fasterxml.jackson.annotation.JsonRootName; | ||
import com.fasterxml.jackson.databind.BaseMapTest; | ||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.SerializationFeature; | ||
|
||
// [databind#4047] : ObjectMapper.valueToTree will ignore the configuration SerializationFeature.WRAP_ROOT_VALUE | ||
public class TestRootType4047Test extends BaseMapTest { | ||
|
||
@JsonRootName("event") | ||
static class Event { | ||
public Long id; | ||
public String name; | ||
} | ||
|
||
/* | ||
/********************************************************** | ||
/* Main tests | ||
/********************************************************** | ||
*/ | ||
|
||
private final String WRAPPED_EVENT_JSON = "{\"event\":{\"id\":1,\"name\":\"foo\"}}"; | ||
private final String UNWRAPPED_EVENT_JSON = "{\"id\":1,\"name\":\"foo\"}"; | ||
|
||
public void testValueToTree() throws Exception { | ||
// Arrange | ||
final ObjectMapper WRAP_ROOT_MAPPER = jsonMapperBuilder() | ||
.enable(SerializationFeature.WRAP_ROOT_VALUE) | ||
.build(); | ||
Event value = new Event(); | ||
value.id = 1L; | ||
value.name = "foo"; | ||
|
||
// (1) works | ||
assertEquals(WRAPPED_EVENT_JSON, | ||
WRAP_ROOT_MAPPER.writeValueAsString(value)); | ||
|
||
// (2) fails w/ {"id":1,"name":"foo"} | ||
assertEquals(WRAPPED_EVENT_JSON, | ||
WRAP_ROOT_MAPPER.valueToTree(value).toString()); | ||
} | ||
|
||
public void testTreeToValue() throws Exception { | ||
// Arrange | ||
final ObjectMapper UNWRAP_MAPPER = jsonMapperBuilder() | ||
.enable(DeserializationFeature.UNWRAP_ROOT_VALUE) | ||
.build(); | ||
|
||
/* | ||
* (1) both fails with | ||
* com.fasterxml.jackson.databind.exc.MismatchedInputException: Root name ('event') does not match expected ('JsonNode') for type `com.fasterxml.jackson.databind.JsonNode` | ||
* at [Source: REDACTED (`StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION` disabled); line: 1, column: 2] (through reference chain: com.fasterxml.jackson.databind.JsonNode["event"]) | ||
*/ | ||
UNWRAP_MAPPER.readTree(WRAPPED_EVENT_JSON); | ||
UNWRAP_MAPPER.readTree(UNWRAPPED_EVENT_JSON); | ||
} | ||
} |
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.
/cc
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.
When
DeserializationFeature.UNWRAP_ROOT_VALUE
is enabled,readTree()
works likeBut, should it?
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 is tricky: my thinking all along has been that no, WRAP/UNWRAP related to POJOs and should not affect
JsonNode
:JsonNode
(tree model in general) being used to represent JSON exactly as is, with no (or minimal) translations: no naming conventions, no ignorals... and hence no wrapping/unwrapping.But then again some users disagree, and maybe more importantly, implementation may be such that wrapping/unwrapping occurs outside control of (de)serializer being used.
I suspect this could actually be one reason for handling of:
to possibly differ.