Skip to content

Serialization with Polymorphisme and EXTERNAL_PROPERTY = duplicate property #802

@Adrien-dev25

Description

@Adrien-dev25

With this kind of POJO :

    public class Cage
    {
        public String id;
        public String type;

        @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type",
                include = JsonTypeInfo.As.EXTERNAL_PROPERTY)
        @JsonSubTypes({
                @JsonSubTypes.Type(value = Cat.class, name = "CAT"),
                @JsonSubTypes.Type(value = Dog.class, name = "DOG")
        })
        public Animal animal;
    }

    public abstract class Animal { }

    public class Cat extends Animal {
        public String firstName = "My name is cat";
    }

    public class Dog extends Animal {
        public String lastName = "My name is dog";
    }

When I serialize this :

Cage cage = new Cage();
cage.id = "123";
cage.type = "CAT";
cage.animal = new Cat();
String xml1 = MAPPER.writeValueAsString(cage);
System.out.println(xml1);

I get the following result (the property type is duplicated) :

<Cage>
  <animal>
    <firstName>My name is cat</firstName>
  </animal>
  <type>CAT</type>
  <id>123</id>
  <type>CAT</type>
</Cage>

Wouldn't be the expected result ?

<Cage>
  <animal>
    <firstName>My name is cat</firstName>
  </animal>
  <id>123</id>
  <type>CAT</type>
</Cage>

Metadata

Metadata

Assignees

Labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions