How to prevent Mono Request body generation when generating the API interfaces for spring-webflux with open api code generator?

Viewed 65

I'm creating a spring boot application using spring-webflux and generating the api's using open api code generator. the generated interfaces wrappes the request body with Mono. How to prevent that wrapping ? for example instead of generating

public Mono<ResponseEntity<PostApiDto>> createPost(Mono<PostApiDto> body, ServerWebExchange exchange) {

}

i want the generated interface to look like:

public Mono<ResponseEntity<PostApiDto>> createPost(PostApiDto body) {

}

Here is the api spec file and the pom.xml file:-

openapi.yml

openapi: 3.0.3
info:
  title: "instagram-post-service"
  description: ""
  termsOfService: ""
  version: 1.0.0
externalDocs:
  description: ""
  url: ""
servers:
  - url: http://localhost:8080
    description: Generated server url
tags:
  - name: Post
    description: Operations about posts
    externalDocs:
      description: ""
      url: ""
  - name: Comment
    description: Operations about comments
    externalDocs:
      description: ""
      url: ""
    
paths:
  /posts/me/:
    get:
      tags:
      - Post
      summary: find current user posts
      operationId: getMyPosts
      responses:
        200:
          description: successful operation
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Post'
  /posts/:                  
    post:
      tags:
        - Post
      summary: Create a new Post
      description: ''
      operationId: createPost
      requestBody:
        description: ''
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Post'
        required: true
      responses:
        '200':
          description: Successful operation
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Post'          


components:
  schemas:
    Post:
      type: object
      properties:
        id:
          type: integer
          format: int32
        title:
          type: string
        serviceAddress:
          type: string      

pom.xml


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.2</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.javaworld.instagram</groupId>
    <artifactId>post-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>post-service</name>
    <description>instagram posts micro service</description>

    <properties>
        <java.version>1.8</java.version>
        <swagger-annotations-version>1.6.6</swagger-annotations-version>
        <jackson-databind-nullable>0.2.1</jackson-databind-nullable>
        <org.mapstruct.version>1.3.1.Final</org.mapstruct.version>
    </properties>

    <dependencies>
        
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
                
        <!-- ################### Testing dependencies ################################ -->  
    
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
        </dependency>       
        
        <!-- ########################### DB & persistence layer dependencies ################################ -->

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
                
    
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>6.1.2.Final</version>
        </dependency>
    

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
        </dependency>
        
        <!-- ################### dependencies needed for open-API code generator ################################ -->
        
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-annotations</artifactId>
            <version>${swagger-annotations-version}</version>
        </dependency>

        <dependency>
            <groupId>org.openapitools</groupId>
            <artifactId>jackson-databind-nullable</artifactId>
            <version>${jackson-databind-nullable}</version>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>


        <!-- ################################ MapStruct ################################################ -->

        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>${org.mapstruct.version}</version>
        </dependency>

        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-processor</artifactId>
            <version>${org.mapstruct.version}</version>
        </dependency>

        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.1.0.Final</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <layers>
                        <enabled>true</enabled>
                        <!-- <configuration>${project.basedir}/src/layers.xml</configuration> -->
                    </layers>
                </configuration>
            </plugin>


            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source> 
                    <target>1.8</target> 
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>${org.mapstruct.version}</version>
                        </path>
                        <!-- other annotation processors -->
                    </annotationProcessorPaths>
                </configuration>
            </plugin>

            <!-- open API plug-in for API code generator -->
            <plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <version>6.0.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>
                                ${project.basedir}/src/main/resources/openapi.yml
                            </inputSpec>
                            <generatorName>spring</generatorName>
                            <apiPackage>com.javaworld.instagram.postservice.server.api</apiPackage>
                            <modelPackage>com.javaworld.instagram.postservice.server.dto</modelPackage>
                            <modelNameSuffix>ApiDto</modelNameSuffix>
                            <configOptions>
                                <interfaceOnly>true</interfaceOnly>
                                <skipDefaultInterface>true</skipDefaultInterface>
                                <reactive>true</reactive>
                            </configOptions>

                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>
1 Answers

You need to customize your own swagger-codegen template files (api.mustache and bodyParams.mustache) and add them to your project.

Your pom.xml config

            <plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <version>6.0.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>
                                ${project.basedir}/src/main/resources/openapi.yml
                            </inputSpec>
                            <templateDirectory>src/main/resources/openapi-templates</templateDirectory>
                            <generatorName>spring</generatorName>
                            <apiPackage>com.javaworld.instagram.postservice.server.api</apiPackage>
                            <modelPackage>com.javaworld.instagram.postservice.server.dto</modelPackage>
                            <modelNameSuffix>ApiDto</modelNameSuffix>
                            <configOptions>
                                <interfaceOnly>true</interfaceOnly>
                                <skipDefaultInterface>true</skipDefaultInterface>
                                <reactive>true</reactive>
                            </configOptions>

                        </configuration>
                    </execution>
                </executions>
            </plugin>

templateDirectory - directory with mustache templates api.mustache and bodyParams.mustache

Here you can find modified mustache template files for your case.

Output

default Mono<ResponseEntity<PostApiDto>> createPost(@Parameter(name = "",required = true) @RequestBody @Valid PostApiDto postApiDto, @Parameter(hidden = true) ServerWebExchange exchange) {}
Related