Netflix / dgs-codegen

Apache License 2.0
182 stars 99 forks source link

Use concrete types for JsonSubType annotations on interfaces if available #683

Closed srinivasankavitha closed 5 months ago

srinivasankavitha commented 5 months ago

This fixes a bug when interface for schema types are generated along with the generateInterfaces = true for data types. As a result, the interfaces generated for data types are used in the JsonSubType annotations which do not deserialize due to lack of constructor.

            interface Fruit {
              seeds: [Seed]
            }

            type Apple implements Fruit {
              seeds: [Seed]
              truth: Boolean!
            }

            type Seed {
              shape: String
            }

The above schema before the fix would produce 3 interfaces - IFruit, IApple and ISeed when generateInterfaces = true. IFruit has the JsonSubTypes set to IApple and ISeed interface classes instead of Apple and Seed which are the concrete types.

Because of this issue, jackson cannot properly deserialize into the interface type IFruit. After the fix, the annotations are now:

@JsonTypeInfo(
    use = JsonTypeInfo.Id.NAME,
    include = JsonTypeInfo.As.PROPERTY,
    property = "__typename"
)
@JsonSubTypes(@JsonSubTypes.Type(value = Apple.class, name = "Apple"))
public interface Fruit {
  List<? extends ISeed> getSeeds();

  void setSeeds(List<? extends ISeed> seeds);
}

Note the value is set to Apple.class instead of IApple.class