-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support strict Type ID handling. (#3854)
Add `MapperFeature.REQUIRE_TYPE_ID_FOR_SUBTYPES` (default: true)
- Loading branch information
1 parent
ac971e9
commit 5e2c643
Showing
7 changed files
with
153 additions
and
11 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
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
...test/java/com/fasterxml/jackson/databind/jsontype/StrictJsonTypeInfoHandling3853Test.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 StrictJsonTypeInfoHandling3853Test extends BaseMapTest { | ||
|
||
@JsonTypeInfo(use = Id.NAME) | ||
interface Command { | ||
} | ||
|
||
@JsonTypeName("do-something") | ||
static class DoSomethingCommand implements Command { | ||
} | ||
|
||
public void testDefaultHasStrictTypeHandling() 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 disallow the deserialization without a type if the target | ||
// is a concrete sub-type | ||
verifyInvalidTypeIdWithConcreteTarget(om); | ||
} | ||
|
||
public void testExplicitNonStrictTypeHandling() throws Exception { | ||
ObjectMapper om = JsonMapper.builder().disable(MapperFeature.REQUIRE_TYPE_ID_FOR_SUBTYPES).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.REQUIRE_TYPE_ID_FOR_SUBTYPES).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); | ||
} | ||
} |
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