OpenAPITools / OpenAPITools/openapi-generator

[BUG] Wrong imports for maps

Open
#6,459 3 comments 0 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

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

While fixing the import paths on one of the python generators, I came across an odd problem.

Its to do with the use of https://swagger.io/docs/specification/data-models/dictionaries/

You get library imports for arrays and maps and it should just be maps.

openapi-generator version

All of them

OpenAPI declaration file content or url

Typical example:

reporting_state:
  type: object
  properties:
    file_list:
      type: object
      additionalProperties:
        $ref: '#/file_path'
file_path:      # Can be referenced via '#/file_path'
  type: object
  properties:
    path:
      type: string
Command line used for generation

N/A

Steps to reproduce

N/A

Related issues/PRs

Issue observed in #6065

Suggest a fix

Its found in the

addVars(CodegenModel m, List<CodegenProperty> vars, Map<String, Schema> properties, Set<String> mandatory)

method in DefaultCodegen.

The method contains the following snippet

// TODO revise the logic to include map
if (cp.isContainer) {
    addImport(m, typeMapping.get("array"));
}

Which courteously tells you whats wrong. The fix for the python generators was very simple

if (cp.isContainer) {
    if (cp.isListContainer) {
        addImport(m, typeMapping.get("array"));
    }
    else if (cp.isMapContainer) {
        addImport(m, typeMapping.get("map"));
    }
}

However, I don't know if this is really valid for the other generators, so I don't want to add the change that effects everything.

It does looks like it may be OK, but it looks too simple not to be there, unless it effects other languages differently. I'm reporting it, just in case it can be used.

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 in DefaultCodegen.addVars(CodegenModel m, List vars, Map<String, Schema> properties, Set mandatory), focusing on the container import logic and the map example in the issue. Compare generated imports for map-only models across generators, including the Python generators and the case from #6065; done means maps receive the appropriate imports without adding array imports.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
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.