OpenAPITools/openapi-generator

[BUG] Issue with dictionary definition and the openapi-generator

Opened this issue ยท 6 comments

Description

I try to generate Java (Spring) code for the openapi 3.1.0 spec file listed below using the openapi gradle plugin version 6.2.1

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.

openapi-generator version

6.2.1

OpenAPI declaration file content or url
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
Generation Details

Here is the gradle plugin task

task generateSpringServerClasses(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask) {
    generatorName = "spring"
    inputSpec.set(f.getCanonicalPath().toString()); // btw, another issue regarding windows paths 
    outputDir = "$buildDir/generated".toString()
    apiPackage = "myorg.api"
    invokerPackage = "myorg.invoker"
    modelPackage = "myorg.model"
    modelNameSuffix = "Dto"

    configOptions[

    ]
}

Steps to reproduce
  • Have a gradle project with openapi generator plugin 6.2.1
  • add the quoted spec file to project resources
  • add the gradle task as quoted above
  • run the gradle task
Related issues/PRs

There is also a Stackoverflow question https://stackoverflow.com/questions/74741536/issue-with-dictionary-definition-and-the-openapi-generator?noredirect=1#comment131912949_74741536

Suggest a fix

An answer to my stackoverflow question indicates that it works with earlier spec versions like 3.0.3 . Thus, it seems to be a missing feature of 3.1.0

I'm facing the same problem ๐Ÿ˜ž

Bump on this, we are experiencing the same problem. Tried generator version 6.3.0 / 6.4.0 / 6.5.0.

This used to work as expected in a previous version (4.3.1) - so this should be marked as a regression?

ff137 commented

An answer to my stackoverflow question indicates that it works with earlier spec versions like 3.0.3 . Thus, it seems to be a missing feature of 3.1.0

I can confirm this seems to be the case. OpenApi Generator fails on our spec when version is set to 3.1.0, but works with 3.0.3. Error reporting is too unclear to debug further:

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

I am also facing the same issue, currently using the --skip-validate-spec argument as a workaround but this is not a good solution. A fix for this would be highly appreciated

I resolved this issue by upgraded from 6.6.0 to 7.4.0.