[英]Generate a property as schema definition in OpenApi 3.0
使用 swagger 2.0 时, java.util.Currency
类作为单独的定义生成。 但是当我们生成 OpenAPI 3.0 时,我们遇到了 swagger-core 将其作为属性生成的问题。
我们有这个班级:
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 规范:
<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
选项似乎不起作用,因为它是 property 而不是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
选项绑定到属性而不是对象?
好吧,您可以考虑一种解决方法:向货币字段添加注释@Schema(ref = "Currency")
将使插件跳过生成属性。 不幸的是,它也不会为 Currency 类生成模式定义:
components:
schemas:
Wrapper:
type: object
properties:
currency:
$ref: '#/components/schemas/Currency'
我不确定这对你来说是否是一个问题,因为你无论如何都要将它映射回java.util.Currency
。 但如果是这样,还有另一个技巧:您可以提供带有部分架构(仅货币定义)的静态文件,并配置插件以将其与生成的架构( openapiFilePath
参数)合并。
插件配置:
<plugin>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-maven-plugin</artifactId>
<version>2.0.9</version>
<configuration>
...
<openapiFilePath>src/main/resources/currency.yaml</openapiFilePath>
...
</configuration>
...
</plugin>
货币.yaml:
components:
schemas:
Currency:
type: object
properties:
currencyCode:
type: string
defaultFractionDigits:
type: integer
format: int32
numericCode:
type: integer
format: int32
displayName:
type: string
symbol:
type: string
这是一个黑客,我知道,但如果没有其他选择......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.