【发布时间】:2020-04-02 18:22:15
【问题描述】:
使用 swagger 2.0 时,java.util.Currency 类作为单独的定义生成。但是当我们生成 OpenAPI 3.0 时,我们遇到了 swagger-core 将其生成为属性的问题。
从类生成 OpenAPI 3.0 规范
我们有 f.e.这个类:
import java.util.Currency;
public class Wrapper {
private Currency currency;
}
通过这段代码,我们使用以下插件配置生成 openapi 规范:
<plugin>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-maven-plugin</artifactId>
<version>2.0.9</version>
<configuration>
<outputFileName>openapi</outputFileName>
<outputFormat>YAML</outputFormat>
<outputPath>....</outputPath>
<resourcePackages>...</resourcePackages>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>resolve</goal>
</goals>
</execution>
</executions>
</plugin>
当生成时,这将导致这个组件定义:
components:
schemas:
Wrapper:
type: "object"
properties:
currency:
type: object
properties:
currencyCode:
type: string
defaultFractionDigits:
type: integer
format: int32
numericCode:
type: integer
format: int32
displayName:
type: string
symbol:
type: string
根据 OpenAPI 3.0 规范生成类
然后我们使用以下插件在另一个项目中生成类:
从这个 openapi 规范生成类的代码:
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>openapi-generation</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/swagger.yaml</inputSpec>
<language>java</language>
<library>jersey2</library>
<generateSupportingFiles>false</generateSupportingFiles>
<configOptions>
<booleanGetterPrefix>is</booleanGetterPrefix>
<dateLibrary>threetenbp</dateLibrary>
<import-mappings>
Currency=java.util.Currency
</import-mappings>
</configOptions>
<generateApis>false</generateApis>
<generateApiDocumentation>false</generateApiDocumentation>
<generateModelTests>false</generateModelTests>
<generateModelDocumentation>false</generateModelDocumentation>
</configuration>
</execution>
</executions>
</plugin>
这将产生一个名为WrapperCurrency 的类。 --import-mappings 选项似乎不起作用,因为它是一个属性而不是 schema。如果将货币作为单独的模式定义生成,这将起作用。
想要的结果
有没有办法以java.util.Currency 将作为模式生成的方式对属性进行注释?类似于:
components:
schemas:
Wrapper:
type: "object"
properties:
currency:
$ref: "components/schemas/Currency"
Currency:
type: object
properties:
currencyCode:
type: string
defaultFractionDigits:
type: integer
format: int32
numericCode:
type: integer
format: int32
displayName:
type: string
symbol:
type: string
或者有没有办法将--import-mappings 选项绑定到属性而不是对象?
【问题讨论】:
标签: java swagger openapi openapi-generator