Subclasses missing in generated @JsonSubTypes #16

Closed
opened 2020-05-26 20:06:42 +02:00 by sebplorenz · 2 comments
sebplorenz commented 2020-05-26 20:06:42 +02:00 (Migrated from github.com)

Hi,
I have the following openapi spec

openapi: 3.0.0
info:
  version: "4.0"
  title: Pet Service

paths:
  /animal:
    post:
      requestBody:
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/Animal"
        required: true
      responses:
        200:
          description: OK
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Animal"

components:
  schemas:
    Animal:
      type: object
      properties:
        name:
          type: string
      discriminator:
        propertyName: dtype
    Mammal:
      allOf:
        - $ref: '#/components/schemas/Animal'
    Nonmammal:
      allOf:
        - $ref: '#/components/schemas/Animal'
    Fish:
      properties:
        saltwater:
          type: boolean
      allOf:
        - $ref: '#/components/schemas/Nonmammal'
    Bird:
      properties:
        altitude:
          type: integer
      allOf:
        - $ref: '#/components/schemas/Nonmammal'

The resulting Animal class is only annotated with the directed descendants Mammal and Nonmammal.

@com.fasterxml.jackson.annotation.JsonSubTypes({
	@com.fasterxml.jackson.annotation.JsonSubTypes.Type(value = Mammal.class, name = "Mammal"),
	@com.fasterxml.jackson.annotation.JsonSubTypes.Type(value = Nonmammal.class, name = "Nonmammal"),
})
public abstract class Animal {
...
}

It is missing Bird and Fish. This will cause an error if Bird or Fish are posted to the server.
The correct JsonSubTypes annotation would be:

@com.fasterxml.jackson.annotation.JsonSubTypes({
	@com.fasterxml.jackson.annotation.JsonSubTypes.Type(value = Mammal.class, name = "Mammal"),
	@com.fasterxml.jackson.annotation.JsonSubTypes.Type(value = Nonmammal.class, name = "Nonmammal"),
	@com.fasterxml.jackson.annotation.JsonSubTypes.Type(value = Fish.class, name = "Fish"),
	@com.fasterxml.jackson.annotation.JsonSubTypes.Type(value = Bird.class, name = "Bird")
})
public abstract class Animal {
...
}
Hi, I have the following openapi spec ``` openapi: 3.0.0 info: version: "4.0" title: Pet Service paths: /animal: post: requestBody: content: application/json: schema: $ref: "#/components/schemas/Animal" required: true responses: 200: description: OK content: application/json: schema: $ref: "#/components/schemas/Animal" components: schemas: Animal: type: object properties: name: type: string discriminator: propertyName: dtype Mammal: allOf: - $ref: '#/components/schemas/Animal' Nonmammal: allOf: - $ref: '#/components/schemas/Animal' Fish: properties: saltwater: type: boolean allOf: - $ref: '#/components/schemas/Nonmammal' Bird: properties: altitude: type: integer allOf: - $ref: '#/components/schemas/Nonmammal' ``` The resulting Animal class is only annotated with the directed descendants Mammal and Nonmammal. ``` @com.fasterxml.jackson.annotation.JsonSubTypes({ @com.fasterxml.jackson.annotation.JsonSubTypes.Type(value = Mammal.class, name = "Mammal"), @com.fasterxml.jackson.annotation.JsonSubTypes.Type(value = Nonmammal.class, name = "Nonmammal"), }) public abstract class Animal { ... } ``` It is missing Bird and Fish. This will cause an error if Bird or Fish are posted to the server. The correct JsonSubTypes annotation would be: ``` @com.fasterxml.jackson.annotation.JsonSubTypes({ @com.fasterxml.jackson.annotation.JsonSubTypes.Type(value = Mammal.class, name = "Mammal"), @com.fasterxml.jackson.annotation.JsonSubTypes.Type(value = Nonmammal.class, name = "Nonmammal"), @com.fasterxml.jackson.annotation.JsonSubTypes.Type(value = Fish.class, name = "Fish"), @com.fasterxml.jackson.annotation.JsonSubTypes.Type(value = Bird.class, name = "Bird") }) public abstract class Animal { ... } ```
sschnabe commented 2020-06-29 08:41:38 +02:00 (Migrated from github.com)

I cannot reproduce this, see 5996980.

May the discriminator.mapping is missing in your spec?

I cannot reproduce this, see [5996980](https://github.com/kokuwaio/micronaut-openapi-codegen/commit/5996980b1edc426915dc4d500374e2b061acf93b). May the *discriminator.mapping* is missing in your spec?
sebplorenz commented 2020-07-07 18:30:48 +02:00 (Migrated from github.com)

That was it! The missing discriminator.mapping was the problem. Thank you, Stephan.

That was it! The missing discriminator.mapping was the problem. Thank you, Stephan.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
kokuwaio/micronaut-openapi-codegen#16
No description provided.