With openapi-generator, how do you generate a default value for a set

570 views Asked by At

If i define a property like this:

  scopes:
    type: array
    default: []
    items:
       type: string

The generated property is an ArrayList

  @JsonProperty("scopes")
  @Valid
  private List<String> scopes = new ArrayList<>();

However, if I want a set and use

uniqueItems: true

I don't get a default value

  @JsonProperty("scopes")
  @Valid
  private Set<String> scopes;

How can I get the following?

  @JsonProperty("scopes")
  @Valid
  private Set<String> scopes = new LinkedHashSet();
1

There are 1 answers

1
gebi On

Unfortunately, you can not currently get your desired output.

That's because it is currently not implemented in OpenAPI Generator. From OpenAPI Generator's toArrayDefaultValue source code (source):

    } else if (cp.isArray && cp.getUniqueItems()) { // set
        // TODO
        return null;

Your best option might be to open an issue or even better a PR for this topic in the OpenAPI Generator project. An alternative, less easy way to get your desired output might be to use OpenAPI Generator's templating feature to override the computed default values by a hard-coded string like "new HashSet<>()".