Skip to content

Disabling FAIL_ON_INVALID_SUBTYPE breaks polymorphic deserialization of Enums #2775

@holgerknoche

Description

@holgerknoche

Apparently, disabling the FAIL_ON_INVALID_SUBTYPE feature has some (at least for me) unexpected side-effects on the deserialization of enums. I originally found this issue in jackson-databind 2.10.0, but it still exists in 2.11.0.
I have an enum that implements an interface:

@JsonTypeInfo(use = Id.NAME)
public interface CustomEnum {}

@JsonTypeName("Test")
public enum TestEnum implements CustomEnum {
    VALUE;
}

Then, this test case works fine:

ObjectMapper mapper = new ObjectMapper();
mapper.registerSubtypes(TestEnum.class);
	
mapper.enable(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE);
	
CustomEnum testValue = TestEnum.VALUE;
String json = mapper.writeValueAsString(testValue);
	
CustomEnum deserializedValue = mapper.readValue(json, CustomEnum.class);
assertEquals(testValue, deserializedValue);

However, when I disable the feature, the test case fails because deserializedValue is suddenly null. The reason seems to be that a NullyfingDeserializer is returned from TypeDeserializerBase._findDefaultImplDeserializer(...) instead of null. Therefore, the AsPropertyDeserializer no longer tries to read the value itself.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions