6

I'm writing a spec for spring-boot app to generate an API for clients using openapi-generator-maven-plugin. There are a few models which I wanted to import so I tried to import them as usual, using schemaMappings property, just in the same way as I did with openapi-generator-gradle-plugin. In yaml spec, I created empty schemas of the models I wanted to import and specified desired types in plugin settings in the pom.xml. My plugins settings:

         <plugin>
            <groupId>org.openapitools</groupId>
            <artifactId>openapi-generator-maven-plugin</artifactId>
            <version>6.4.0</version>
            <executions>
                <execution>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <inputSpec>${project.basedir}/src/main/resources/openapi/some-api.yaml</inputSpec>
                        <generatorName>spring</generatorName>
                        <output>${project.build.directory}/generated-sources/openapi</output>
                        <generateApiTests>false</generateApiTests>
                        <generateModelTests>false</generateModelTests>
                        <generateModelTests>false</generateModelTests>
                        <generateModelDocumentation>false</generateModelDocumentation>
                        <importMappings>
                            <importMapping>MyDto1=com.some.project.metric.MyDto1</importMapping>
                            <importMapping>MyDto2=com.some.project.metric.MyDto2</importMapping>
                        </importMappings>
                        <configOptions>
                            <title>Some project</title>
                            <library>spring-boot</library>
                            <useTags>true</useTags>
                            <dateLibrary>java8</dateLibrary>
                            <basePackage>${default.package}</basePackage>
                            <apiPackage>${default.package}.api</apiPackage>
                            <modelPackage>${default.package}.model</modelPackage>
                            <performBeanValidation>true</performBeanValidation>
                            <interfaceOnly>true</interfaceOnly>
                        </configOptions>
                    </configuration>
                </execution>
            </executions>
        </plugin>

But suddenly I found that for some reason the openapi-generator-maven-plugin, unlike openapi-generator-gradle-plugin, doesn't use the importMappings property. It's present in the code of the plugin and you can use it either in the configuration or in configOptions of a generator, but eventually, it doesn't work with my own DTOs. Checked one more time. In Gradle, everything worked fine, but not in Maven. It didn't generate the POJOs because they are free-form objects and simply substituted them with the Object type. I started digging into the problem and after some time I saw that the problem exists for a long time. At least from version 5.3.1. You can see here

3 Answers 3

22

I've spent on this issue a lot of time so maybe it will be useful to post it here. The solution is to use schemaMappings instead of importMappings. Like that:

                <configuration>
                    <inputSpec>${project.basedir}/src/main/resources/openapi/some-api.yaml</inputSpec>
                    <generatorName>spring</generatorName>
                    <output>${project.build.directory}/generated-sources/openapi</output>
<schemaMappings>MyDto1=com.some.project.metric.MyDto1,MyDto2=com.some.project.metric.MyDto2</schemaMappings>
                    <generateApiTests>false</generateApiTests>
                    <generateModelTests>false</generateModelTests>
                    <generateModelTests>false</generateModelTests>
                    <generateModelDocumentation>false</generateModelDocumentation>
                    <configOptions>
                        <title>Some project</title>
                        <library>spring-boot</library>
                        <useTags>true</useTags>
                        <dateLibrary>java8</dateLibrary>
                        <basePackage>${default.package}</basePackage>
                        <apiPackage>${default.package}.api</apiPackage>
                        <modelPackage>${default.package}.model</modelPackage>
                        <performBeanValidation>true</performBeanValidation>
                        <interfaceOnly>true</interfaceOnly>                        
                    </configOptions>
                </configuration>

Will be glad if you also provide your suggestions.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much for this answer. This configuration has nearly driven me mad....
3

I'll leave an alternative, this option helped me

<configuration>
    <inputSpec>${project.basedir}/src/main/resources/openapi/some-api.yaml</inputSpec>
    <generatorName>spring</generatorName>
    <output>${project.build.directory}/generated-sources/openapi</output>
    <importMappings>
        <importMapping>MyDto1=com.some.project.metric.MyDto1,MyDto2=com.some.project.metric.MyDto2</importMapping>
    </importMappings>
    <generateApiTests>false</generateApiTests>
    <generateModelTests>false</generateModelTests>
    <generateModelTests>false</generateModelTests>
    <generateModelDocumentation>false</generateModelDocumentation>
    <configOptions>
        <title>Some project</title>
        <library>spring-boot</library>
        <useTags>true</useTags>
        <dateLibrary>java8</dateLibrary>
        <basePackage>${default.package}</basePackage>
        <apiPackage>${default.package}.api</apiPackage>
        <modelPackage>${default.package}.model</modelPackage>
        <performBeanValidation>true</performBeanValidation>
        <interfaceOnly>true</interfaceOnly>
    </configOptions>
</configuration>

Comments

0

Answering here for people who (are working with SwaggerHub and) are desperately searching for a way to use Spring org.springframework.data.domain.Page as result in their REST requests:

In my case, the mapping with importMappings did not work, I had to use schemaMappings (in "configuration", not "configOptions" btw):

            <plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <version>${openapi-generator-maven-plugin.version}</version>
                <executions>
                    <execution>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <!-- General config: https://github.com/OpenAPITools/openapi-generator/tree/master/modules/openapi-generator-maven-plugin -->
                            <inputSpec>${swaggerhub-api.json-path}</inputSpec>
                            <generatorName>spring</generatorName>
                            <auth>Authorization: ${env.SWAGGER_HUB_TOKEN}</auth>
                            <!-- Spring pagination result -->
                            <schemaMappings>
                                Page=org.springframework.data.domain.Page
                            </schemaMappings>
                            <!-- config options: https://openapi-generator.tech/docs/generators/spring/#config-options -->
                            <configOptions>
                            ..

I'm using springdoc-openapi version 2.8.9

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.