openapi-generator

Models are not generated when specific apis are selected through the gradle plugin


I am trying to generate java code from the following openapi example spec

openapi: "3.0.3"
info:
  title: Demo API
  version: "1.0"
servers:
  - url: http://localhost:8080/api
    description: Local development server
tags:
  - name: Common
    description: Operations related to common functionalities. Define multiple tags to generate multiple Api classes.
  - name: Other
    description: To test generation of separate APIs
paths:
  /UM/{id}:
    get:
      tags:
        - Common
      description: Retrieve UM by ID
      operationId: GetUM
      parameters:
        - in: path
          name: id
          schema:
            type: string
          required: true
      responses:
        "200":
          description: UM
          content:
            application/json:
              schema: { }
        "404":
          description: UM not found
          content:
            text/plain:
              schema:
                type: string
  /UM:
    post:
      tags:
        - Common
      description: Create new UM
      operationId: CreateUM
      requestBody:
        required: true
        content:
          application/json:
            schema: { }
      responses:
        201:
          description: ID of the newly created UM
          content:
            text/plain:
              schema:
                type: string

  /palettes/{id}:
    get:
      tags:
        - Common
      operationId: getPalette # necessary to generate proper method name
      description: Retrieve a palette by its id
      parameters:
        - in: path
          name: id
          schema:
            type: integer
          required: true
      responses:
        "200":
          description: OK
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Palette"
              examples:
                Default:
                  value:
                    id: 1
                    firstName: John
                    lastName: Doe
                    role: user
        "404":
          description: Palette not found
          content:
            text/plain:
              schema:
                type: string
  /palettes:
    get:
      tags:
        - Common
      operationId: listPalettes
      description: Returns the list of palettes
      responses:
        "200":
          description: OK
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: "#/components/schemas/Palette"

    put:
      tags:
        - Common
      operationId: createPalette # necessary to generate proper method name
      description: Creates a new palette
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/NewPalette"
      responses:
        "201":
          description: Created
          content:
            text/plain:
              schema:
                type: integer
        "400":
          description: Validation errors
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ValidationError"
  /orders:
    put:
      tags:
        - Common
      operationId: sendOrder
      description: Sends a dummy order
      responses:
        "200":
          description: OK
          content:
            text/plain:
              schema:
                type: string
  /otherresources:
    get:
      tags:
        - Other
      operationId: getOther # necessary to generate proper method name
      description: Example operation
      responses:
        "200":
          description: OK
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Palette"

components:
  schemas:
    ObjectType:
      type: string
      enum: [ ENGINE, SEAT ]
    Palette:
      type: object
      x-tags:
        - Common
      properties:
        id:
          type: integer
          description: The user ID
        firstName:
          type: string
          description: The user's first name
        lastName:
          type: string
          description: The palette's last name
          minLength: 1
          maxLength: 20
        role:
          $ref: "#/components/schemas/ObjectType"

    NewPalette:
      type: object
      properties:
        firstName:
          type: string
          description: The user's first name
        lastName:
          type: string
          description: The user's last name
          minLength: 1
          maxLength: 20
        role:
          $ref: "#/components/schemas/ObjectType"

    ValidationErrorField:
      properties:
        field:
          type: string
        message:
          type: string
        constraint:
          type: string
        value:
          type: string
      example:
        - field: name
          message: size must be between 1 and 20
          constraint: Size
          value: This is way toooooooo long a name!

    ValidationError:
      properties:
        status:
          type: string
        message:
          type: string
        errors:
          type: array
          $ref: "#/components/schemas/ValidationErrorField"
      example:
        - status: Bad Request
          message: Validation failed
          errors:
            - field: lastName
              message: size must be between 1 and 20
              constraint: Size
              value: This is wayyyyyy toooo loooooong

  securitySchemes:
    oidc:
      type: oauth2
      flows:
        authorizationCode:
          authorizationUrl: http://localhost:8180/realms/quarkus/protocol/openid-connect/auth
          tokenUrl: http://localhost:8180/realms/quarkus/protocol/openid-connect/token
          refreshUrl: http://localhost:8180/realms/quarkus/protocol/openid-connect/token
          scopes:
            openid: OpenID Connect authentication
security:
  - oidc: [ ]

I am building this with the following gradle task:

openApiGenerate {
    generatorName = 'jaxrs-spec'
    inputSpec = file(openapiSourcefile).absolutePath
    outputDir = file(openapiGeneratedSources).absolutePath
    apiPackage = "${apiPackageName}.controller"
    modelPackage = "${apiPackageName}.model"
    generateAliasAsModel = true
    verbose = true
    globalProperties = [
        'apis'  : project.ext.apisToGenerate,
        'models': 'true'
    ]
    configOptions = [
        interfaceOnly                           : 'true',
        singleContentTypes                      : 'true',
        useSingleRequestMethod                  : 'true',
        useSwaggerAnnotations                   : 'false',
        useTags                                 : 'true',
        dateLibrary                             : 'java8',
        library                                 : 'quarkus',
        useMicroProfileOpenAPIAnnotations       : 'true',
        additionalModelTypeAnnotations          : '@jakarta.validation.constraints.NotNull',
        useBeanValidation                       : 'true',
        useJakartaEe                            : 'true',
        disallowAdditionalPropertiesIfNotPresent: 'false',
        generateModelTests                      : 'false',
        generateModelDocumentation              : 'false',
        generateApiTests                        : 'false',
        generateApiDocumentation                : 'false',
        generateBuilders                        : 'true',
        modelPropertyNaming                     : 'original',
        returnResponse                          : 'true',
    ]
}

And the project.ext.apisToGenerate property is set to 'Common'. The problem I have is that models are not generated. They are only when I remove the selection of apis to generate. But I need to generate only the apis for specific tags.

Any help is appreciated.

Do I need to explicitly list the models I need to generate in the ‘models’ property?


Solution

  • I found the answer in the gradle plugin documentation:

    When enabling generation of only specific parts you either have to provide CSV list of what you particularly are generating or provide an empty string "" to generate everything. If you provide "true" it will be treated as a specific name of model or api you want to generate.