Search code examples
swaggerpolymorphismopenapiopenapi-generator

OpenAPI models not being generated when using oneOf


I am using OpenAPI+OpenAPI-generator with spring boot, and trying to use the schema oneof as follows:

This is the configuration in the requests.yaml file:

...

requestBody:
 name: request
 required: true
 content:
   application/json:
     schema:
       oneOf:
         - $ref: 'components.yaml#/Request'
         - $ref: 'components.yaml#/ComplexRequest'

...

and this is the relevant configuration in the components.yaml file:

Request:
  allOf:
    - $ref: '#/BaseInfo'
    - type: object
      properties:
        should_create:
          type: boolean
          enum: [ false ]
        reference_id:
          type: string
        required:
          - reference_id

ComplexRequest:
  allOf:
    - $ref: '#/BaseInfo'
    - type: object
      properties:
        should_create:
          type: boolean
          enum: [ true ]
        create_data:
          $ref: '#/Reference'
      required:
        - create_data

BaseInfo:
  type: object
  properties:
    customer_id:
      type: string

Reference:
  type: object
  properties:
    propery_1:
      type: string
    propery_2:
      type: string
    propery_3:
      type: string

For some reason, all of these components and only these are not being generated. Can someone enlighten me on what am I doing wrong here?


Solution

  • If someone is facing this issue, I hope I'll save you some investigation time; As for March 2022, it seems like oneOf (and anyOf) is just not supported by openapi-generator: https://openapi-generator.tech/docs/roadmap/#short-term

    And though not compatible for my situation, you can try the solutions suggested here: How to use OpenAPI "oneOf" property with openapi-generator-maven-plugin when generating Spring code