[typescript-node] Import missing when additional properties point to $ref on the root of a schema

Open
#1,139 3 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
38/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
node.js, openapi, typescript
Domain
tooling

Research direction

Use the provided OpenAPI schema and run the documented openapi-generator command for the typescript-node generator. Inspect the generated TypeScript, especially api.ts and the model references, then run tsc ./ts/api.ts to reproduce the missing B error. Done means the generated code includes the needed import and compiles successfully.

Written by the indexing model from the issue text.

Description

Description

When additionalProperties are on the root of a schema and they point to a $ref of another schema, the generated code for the API does not include the import statement for the model. The generated code attempts to use the model, but the code fails to compile due to the missing import.

openapi-generator version

3.3.0-SNAPSHOT

OpenAPI declaration file content or url
components:
  schemas:
      A:
        type: object
        properties:
          name:
            type: string
        additionalProperties:
          $ref: '#/components/schemas/B'
      B:
        type: object
        properties:
          id:
            type: string
          value:
            anyOf:
              - type: string
              - type: integer

Full test yaml: https://gist.github.com/johnkoerner/faea41ec36ddea55d6522752b9981452

Command line used for generation

openapi-generator generate -i Test.yaml -g typescript-node -o ./ts

Steps to reproduce
  1. Generate the typescript using the command above
  2. Attempt to build the typescript using tsc ./ts/api.ts
  3. Notice that you will get the error error TS2304: Cannot find name 'B'.
Related issues/PRs

See #398 as that is a similar issue. Note that you need 3.3.0 or higher as there are errors you receive from the generator in prior versions.

Dominant language
Java
Stars
26.8k
Forks
7.7k
Avg merge
1d 6h
Merged PRs (30d)
100

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.

More from OpenAPITools/openapi-generator

All issues in OpenAPITools/openapi-generator

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.