Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define List<Map<Integer, Set<String>>> property in openapi-generator?

I define OpenAPI 3.0 documents and use openapi-generator-cli-3.3.4.jar to generate Java code (DTO). But I can't solve this case: List<Map<Integer, Set<String>>>.

  1. In Map<Integer, String> issue:

    • As I know I can use schema object: additionalProperties define map type.

    • OpenAPI Specification additionalProperties: Value can be boolean or object. Inline or referenced schema MUST be of a Schema Object and not a standard JSON Schema.

    • According above, I can't set Map key to an integer, right? Have any suggestions on this issue?

  2. In set<String> or set<List<String>> issue: I have to try some effort:

Testing1: set "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"
                        }
                    }
                }
            }
        }
    }
}

Generate 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: edit the properties test type to Set

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

Warn

[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

Generate Java DTO: Have syntax error

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

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

Testing3: edit the properties test type to set

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

Warn

[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

Generate Java DTO: Have java Set type but no idea to setting generic

      /**
   * Get test
   * @return test
  **/
  @ApiModelProperty(value = "")
  public Set getTest() {
    return test;
  }
  public void setTest(Set test) {
    this.test = test;
  }
  • Have any suggestions to fix Map<Integer, String> and java Set generic issue in openapi-generator?
like image 415
Eddie.C Avatar asked Jul 05 '19 09:07

Eddie.C


1 Answers

For openapi version 3.0.3 this example of Java generic set works

yaml definition:

mapOfSets:
      type: object
      additionalProperties:
        type: setOfStrings

maven pom.xml configuration for openapi-generator-maven-plugin:

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

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

Generated Java code:

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

Here setOfStrings is a custom openapi type that has custom mappings defined in pom.xml and will generate Set<String> Java type.

like image 102
medojev Avatar answered Oct 17 '22 11:10

medojev