我正在为 spring-boot 应用程序编写一个规范,以使用 openapi-generator-maven-plugin 为客户端生成 API。我想导入一些模型,因此我尝试像往常一样使用 schemaMappings 属性导入它们,就像我使用 openapi-generator-gradle-plugin 一样。在 yaml 规范中,我创建了要导入的模型的空架构,并在 pom.xml 的插件设置中指定了所需的类型。我的插件设置:
<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>
Run Code Online (Sandbox Code Playgroud)
但突然我发现,由于某种原因,openapi-generator-maven-plugin 与 openapi-generator-gradle-plugin 不同,不使用 importMappings 属性。它存在于插件的代码中,您可以在生成器的配置或 configOptions 中使用它,但最终它不适用于我自己的 DTO。又检查了一遍。在 Gradle 中一切正常,但在 Maven 中则不然。它没有生成 POJO,因为它们是自由格式的对象,只是用对象类型替换它们。我开始深入研究这个问题,一段时间后我发现这个问题已经存在很长时间了。至少从 5.3.1 版本开始。你可以在这里看到