0

I use openapi-generator-cli to generate a java client from an openapi 3.0.1 yaml file. If you have a request with multiple responses, the generator creates a java class for each response, but actually does not use those java classes anywhere. So it creates lots of unused files and lots of unused imports. For example check out this yaml file:

openapi: 3.0.1
info:
  title: example-api
  description: 'Example API'
  version: 1.0.0
servers:
  - url: /example/api/v1
paths:
  /createRequest:
    get:
      summary: Creates a request
      responses:
        '200':
          description: OK
          content:
            '*/*':
              schema:
                type: object
                properties:
                  status:
                    type: string
                    example: OK
                    description: Status of request
        '400':
          description: Error 400
          content:
            '*/*':
              schema:
                type: object
                properties:
                  status:
                    type: string
                    example: Error 400
        '401':
          description: Error 401
          content:
            '*/*':
              schema:
                type: object
                properties:
                  status:
                    type: string
                    example: Error 401
        '402':
          description: Error 402
          content:
            '*/*':
              schema:
                type: object
                properties:
                  status:
                    type: string
                    example: Error 402
        '490':
          description: Error 490
          content:
            '*/*':
              schema:
                type: object
                properties:
                  status:
                    type: string
                    example: Error 490
        '499':
          description: Error 499
          content:
            '*/*':
              schema:
                type: object
                properties:
                  status:
                    type: string
                    example: Error 499

The generator creates files like CreateRequestGet400Response, CreateRequestGet401Response or CreateRequestGet402Response and so on without using them anywhere. Is there any option I can use to disable generating these files?

1 Answer 1

1

Since your 200 and 4xx responses have exactly the same structure (an object with the status property), you can create a common Response schema and $ref it. This way you should end up with just a single Response class instead of multiple classes generated from unnamed inline schemas.

      responses:
        '200':
          description: OK
          content:
            '*/*':
              schema:
                $ref: '#/components/schemas/Response'  # <-----
              example:
                status: OK
        '400':
          description: Error 400
          content:
            '*/*':
              schema:
                $ref: '#/components/schemas/Response'  # <-----
              example:
                status: Error 400
        ...

components:
  schemas:
    Response:
      type: object
      properties:
        status:
          type: string
          example: OK
          description: Request status
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.