openapi-generatoropenapi-generator-maven-plugin

Use existing class with openapi-generator-maven-plugin without skipping validation


I am using plugin openapi-generator-maven-plugin in version 6.3.0 and openapi 3.0.3 (yaml spec).

I want to use an existing model class instead of creating it by generator. I almost used the existing class successfully in generated api, but I had to skip the validation of spec by setting <skipValidateSpec>true</skipValidateSpec>.

Is there a way of keeping the spec validation active?

Here is my current config which works with skipped validation:

            <plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <version>6.3.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>
                                ${project.basedir}/src/main/resources/openapi.yaml
                            </inputSpec>
                            <generatorName>spring</generatorName>
                            <apiPackage>com.example.openapigenerator.api</apiPackage>
                            <skipValidateSpec>true</skipValidateSpec>
                            <languageSpecificPrimitives>ArticleEntity</languageSpecificPrimitives>
                            <importMappings>
                                ArticleEntity=com.example.openapigenerator.model.Article
                            </importMappings>
                            <modelPackage>com.example.openapigenerator.model</modelPackage>
                            <configOptions>
                                <interfaceOnly>true</interfaceOnly>
                                <useSpringBoot3>true</useSpringBoot3>
                                <legacyDiscriminatorBehavior>false</legacyDiscriminatorBehavior>
                            </configOptions>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
paths:
  /article:
    post:
      tags:
        - article
      summary: Add a new article to the store
      description: Add a new article to the store
      operationId: addArticle
      requestBody:
        description: Create a new article in the store
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ArticleEntity'
        required: true
      responses:
        '200':
          description: Successful operation
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ArticleEntity'

components:
  schemas:
    ArticleEntity:
      $ref: '#/components/schemas/Article'


Solution

  • Taking @tbatch 's answer further, the typeMappings brougth me to the following solution:

    paths:
      /article:
        post:
          tags:
            - article
          summary: Add a new article to the store
          description: Add a new article to the store
          operationId: addArticle
          requestBody:
            description: Create a new article in the store
            content:
              application/json:
                schema:
                  $ref: '#/components/schemas/Article'
            required: true
          responses:
            '200':
              description: Successful operation
              content:
                application/json:
                  schema:
                    $ref: '#/components/schemas/Article'
    
    components:
      schemas:
        Article:
          type: object
          format: article
    
    <plugin>
        <groupId>org.openapitools</groupId>
        <artifactId>openapi-generator-maven-plugin</artifactId>
        <version>6.3.0</version>
        <executions>
            <execution>
                <goals>
                    <goal>generate</goal>
                </goals>
                <configuration>
                    <inputSpec>
                        ${project.basedir}/src/main/resources/openapi.yaml
                    </inputSpec>
                    <generatorName>spring</generatorName>
                    <apiPackage>com.example.openapigenerator.api</apiPackage>
                    <skipValidateSpec>false</skipValidateSpec> <!-- My intended goal, to keep spec validation active -->
                    <importMappings>
                        Article=com.example.openapigenerator.model.Article
                    </importMappings>
                    <typeMappings>object+article=Article</typeMappings> <!-- THIS DID THE TRICK FOR ME -->
                    <modelPackage>com.example.openapigenerator.model</modelPackage>
                    <configOptions>
                        <interfaceOnly>true</interfaceOnly>
                        <useSpringBoot3>true</useSpringBoot3>
                        <legacyDiscriminatorBehavior>false</legacyDiscriminatorBehavior>
                    </configOptions>
                </configuration>
            </execution>
        </executions>
    </plugin>