Andrey
Andrey

Reputation: 75

Code generated by openapi-generator always sends empty arrays when uniqueItems = true in openapi documentation

I made a REST endpoint and an open-api documentation for it using SpringDoc+Swagger. Request body of endpoint has a field - set of objects. As I see in generated documentation, it's the same thing as array, except the uniqueItems field:

  "fooSet": {
    "uniqueItems": true,
    "type": "array",
    "items": {
      "$ref": "#/components/schemas/Foo"
    }
  }

Frontend dev uses openapitools/openapi-generator-cli to interact with my endpoint using open-api docs. This lib enforces him to use JS Set in this case. But it cannot serialize Set properly! The openapi-generator-cli generated this code for serialization: openapi-generator-cli generated code for serialization JSON.stringify returns empty array for any Set: JSON stringify a Set

  1. How can frontend dev customize set serialization? Without editing generated code manually, of course
  2. How can I disable uniqueItems for all Sets without having to replace all of them to Lists, or add annotation on each Set in each DTO?

Upvotes: 0

Views: 2042

Answers (1)

Andrey
Andrey

Reputation: 75

As Helen commented, the problem could be solved by mapping Set to Array via type mappings in gradle task: github.com/OpenAPITools/openapi-generator/issues/11746

The other solution I found is to remove uniqueItems flag manually in OpenApiCustomiser:

override fun customise(openApi: OpenAPI) {
    openApi.components.schemas.forEach { customSchema ->
        customSchema.value.properties.values
            .forEach {
                if (it.uniqueItems == true) {
                    it.uniqueItems = null
                }
            }
    }
}

Upvotes: 0

Related Questions