Skip to content

JsonTypeId-only class is not serialized properly #2156

Description

@alexzam

I have this code:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonSubTypes({
        @JsonSubTypes.Type(value = A.class, name="suppressed")
})
interface I{}

class A implements I {
    @JsonTypeId
    public String getType(){
        return "goodA";
    }
}

After serialization of new A() in field of type I I expect to get {type: "goodA"}, but instead I get {type: "suppressed"}.
But if I add any other property to A (like public int getFake() {return 0;}) then serialization works right (and I get {type: "goodA", fake: 0}).

That's because BeanSerializerBuilder.build() exits if no "real" properties present ignoring _typeId:

    if (_properties == null || _properties.isEmpty()) {
            if (_anyGetter == null && _objectIdWriter == null) {
                return null; 
...

Found in 2.9.5 but code is not changed up to 2.9.7

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions