FasterXML / jackson-annotations

Core annotations (annotations that only depend on jackson-core) for Jackson data processor
https://github.com/FasterXML/jackson
Apache License 2.0
1.03k stars 330 forks source link

@JsonTypeInfo always required for JsonTypeInfo.Id.NAME even if globally configured #112

Closed franjavi closed 1 year ago

franjavi commented 7 years ago

Using 2.8.5, if we set a global configuration for the type resolver like:

TypeResolverBuilder<?> typeResolverBuilder = new ObjectMapper.DefaultTypeResolverBuilder(ObjectMapper.DefaultTyping.OBJECT_AND_NON_CONCRETE);
typeResolverBuilder.init(JsonTypeInfo.Id.NAME, null);
typeResolverBuilder.inclusion(JsonTypeInfo.As.PROPERTY);
typeResolverBuilder.typeProperty("__class");
mapper.setDefaultTyping(typeResolverBuilder);

The @JsonTypeInfo annotation still required. (WORKS)

@JsonTypeInfo(property = "__class", use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY)
@JsonSubTypes({
        @JsonSubTypes.Type(value=A.class, name="classA"),
        @JsonSubTypes.Type(value=B.class, name="classB")
})
public abstract class Base {
}

(DOES NOT WORK)

@JsonSubTypes({
        @JsonSubTypes.Type(value=A.class, name="classA"),
        @JsonSubTypes.Type(value=B.class, name="classB")
})
public abstract class Base {
}

Is this behavior intended? In my opinion @JsonTypeInfo is redundant here, where can be different type configurations by mistake of the developer.

cowtowncoder commented 7 years ago

I think I'd need a full reproducible test case to see what happens. This issue actually belongs under jackson-databind (annotations package only defines annotations, but none of functionality that uses them), but for now that's ok.

cowtowncoder commented 7 years ago

FWTW from symptoms it would seem like problem was that default typing was simply not applied. It would also seem like you could simplify your code a bit by calling

mapper.enableDefaultTypingAsProperty(DefaultTyping.OBJECT_AND_NON_CONCRETE, "__class");

instead of explicitly configuring ``TypeResolverBuilder.

franjavi commented 7 years ago

I tried again, but that won't work for JsonTypeInfo.Id.NAME. I attach a small test that checks it. testJackson.zip

cowtowncoder commented 7 years ago

@franjavi I did not mean it should fix it, just that it'd be simpler way to do it. I'll see if test case sheds some light on why typing is not applied.