I am wondering if there is a way to force Swagger and Spring (maybe using annotations?) to "inline" objects when generating openapi docs. An example of how it works right now:

  paths:
    /pets:
      patch:
        requestBody:
          content:
            application/json:
              schema:
                anyOf:
                  - $ref: '#/components/schemas/PetByAge'
                  - $ref: '#/components/schemas/PetByType'
        responses:
          '200':
            description: Updated
  components:
    schemas:
      PetByAge:
        type: object
        properties:
          age:
            type: integer
          nickname:
            type: string
        required:
          - age
  
      PetByType:
        type: object
        properties:
          pet_type:
            type: string
            enum: [Cat, Dog]
          hunts:
            type: boolean
        required:
          - pet_type
  
      Dog:
        type: object
        properties:
          bark:
            type: boolean
          breed:
            type: string
  
      Cat:
        type: object
        properties:
          hunts:
            type: boolean
          age:
            type: integer

An example of how I would like it to work:

    paths:
      /pets:
        patch:
          requestBody:
            content:
              application/json:
                schema:
                  anyOf:
                    - type: object
                      properties:
                        age:
                          type: integer
                        nickname:
                          type: string
                      required:
                        - age
                    - type: object
                      properties:
                        pet_type:
                          type: string
                          enum: [ Cat, Dog ]
                        hunts:
                          type: boolean
                      required:
                        - pet_type
          responses:
            '200':
              description: Updated
    
    
    components:
      schemas:
        PetByAge:
          type: object
          properties:
            age:
              type: integer
            nickname:
              type: string
          required:
            - age
    
        PetByType:
          type: object
          properties:
            pet_type:
              type: string
              enum: [Cat, Dog]
            hunts:
              type: boolean
          required:
            - pet_type
    
        Dog:
          type: object
          properties:
            bark:
              type: boolean
            breed:
              type: string
    
        Cat:
          type: object
          properties:
            hunts:
              type: boolean
            age:
              type: integer

I use @Schema annotation on the class together with anyOf = {PetByAge.class, PetByType.class} Unfortunately, I can't use any JSON string instead of .class.

Does anyone have any idea how this can be achieved?

0

There are 0 best solutions below