Skip to content
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

FactoryBasedEnumDeserializer does not respect DeserializationFeature.WRAP_EXCEPTIONS #2164

Closed
huangyq23 opened this issue Oct 24, 2018 · 1 comment
Milestone

Comments

@huangyq23
Copy link

huangyq23 commented Oct 24, 2018

We have the following Enum setup with a @JsonCreator on fromString:

public enum TestEnum {
    A,
    B;

    @JsonCreator
    public static TestEnum fromString(String input) {
        // after some check...
        throw new IllegalArgumentException();
    }
}

When DeserializationFeature.WRAP_EXCEPTIONS is disabled

Expected Behavior

The IllegalArgumentException gets propergated.

Actual Behavior

An InvalidDefinitionException wrapping the exception was thrown.

Error:
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `TestModule$TestEnum`, problem: null
 at [Source: (String)"{"key": "value"}"; line: 1, column: 9] (through reference chain: java.util.LinkedHashMap["key"])
	at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:67)
	at com.fasterxml.jackson.databind.DeserializationContext.instantiationException(DeserializationContext.java:1601)
	at com.fasterxml.jackson.databind.DeserializationContext.handleInstantiationProblem(DeserializationContext.java:1072)
	at com.fasterxml.jackson.databind.deser.std.FactoryBasedEnumDeserializer.deserialize(FactoryBasedEnumDeserializer.java:142)
	at com.fasterxml.jackson.databind.deser.std.MapDeserializer._readAndBindStringKeyMap(MapDeserializer.java:527)
	at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:364)
	at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:29)
	at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3023)
	at TestModule.testEnumDeserialization(TestModule.java:34)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.rules.ExpectedException$ExpectedExceptionStatement.evaluate(ExpectedException.java:239)
	at org.junit.rules.RunRules.evaluate(RunRules.java:20)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
Caused by: java.lang.IllegalArgumentException
	at TestModule$TestEnum.fromString(TestModule.java:24)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at com.fasterxml.jackson.databind.introspect.AnnotatedMethod.callOnWith(AnnotatedMethod.java:122)
	at com.fasterxml.jackson.databind.deser.std.FactoryBasedEnumDeserializer.deserialize(FactoryBasedEnumDeserializer.java:134)
	... 30 more
Full Junit Test Case
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.util.Map;

public class TestModule {
    private static final ObjectMapper MAPPER = new ObjectMapper();

    @Rule
    public final ExpectedException expectedException = ExpectedException.none();

    public enum TestEnum {
        A,
        B;

        @JsonCreator
        public static TestEnum fromString(String input) {
            throw new IllegalArgumentException();
        }
    }

    @Test
    public void testEnumDeserialization() throws Exception {
        expectedException.expect(IllegalArgumentException.class);

        MAPPER.disable(DeserializationFeature.WRAP_EXCEPTIONS);

        MAPPER.readValue("{\"key\": \"value\"}", new TypeReference<Map<String, TestEnum>>() {
        });
    }
}
@cowtowncoder cowtowncoder added this to the 2.10.0 milestone Sep 11, 2019
@cowtowncoder cowtowncoder changed the title FactoryBasedEnumDeserializer does not respect DeserializationFeature.WRAP_EXCEPTIONS FactoryBasedEnumDeserializer does not respect DeserializationFeature.WRAP_EXCEPTIONS Sep 11, 2019
cowtowncoder added a commit that referenced this issue Sep 11, 2019
@cowtowncoder
Copy link
Member

Took a while for me to find this issue, but it's now fixed, will be in 2.10.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants