Iva*_*ers 6 java swagger openapi openapi-generator
使用 swagger 2.0 时,java.util.Currency该类作为单独的定义生成。但是当我们生成 OpenAPI 3.0 时,我们遇到了 swagger-core 将其作为属性生成的问题。
我们有这个班级:
import java.util.Currency;
public class Wrapper {
private Currency currency;
}
Run Code Online (Sandbox Code Playgroud)
从这段代码中,我们使用以下插件配置生成 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>
Run Code Online (Sandbox Code Playgroud)
生成时,这将导致此组件定义:
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
Run Code Online (Sandbox Code Playgroud)
然后我们使用以下插件在另一个项目中生成类:
生成类的代码形成了这个 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>
Run Code Online (Sandbox Code Playgroud)
这将产生一个名为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
Run Code Online (Sandbox Code Playgroud)
或者有没有办法将--import-mappings选项绑定到属性而不是对象?
好吧,您可以考虑一种解决方法:向货币字段添加注释@Schema(ref = "Currency")将使插件跳过生成属性。不幸的是,它也不会生成Currency 类的架构定义:
components:
schemas:
Wrapper:
type: object
properties:
currency:
$ref: '#/components/schemas/Currency'
Run Code Online (Sandbox Code Playgroud)
我不确定这对您来说是否是一个问题,因为您正在将其映射回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>
Run Code Online (Sandbox Code Playgroud)
货币.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
Run Code Online (Sandbox Code Playgroud)
我知道这是一个黑客行为,但如果没有其他选择......
| 归档时间: |
|
| 查看次数: |
1555 次 |
| 最近记录: |