[英]Generate a property as schema definition in OpenApi 3.0
When using swagger 2.0, the java.util.Currency
class got generated as a seperate definition.使用 swagger 2.0 时, java.util.Currency
类作为单独的定义生成。 But when we generate OpenAPI 3.0, we run into the issue that swagger-core generates it as a property.但是当我们生成 OpenAPI 3.0 时,我们遇到了 swagger-core 将其作为属性生成的问题。
We have fe this class:我们有这个班级:
import java.util.Currency;
public class Wrapper {
private Currency currency;
}
From this code we generate the openapi specification with the following plugin configuration:从这段代码中,我们使用以下插件配置生成 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>
When generated this will result in this component definition:生成时,这将导致此组件定义:
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
We then then generate the classes in another project with the following plugin:然后我们使用以下插件在另一个项目中生成类:
The code to generate the classes form this openapi specification:生成类的代码形成了这个 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>
This will result in a class named WrapperCurrency
.这将产生一个名为WrapperCurrency
的类。 The --import-mappings
option does not seem to work since it's a property and not a schema
. --import-mappings
选项似乎不起作用,因为它是 property 而不是schema
。 This would work if the Currency would be generated as a seperate Schema definition.如果货币将作为单独的架构定义生成,这将起作用。
Is there any way to either, annotate the property in such a way that the java.util.Currency
will be generated as a schema?有没有办法以将java.util.Currency
生成为模式的方式注释属性? something along the lines of:类似的东西:
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
Or is there a way to bind the --import-mappings
option to a property instead of the object?或者有没有办法将--import-mappings
选项绑定到属性而不是对象?
Well, you may consider a workaround: adding an annotation @Schema(ref = "Currency")
to the currency field will make the plugin skip generating properties.好吧,您可以考虑一种解决方法:向货币字段添加注释@Schema(ref = "Currency")
将使插件跳过生成属性。 Unfortunately, it won't generate the schema definition for the Currency class either:不幸的是,它也不会为 Currency 类生成模式定义:
components:
schemas:
Wrapper:
type: object
properties:
currency:
$ref: '#/components/schemas/Currency'
I am not sure if it is an issue for you, as you are mapping it back to java.util.Currency
anyways.我不确定这对你来说是否是一个问题,因为你无论如何都要将它映射回java.util.Currency
。 But if it is, there is another hack: you can provide a static file with a partial schema (just the Currency definition) and configure the plugin to merge it with the generated schema ( openapiFilePath
parameter).但如果是这样,还有另一个技巧:您可以提供带有部分架构(仅货币定义)的静态文件,并配置插件以将其与生成的架构( openapiFilePath
参数)合并。
Plugin configuration:插件配置:
<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>
currency.yaml:货币.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
It's a hack, I know, but if there are no other options...这是一个黑客,我知道,但如果没有其他选择......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.