forked from FasterXML/jackson-databind
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolves FasterXML#3853 by adding support for strict handling of type information when deserializing into a registered subtype.
- Loading branch information
1 parent
68ba588
commit a5520bc
Showing
4 changed files
with
121 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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 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 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
94 changes: 94 additions & 0 deletions
94
src/test/java/com/fasterxml/jackson/databind/jsontype/StrictJsonTypeInfoHandlingTest.java
This file contains 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,94 @@ | ||
package com.fasterxml.jackson.databind.jsontype; | ||
|
||
import com.fasterxml.jackson.annotation.JsonTypeInfo; | ||
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id; | ||
import com.fasterxml.jackson.annotation.JsonTypeName; | ||
import com.fasterxml.jackson.databind.BaseMapTest; | ||
import com.fasterxml.jackson.databind.MapperFeature; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.exc.InvalidTypeIdException; | ||
import com.fasterxml.jackson.databind.json.JsonMapper; | ||
|
||
public class StrictJsonTypeInfoHandlingTest extends BaseMapTest { | ||
|
||
@JsonTypeInfo(use = Id.NAME) | ||
interface Command { | ||
} | ||
|
||
@JsonTypeName("do-something") | ||
static class DoSomethingCommand implements Command { | ||
} | ||
|
||
public void testDefaultNonStrictTypeHandling() throws Exception { | ||
ObjectMapper om = new ObjectMapper(); | ||
om.registerSubtypes(DoSomethingCommand.class); | ||
|
||
// This should pass in all scenarios | ||
verifyDeserializationWithFullTypeInfo(om); | ||
// and throw an exception if the target was a super-type in all cases | ||
verifyInvalidTypeIdWithSuperclassTarget(om); | ||
|
||
// Default is to allow the deserialization without a type if the target | ||
// is a concrete sub-type | ||
verifyDeserializationWithConcreteTarget(om); | ||
} | ||
|
||
public void testExplicitNonStrictTypeHandling() throws Exception { | ||
ObjectMapper om = JsonMapper.builder().disable(MapperFeature.STRICT_TYPE_ID_HANDLING).build(); | ||
om.registerSubtypes(DoSomethingCommand.class); | ||
|
||
// This should pass in all scenarios | ||
verifyDeserializationWithFullTypeInfo(om); | ||
// and throw an exception if the target was a super-type in all cases | ||
verifyInvalidTypeIdWithSuperclassTarget(om); | ||
|
||
// Default is to allow the deserialization without a type if the target | ||
// is a concrete sub-type | ||
verifyDeserializationWithConcreteTarget(om); | ||
} | ||
|
||
public void testStrictTypeHandling() throws Exception { | ||
ObjectMapper om = JsonMapper.builder().enable(MapperFeature.STRICT_TYPE_ID_HANDLING).build(); | ||
om.registerSubtypes(DoSomethingCommand.class); | ||
|
||
// This should pass in all scenarios | ||
verifyDeserializationWithFullTypeInfo(om); | ||
// and throw an exception if the target was a super-type in all cases | ||
verifyInvalidTypeIdWithSuperclassTarget(om); | ||
|
||
// With strict mode enabled, fail if there's no type information on the | ||
// JSON | ||
verifyInvalidTypeIdWithConcreteTarget(om); | ||
|
||
} | ||
|
||
private void verifyInvalidTypeIdWithSuperclassTarget(ObjectMapper om) throws Exception { | ||
try { | ||
om.readValue("{}", Command.class); | ||
fail("Should not pass"); | ||
} catch (InvalidTypeIdException e) { | ||
verifyException(e, "missing type id property '@type'"); | ||
} | ||
} | ||
|
||
private void verifyInvalidTypeIdWithConcreteTarget(ObjectMapper om) throws Exception { | ||
try { | ||
om.readValue("{}", DoSomethingCommand.class); | ||
fail("Should not pass"); | ||
} catch (InvalidTypeIdException e) { | ||
verifyException(e, "missing type id property '@type'"); | ||
} | ||
} | ||
|
||
private void verifyDeserializationWithConcreteTarget(ObjectMapper om) throws Exception { | ||
DoSomethingCommand cmd = om.readValue("{}", DoSomethingCommand.class); | ||
assertType(cmd, DoSomethingCommand.class); | ||
} | ||
|
||
private void verifyDeserializationWithFullTypeInfo(ObjectMapper om) throws Exception { | ||
Command cmd = om.readValue("{\"@type\":\"do-something\"}", Command.class); | ||
assertType(cmd, DoSomethingCommand.class); | ||
cmd = om.readValue("{\"@type\":\"do-something\"}", DoSomethingCommand.class); | ||
assertType(cmd, DoSomethingCommand.class); | ||
} | ||
} |