3

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?

Tom Carmi
  • 385
  • 1
  • 5
  • 18
  • 1
    Possibly this issue: https://github.com/OpenAPITools/openapi-generator/issues/10880 – Helen Feb 09 '22 at 11:17
  • yes, I've also noticed [this](https://github.com/OpenAPITools/openapi-generator/issues/5903), I really hoped it's not a bug but something I've missed here. :( – Tom Carmi Feb 09 '22 at 11:36
  • I see that it should be theoretically possible to discriminate a Request from a ComplexRequest at deserialization because of your differing required fields. I have oneOf generation working for java using openapi-generator 5.4.0 but I do add a discriminator field (like requestType: type: string enum: ['COMPLEX']). – Dave Moten Mar 24 '22 at 22:43

1 Answers1

2

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

Tom Carmi
  • 385
  • 1
  • 5
  • 18