Home > Software design >  Issue with dictionary definition and the openapi-generator
Issue with dictionary definition and the openapi-generator

Time:12-12

I try to generate Java (Spring) code for the following openapi 3.1.0 specification using the openapi gradle plugin version 6.2.1

openapi: 3.1.0
info:
  title: My-API
  version: 0.0.1
paths:
  /module:
    get:
      operationId: listModules
      summary: get modules
      tags:
        - Modules
      responses:
        "200":
          description: OK
          content:
           application/json:
            schema:
              $ref: '#/components/schemas/Module'

components:
  schemas:
    Module:
      type: object
      description: A module
      properties:
        id:
          type: string
          format: uuid
        metaData:
          type: object
          additionalProperties:
            type: string

The type Module should have a simple id and a map from String to String. Using additionalProperties is the way to define maps according to the official openapi specification https://spec.openapis.org/oas/v3.1.0#parameter-object-examples

However, it fails with the follwing Exception:

java.lang.IllegalArgumentException: Cannot deserialize value of type java.lang.Boolean from Object value (token JsonToken.START_OBJECT) at [Source: UNKNOWN; byte offset: #UNKNOWN]

It seems, additionalProperties expects to be a boolean, and indeed, if I change the line to

additionalProperties: true

the code generation succeeds and generates a Map<String, Object>. However, I would like to specify the value type. In fact, I don't want simple string values but a more complex types using

additionalProperties:
  $ref: '#components/schemas/MetaDataItem'

which fails with the same exception. What is wrong here?

CodePudding user response:

Your issue is with the very first line of your schema. The openapi generator doesn't fully support openapi 3.1.0. Per the readme on their github:

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (both 2.0 and 3.0 are supported).

We've gradually supported more features (e.g. oneOf, anyOf introduced in OpenAPI 3.0) in various generators and we will continue this approach to deliver something based on our understanding of user demand and what they want, and continue to add support of new features introduced in OpenAPI specification (such as v3.1 and future versions of the OpenAPI specification).

If you change the version to 3.0.* then you should se that the file is generated as you would expect. I tried it myself using 3.0.3 and got a Module.java file with

  @JsonProperty("metaData")
  @Valid
  private Map<String, String> metaData = null;

  public Module metaData(Map<String, String> metaData) {
    this.metaData = metaData;
    return this;
  }
  • Related