OpenAPI models not being generated when using oneOf
Asked Answered
P

1

6

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?

Pruchno answered 9/2, 2022 at 9:0 Comment(3)
Possibly this issue: github.com/OpenAPITools/openapi-generator/issues/10880Tonneson
yes, I've also noticed this, I really hoped it's not a bug but something I've missed here. :(Pruchno
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']).Stella
P
4

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

Pruchno answered 2/3, 2022 at 12:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.