JsonTypeInfo annotations in Codegen 3.0

819 views
Skip to first unread message

Amanda Wang

unread,
Jul 6, 2018, 1:23:11 PM7/6/18
to Swagger
Swagger 3.0 already has much better support for inheritance and polymorphism compared to 2.0, but codegen, at least for generating java client code, seems to have a few bugs (I'm aware that the tool is still under development, just thought I'd point them out). 

The @JsonTypeInfo annotation seems necessary for proper serializing and deserializing when you have multiple subclasses extending a superclass. However, these annotations won't be added unless "--additional-properties jackson=true" is added to the end of the codegen command (java -jar path/to/codegen.jar generate -i source.yaml -l java --additional-properties jackson=true). At least to me, it wasn't immediately obvious that this needed to be added on for the generated java code to have the needed annotations so perhaps it could be noted somewhere?

Once the --additional-properties option was added, the necessary @JsonTypeInfo annotations were put into the superclass model files. However, a openapi spec such as this:

components:
  schemas:
    superClass:
      properties:
        type:
          type: string
      discriminator:
          propertyName: type

results in the following annotation:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "Discriminator{propertyName='type', mapping=null}", visible = true )
 
instead of the expected

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type", visible = true ) 

Amanda Wang

unread,
Jul 6, 2018, 1:52:24 PM7/6/18
to Swagger
Also, perhaps this problem would be resolved along with the problems listed previously but for now, I went with the generated property name despite it not being the intended existing property but rather creating a new property named "Discriminator{propertyName='type', mapping=null}". However, the fact that visible is set to true results in errors when deserializing from json since "Discriminator{propertyName='type', mapping=null}" doesn't match any property in the pojo it is being deserialized to.
Reply all
Reply to author
Forward
0 new messages