简体   繁体   English

在 OpenApi 3.0 中生成一个属性作为架构定义

[英]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 将其作为属性生成的问题。


Generation of the OpenAPI 3.0 specification from the classes从类生成 OpenAPI 3.0 规范

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

Generation of the classes from the OpenAPI 3.0 specification从 OpenAPI 3.0 规范生成类

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.如果货币将作为单独的架构定义生成,这将起作用。


Desired result想要的结果

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.

相关问题 仅从 OpenAPI 定义生成 POJO - Generate only POJOs from OpenAPI definition 为 spring 启动 json 响应测试集成 OpenAPI 3.0 模式 - Integrate OpenAPI 3.0 schema for spring boot json response test Java - 如何直接从 openapi 3.0 规范生成 Swagger UI - Java - how to generate Swagger UI directly from openapi 3.0 specification 如何从 OpenAPI 3.0 yaml 文件生成 JSON 示例? - How to generate JSON examples from OpenAPI 3.0 yaml file? 是否有Java API可以使用内联模式定义生成xml - Is there a java API to generate xml with an inline schema definition 通过 Spring Boot 配置全局定义 Swagger 3.0 (OpenApi) 类型架构 - Define Swagger 3.0 (OpenApi) Type Schema globally via Spring Boot Configuration OpenAPI 规范 (YML/YAML):所有 $refs 替换或扩展至其定义(带有模式验证) - OpenAPI Specification (YML/YAML) : All $refs replace or expand to its definition (with schema validation) 使用 maven 插件在编译时从 Spring 应用程序源生成 OpenAPI 3.0 json/yaml - Generate OpenAPI 3.0 json/yaml from Spring application sources at compile time with maven plugin OpenApi 3.0 SpringBoot 控制器使用 - OpenApi 3.0 SpringBoot Controller use 如何生成支持多态请求主体的 openapi 定义,并相应地生成客户端和服务器存根? - How can I generate an openapi definition which supports a polymorphic request body, and can generate client and server stubs accordingly?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM