-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Fix handling of ObjectId-property in JsonIdentityInfo for uniform deserialization #3868
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
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
56167b0
Fix all
JooHyukKim 1e4544a
Update JsonIdentityInfoIdProperty3838Test.java
JooHyukKim 78c67fd
Add description on change itself
JooHyukKim e8ebfb9
add testcase with JsonTypeInfo and Identity together
JooHyukKim 8f71294
Update BeanDeserializer.java
JooHyukKim 17012c4
Move the fix after native objectIds
JooHyukKim 520fa48
Add more test
JooHyukKim 21a17ec
should revert
JooHyukKim 5cde949
Revert "should revert"
JooHyukKim f87cf0f
Add passing tests that might need to fail
JooHyukKim f13cbcb
Update TestObjectIdDeserialization.java
JooHyukKim e636687
Revert "Merge branch '3838' of https://github.com/JooHyukKim/jackson-…
JooHyukKim 534d1b0
Fix test
JooHyukKim 52394ca
simplify checking empty json
JooHyukKim cc20173
Clean up changes
JooHyukKim 7eea472
Squashed commit of the following:
JooHyukKim 6b71e01
Revert "Squashed commit of the following:"
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
197 changes: 197 additions & 0 deletions
197
src/test/java/com/fasterxml/jackson/databind/deser/JsonIdentityInfoIdProperty3838Test.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,197 @@ | ||
package com.fasterxml.jackson.databind.deser; | ||
cowtowncoder marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import com.fasterxml.jackson.annotation.*; | ||
import com.fasterxml.jackson.databind.BaseMapTest; | ||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.exc.MismatchedInputException; | ||
|
||
// [databind#3838]: Difference in the handling of ObjectId-property in JsonIdentityInfo depending on the deserialization route. | ||
public class JsonIdentityInfoIdProperty3838Test extends BaseMapTest { | ||
|
||
/* | ||
/********************************************************** | ||
/* Set Up | ||
/********************************************************** | ||
*/ | ||
interface ResultGetter { | ||
String result(); | ||
} | ||
|
||
@JsonIdentityInfo(property = "id", generator = ObjectIdGenerators.PropertyGenerator.class) | ||
static class SetterBased implements ResultGetter { | ||
private String id; | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public void setId(String id) { | ||
this.id = id; | ||
} | ||
|
||
@Override | ||
public String result() { | ||
return id; | ||
} | ||
} | ||
|
||
@JsonIdentityInfo(property = "id", generator = ObjectIdGenerators.PropertyGenerator.class) | ||
static class CreatorBased implements ResultGetter { | ||
private final String id; | ||
|
||
@JsonCreator | ||
CreatorBased(@JsonProperty(value = "id") String id) { | ||
this.id = id; | ||
} | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
@Override | ||
public String result() { | ||
return id; | ||
} | ||
} | ||
|
||
@JsonIdentityInfo(property = "id", generator = ObjectIdGenerators.PropertyGenerator.class) | ||
static class DefaultConstructorBased implements ResultGetter { | ||
public String id; | ||
|
||
@Override | ||
public String result() { | ||
return id; | ||
} | ||
} | ||
|
||
@JsonIdentityInfo(property = "id", generator = ObjectIdGenerators.PropertyGenerator.class) | ||
static class StaticFactoryMethodBased implements ResultGetter { | ||
private final String id; | ||
|
||
private StaticFactoryMethodBased(String id) { | ||
this.id = id; | ||
} | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
@JsonCreator | ||
public static StaticFactoryMethodBased create(@JsonProperty("id") String id) { | ||
return new StaticFactoryMethodBased(id); | ||
} | ||
|
||
@Override | ||
public String result() { | ||
return id; | ||
} | ||
} | ||
|
||
@JsonIdentityInfo(property = "id", generator = ObjectIdGenerators.PropertyGenerator.class) | ||
static class MultiArgConstructorBased implements ResultGetter { | ||
private final String id; | ||
private final int value; | ||
|
||
@JsonCreator | ||
MultiArgConstructorBased(@JsonProperty("id") String id, @JsonProperty("value") int value) { | ||
this.id = id; | ||
this.value = value; | ||
} | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public int getValue() { | ||
return value; | ||
} | ||
|
||
@Override | ||
public String result() { | ||
return id; | ||
} | ||
} | ||
|
||
@JsonIdentityInfo( | ||
generator = ObjectIdGenerators.PropertyGenerator.class, | ||
property = "id" | ||
) | ||
@JsonTypeInfo( | ||
use = JsonTypeInfo.Id.NAME, | ||
include = JsonTypeInfo.As.WRAPPER_OBJECT, | ||
property = "type" | ||
) | ||
@JsonSubTypes({ | ||
@JsonSubTypes.Type(value = Concrete3838.class, name = "concrete_3838") | ||
}) | ||
static class BaseType3838 implements ResultGetter { | ||
public String id; | ||
|
||
@Override | ||
public String result() { | ||
return id; | ||
} | ||
} | ||
|
||
@JsonTypeName("concrete_3838") | ||
static class Concrete3838 extends BaseType3838 { | ||
public String location; | ||
|
||
protected Concrete3838() {} | ||
|
||
public Concrete3838(String id, String loc) { | ||
this.id = id; | ||
location = loc; | ||
} | ||
} | ||
|
||
@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "id") | ||
static class IntSequencedBean implements ResultGetter{ | ||
public String value; | ||
|
||
@Override | ||
public String result() { | ||
return value; | ||
} | ||
} | ||
|
||
/* | ||
/********************************************************** | ||
/* Test | ||
/********************************************************** | ||
*/ | ||
|
||
private final ObjectMapper MAPPER = newJsonMapper(); | ||
|
||
final static Object[][] CLASS_AND_JSON_STRING = new Object[][]{ | ||
{SetterBased.class, "{'id':'great'}"}, | ||
{CreatorBased.class, "{'id':'great'}"}, | ||
{DefaultConstructorBased.class, "{'id':'great'}"}, | ||
{StaticFactoryMethodBased.class, "{'id':'great'}"}, | ||
{MultiArgConstructorBased.class, "{'id':'great','value':42}"}, | ||
{BaseType3838.class, "{'concrete_3838':{'id':'great','location':'Bangkok'}}"}, | ||
{IntSequencedBean.class, "{'id':-1,'value':'great'}"} | ||
}; | ||
|
||
public void testUniformHandlingForMissingObjectId() throws Exception { | ||
for (Object[] classAndJsonStrEntry : CLASS_AND_JSON_STRING) { | ||
final Class<?> cls = (Class<?>) classAndJsonStrEntry[0]; | ||
final String jsonStr = (String) classAndJsonStrEntry[1]; | ||
|
||
// 1. throws MismatchedInputException with empty JSON object | ||
try { | ||
MAPPER.readValue("{}", cls); | ||
fail("should not pass"); | ||
} catch (MismatchedInputException e) { | ||
verifyException(e, | ||
"No Object Id found for an instance of", "to", | ||
"to assign to property 'id'"); | ||
} | ||
|
||
// 2. but works with non-empty JSON object | ||
ResultGetter nonEmptyObj = (ResultGetter) MAPPER.readValue(a2q(jsonStr), cls); | ||
assertEquals("great", nonEmptyObj.result()); | ||
} | ||
} | ||
} |
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.