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

1

8

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?
Ashlar answered 5/7, 2019 at 9:4 Comment(1)
Hi Eddie, were you able to find a fix to your issue?Thorner
C
1

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.

Coelho answered 19/10, 2021 at 11:57 Comment(1)
"No enum constant org.eclipse.microprofile.openapi.models.media.Schema.SchemaType.SETOFSTRINGS" with openapi-generator-maven-plugin 5.4.0Widow

© 2022 - 2024 — McMap. All rights reserved.