>> openapi-generator 中的属性?,java,generics,hashmap,hashset,openapi-generator"/>

首页 > 解决方案 > 如何定义列表>> openapi-generator 中的属性?

问题描述

我定义了 OpenAPI 3.0 文档并使用 openapi-generator-cli-3.3.4.jar 生成 Java 代码 (DTO)。但我无法解决这种情况:List<Map<Integer, Set<String>>>.

  1. Map<Integer, String>问题:

    • 据我所知,我可以使用模式对象additionalProperties定义地图类型。

    • OpenAPI 规范附加属性:值可以是布尔值或对象。内联或引用模式必须是模式对象,而不是标准 JSON 模式。

    • 根据上面,我不能将 Map 键设置为整数,对吗?对这个问题有什么建议吗?

  2. set<String>set<List<String>>问题:我必须尝试一些努力:

测试1:设置“ uniqueItems ”:真

 {
    "openapi": "3.0",
    "info": {
        "version": "1.0.0",
        "title": "Dr.First Schema",
        "license": {
            "name": "MIT"
        }
    },
    "components": {
        "schemas": {
            "Question": {
                "type": "object",
                "properties": {
                    "test": {
                        "type": "array",
                        "items":{
                            "type":"string"
                        }
                    }
                }
            }
        }
    }
}

生成 Java DTO : not Set is List

     /**
   * Get test
   * @return test
  **/
  @ApiModelProperty(value = "")
  public List<String> getTest() {
    return test;
  }

  public void setTest(List<String> test) {
    this.test = test;
  }

Testing2 : 将属性测试类型编辑为 Set

    "test": {
       "type": "Set"       
    }

警告

[main] WARN  o.o.codegen.DefaultCodegen - Unknown type found in the schema: Set
[main] WARN  o.o.codegen.DefaultCodegen - Unknown type found in the schema: Set
[main] WARN  o.o.codegen.DefaultCodegen - Unknown type found in the schema: Set

生成 Java DTO:有语法错误

   /**
   * Get test
   * @return test
  **/
  @ApiModelProperty(value = "")
  public java.util.* getTest() {
    return test;
  }

  public void setTest(java.util.* test) {
    this.test = test;
  }

Testing3 : 编辑要设置的属性测试类型

    "test": {
       "type": "set"       
    }

警告

[main] WARN  o.o.codegen.DefaultCodegen - Unknown type found in the schema: set
[main] WARN  o.o.codegen.DefaultCodegen - Unknown type found in the schema: set
[main] WARN  o.o.codegen.DefaultCodegen - Unknown type found in the schema: set

生成 Java DTO:有 java Set 类型但不知道设置泛型

      /**
   * Get test
   * @return test
  **/
  @ApiModelProperty(value = "")
  public Set getTest() {
    return test;
  }
  public void setTest(Set test) {
    this.test = test;
  }

标签: javagenericshashmaphashsetopenapi-generator

解决方案


对于 openapi 版本 3.0.3,此 Java 泛型集示例有效

yaml 定义:

mapOfSets:
      type: object
      additionalProperties:
        type: setOfStrings

openapi-generator-maven-plugin 的 maven pom.xml 配置:

<typeMappings>
  <typeMapping>setOfStrings=Set&lt;String&gt;</typeMapping>
</typeMappings>

<importMappings>
  <importMapping>Set&lt;String&gt;=java.util.Set</importMapping>
</importMappings>

生成的Java代码:

private Map<String, Set<String>> mapOfSets = null;

这里的setOfStrings是一个自定义的 openapi 类型,它在 pom.xml 中定义了自定义映射,并将生成 Set<String> Java 类型。


推荐阅读