Search code examples
spring-bootmaven-pluginspringdoc-openui

How to append throws exception to a rest endpoint generated by openapi-generator-maven-plugin


I'm using openapi-generator-maven-plugin to generate java class from yaml file in springboot application. I want to make all the endpoint throwing CustomException.

How to configure the plugin to do this ?

Here's the open-ui.yaml. I defined the endpoint "users" that return a list of UserDto. I want that method to have throws Exception in the signature.

openapi: "3.0.0"
info:
  version: 1.0.0
  title: Test Api
  license:
    name: MIT
servers:
  - url: "https://{domain}/test/{basePath}"

paths:
  /users:
    get:
      summary: users
      operationId: getUsers
      tags:
        - users
      parameters:
        - name: limit
          in: query
          schema:
            type: integer

      responses:
        '200':
          description: A page of users
          content:
            application/v1+json; charset=utf-8:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/UserDto'
  

pom.xml

     <plugin>
            <groupId>org.openapitools</groupId>
            <artifactId>openapi-generator-maven-plugin</artifactId>
            <version>5.3.1</version>
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <inputSpec>
                            ${project.basedir}/swagger/open-ui.yaml
                        </inputSpec>
            ....
                </execution>
            </executions>
        </plugin>

Solution

  • had the same problem :) corresponding to official doc (https://openapi-generator.tech/docs/generators/spring/), just add <unhandledException>true</unhandledException> to the <configOptions>

    <configuration>
        ...
        <configOptions>
            ...
            <unhandledException>true</unhandledException>
            ...
        </configOptions>
    </configuration>
    

    This generates my API with the "throws Exception".