OpenAPITools / OpenAPITools/openapi-generator

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

Open
#14,232 6 comments 10 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the supplied OpenAPI 3.1.0 schema and the Gradle generateSpringServerClasses task using openapi-generator 6.2.1. Reproduce the failure with typed additionalProperties, then verify that generation succeeds and produces the intended Map<String, String> or referenced value type without the Boolean deserialization exception.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi, spring
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.