【问题标题】:How to define List<Map<Integer, Set<String>>> property in openapi-generator?如何在 openapi-generator 中定义 List<Map<Integer, Set<String>>> 属性?
【发布时间】:2019-11-15 21:41:37
【问题描述】:

我定义了 OpenAPI 3.0 文档并使用 openapi-generator-cli-3.3.4.jar 生成 Java 代码 (DTO)。 但是我解决不了这个案子:List&lt;Map&lt;Integer, Set&lt;String&gt;&gt;&gt;.

  1. Map&lt;Integer, String&gt; 问题中:

    • 据我所知,我可以使用schema objectadditionalProperties 定义地图类型。

    • OpenAPI 规范additionalProperties:值可以是布尔值或对象。内联或引用架构必须是架构对象,而不是标准 JSON 架构。

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

  2. set&lt;String&gt;set&lt;List&lt;String&gt;&gt; 问题中:我必须尝试一些努力:

Testing1:设置“uniqueItems”:true

 {
    "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:不 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 设置类型但不知道设置泛型

      /**
   * Get test
   * @return test
  **/
  @ApiModelProperty(value = "")
  public Set getTest() {
    return test;
  }
  public void setTest(Set test) {
    this.test = test;
  }
  • 有什么建议可以修复Map&lt;Integer, String&gt; 和 java Set generic issue in openapi-generator?

【问题讨论】:

  • 嗨,Eddie,您找到解决问题的方法了吗?

标签: java generics hashmap hashset openapi-generator


【解决方案1】:

对于 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 Java 类型。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-09-30
    • 1970-01-01
    • 1970-01-01
    • 2019-08-26
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 2017-04-06
    相关资源
    最近更新 更多