【问题标题】:Generate a property as schema definition in OpenApi 3.0在 OpenApi 3.0 中生成一个属性作为模式定义
【发布时间】: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


    【解决方案1】:

    好吧,您可以考虑一种解决方法:在货币字段中添加注释@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>
    

    currency.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
    

    我知道这是一个 hack,但如果没有其他选择...

    【讨论】:

    • 感谢您的回答。虽然它不是一个完美的解决方案,但它是一个。如果这是我们想要的方式,我们将进一步调查。现在我会将赏金奖励给你,如果我们决定遵循这条路线,我会将此答案标记为已接受
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-05
    • 2021-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多