jackson-databind icon indicating copy to clipboard operation
jackson-databind copied to clipboard

JsonTypeInfo.As.EXTERNAL_PROPERTY does not work with a Delegate

Open alexwen opened this issue 11 years ago • 6 comments

I am using a library which generates the Jackson deserialization code as a delegate. I have created a class here which simulates this format:

public class HeroBattle {

    private final Hero hero;

    private HeroBattle(Hero hero) {
        this.hero = requireNonNull(hero);
    }

    @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.EXTERNAL_PROPERTY, property = "heroType")
    public Hero getHero() {
        return hero;
    }

    @JsonCreator
    static HeroBattle fromJson(Delegate json) {
        return new HeroBattle(json.hero);
    }

    public class Delegate {
        @JsonProperty
        @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.EXTERNAL_PROPERTY, property = "heroType")
        Hero hero;
    }

    public static void main(String[] args) throws IOException {
        ObjectMapper mapper = new ObjectMapper();

        final String json = mapper.writeValueAsString(new HeroBattle(new Superman()));
        final HeroBattle battle = mapper.readValue(json, HeroBattle.class);

        assert battle.getHero() instanceof Superman;
    }

    public interface Hero {
    }

    public static class Superman implements Hero {
        public String getName() {
            return "superman";
        }
    }
}

This results in the following exception:

Exception in thread "main" java.lang.IllegalStateException: No default constructor for [simple type, class HeroBattle]
    at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createUsingDefault(StdValueInstantiator.java:211)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeWithExternalTypeId(BeanDeserializer.java:652)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:262)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:124)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3066)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2161)
    at HeroBattle.main(HeroBattle.java:43)

alexwen avatar Nov 12 '15 17:11 alexwen

Ok that may be a bug. One thing you may want to try is to add mode property for @JsonCreator, with value Mode.DELEGATING. It should not be necessary, but with possibility of confusion between single-property creator and delegating one that could be one reason for failure.

I assume this is (or can be reproduced) with Jackson 2.6.3?

cowtowncoder avatar Nov 12 '15 17:11 cowtowncoder

Yes, sorry should have said the version. I originally found the issue in 2.4.6 but verified it in 2.6.3 as well.

Specifying Mode.Delegating does not change the exception that is thrown.

alexwen avatar Nov 12 '15 18:11 alexwen

I can reproduce the issue locally; added a failing unit test. No idea yet as to what causes this.

cowtowncoder avatar Nov 25 '15 04:11 cowtowncoder

Actually looks like this specific combination of "exotic" features is just not yet supported, unlike, say, property-based creator and external type id. Will try to see if it'd be easy enough to add support.

cowtowncoder avatar Nov 25 '15 04:11 cowtowncoder

Took a while to figure this out, but I think I solved it the way it should be solved; test had one minor flaw (no setter for Superman), but after fixing that also test passes.

cowtowncoder avatar Nov 25 '15 05:11 cowtowncoder

Awesome! Thanks for the fix!

alexwen avatar Nov 25 '15 17:11 alexwen