1

I have an class hierarchy as below

public class Car {
     private String make; 
     private String model; 
     
     @Schema(example = "MANUAL")
     private TransmissionType transmissionType;

     @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXTERNAL_PROPERTY, 
          property = "transmissionType")
     private Transmission transmission; 
}

@JsonSubTypes({
        @JsonSubTypes.Type(value = AutomaticTransmission.class, name = "AUTOMATIC"),
        @JsonSubTypes.Type(value = ManualTransmission.class, name = "MANUAL"))
})
public abstract class Transmission {
}

public class AutomaticTransmission {
     @Schema(example = "DCT")
     public Technology technology;
}

public class ManualTransmission {
     @Schema(example = "5")
     public int numGears; 
}

Now, when swagger is generated, I see that for car model,

{
   "transmissionType": "MANUAL"
   "transmission": {  
       "technology": "DCT"
   }
}

Here transmission type is manual but example given of automatic. Requirement is give example of manual transmission. How do I link these two properties.

I know that I can create an example json and put it in @Schema(example = "{\"numGears\": 5}") but this will create maintenance overhead of modifying json when class is modified.

Ganesh Satpute
  • 3,664
  • 6
  • 41
  • 78

0 Answers0