Q:

Jackson : Inheritance and required attributes

I'm currently trying to implement a deserializer using jackson able to handle polymorphism, that is, given these two classes : 
public abstract class Animal {
  private String name;
  private float weight;

  @JsonCreator
  protected Animal(@JsonProperty(value="name") String name, @JsonProperty(value="weight",required=true) int weight) {
      this.name=name;
      this.weight=weight;
  }
}

public class Dog extends Animal {
    private int barkVolume;

    @JsonCreator
    public Dog(String name,int weight, @JsonProperty(value="barkVolume",required=true) int barkVolume) {
        super(name, weight);
        this.barkVolume=barkVolume;
    }

}

The deserializer should be able to infer and instantiate the proper subclass from a json string.
I use a custom deserializer module, the UniquePropertyPolymorphicDeserializer (From https://gist.github.com/robinhowlett/ce45e575197060b8392d). This module is configured as follows : 
UniquePropertyPolymorphicDeserializer<Animal> deserializer =
             new UniquePropertyPolymorphicDeserializer<Animal>(Animal.class);

        deserializer.register("barkVolume", Dog.class);

        SimpleModule module = new SimpleModule("UniquePropertyPolymorphicDeserializer");
        module.addDeserializer(Animal.class, deserializer);
        mapper.registerModule(module);

This module asks the user the unique properties of each subclasses of Animal. Thus, when the deserializer finds a json string with a barkVolume property, it knows that a Dog should be instantiated. 
However, I have an issue with the specification of the json properties, as subclasses can't inherit from the properties given in the parent class. In the class Dog, I have to specify again that "name" and "weight" are json properties, even if these properties are already specified in the Animal class : 
public Dog(@JsonProperty(value="name") String name, @JsonProperty(value="weight",required=true) int weight, @JsonProperty(value="barkVolume",required=true) int barkVolume) {
        super(name, weight);
        this.barkVolume=barkVolume;
    }

Otherwise, the deserializer generates an error : 
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Invalid type definition for type `Animals.Dog`: Argument #0 has no property name, is not Injectable: can not use as Creator [constructor for Animals.Dog, annotations: {interface com.fasterxml.jackson.annotation.JsonCreator=@com.fasterxml.jackson.annotation.JsonCreator(mode=DEFAULT)}]
 at [Source: UNKNOWN; line: -1, column: -1]

This solution is not satisfying for me : 

Each time we want to create a new subclass of Animal we have to
specify in this class that name and weight are json properties
It is tricky, as, for example, in the Animal class, the weight property is marked as required while in the subclasses, we can define that weight is not a required property.

Do you know a way to "inherit" from the properties of the parent class, so as to avoid specifying each time in the subclasses the corresponding json properties ?
Best Regards,
Mathieu

A:

I finally decided to create my own Deserializer (instead of the UniquePropertyDeserializer), in which I use introspection to get the fields of the parent class. It permits to avoid specifying again in the child classes the required json properties.

