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

ValueInstantiator.canInstantiate() ignores canCreateUsingArrayDelegate() #1804

Closed
henryptung opened this issue Oct 21, 2017 · 2 comments
Closed
Milestone

Comments

@henryptung
Copy link

henryptung commented Oct 21, 2017

Problem

Method Javadoc doesn't match behavior. As a result, delegate collection constructors (for abstract types) don't work properly.

Tested versions

jackson-databind v2.8.7 and v2.9.2.

Location in code

https://github.com/FasterXML/jackson-databind/blob/master/src/main/java/com/fasterxml/jackson/databind/deser/ValueInstantiator.java#L70

Expected Behavior

Outputs [].

Observed Behavior

Exception in thread "main" com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `ArrayDelegateDeserializationTest$MyType` (no Creators, like default construct, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information
 at [Source: (String)"[]"; line: 1, column: 1]
	at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:67)
	at com.fasterxml.jackson.databind.DeserializationContext.reportBadDefinition(DeserializationContext.java:1451)
	at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1027)
	at com.fasterxml.jackson.databind.deser.AbstractDeserializer.deserialize(AbstractDeserializer.java:265)
	at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4001)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2992)
	at ArrayDelegateDeserializationTest.main(ArrayDelegateDeserializationTest.java:35)

Test code

import java.util.List;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.databind.ObjectMapper;

public class ArrayDelegateDeserializationTest {

    public static class MyTypeImpl implements MyType {
        private final List<Integer> values;

        MyTypeImpl(List<Integer> values) {
            this.values = values;
        }

        @Override
        public List<Integer> getValues() {
            return values;
        }
    }

    public interface MyType {
        @JsonValue
        List<Integer> getValues();

        @JsonCreator
        static MyType of(List<Integer> values) {
            return new MyTypeImpl(values);
        }
    }


    public static void main(String[] args) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        MyType thing = mapper.readValue("[]", MyType.class);
        System.out.println(thing.getValues());
    }

}
@henryptung
Copy link
Author

Note: did some debugger hackery to verify that bypassing this check seems to fix the bug, so adding the missing piece of canInstantiate() should do the trick.

@cowtowncoder
Copy link
Member

Thank you for reporting the problem. Sounds like retrofitting done to add array-delegator was missing the call.

@cowtowncoder cowtowncoder added 2.9 and removed 2.9 labels Oct 24, 2017
@cowtowncoder cowtowncoder added this to the 2.8.11 milestone Oct 24, 2017
@cowtowncoder cowtowncoder changed the title ValueInstantiator.canInstantiate() ignores canCreateUsingArrayDelegate() ValueInstantiator.canInstantiate() ignores canCreateUsingArrayDelegate() Oct 24, 2017
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