OpenAPITools / OpenAPITools/openapi-generator

[BUG][Typescript] Circular dependencies inside the same file

Open
#16,306 2 comments 1 reaction 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)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output? It adds imports that don't need to be there, because the interface is in the file already.
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

Hello. I'm using the Typescript generator to create an API client for one of our micro services. It seems to be creating an import from the file it's already in. I have a schema Tree that has a list of RelationNode, which also might have lists of RelationNode, and I end up with imports of RelationNode inside RelationNode, which is an error. I don't know if we call it circular, but in lack of a better word I did.

Codewise, it looks like this: In the file RelationNode.ts, I have this import

import type { RelationNode } from './RelationNode';
import {
    RelationNodeFromJSON,
    RelationNodeFromJSONTyped,
    RelationNodeToJSON,
} from './RelationNode';

which is an error. The code still works, but running tsc on it fails.

openapi-generator version

Tested with 2.5.2 and 2.7.0 (latest as of writing).

OpenAPI declaration file content or url

https://gist.github.com/evengul/ac8759df0d42c33f63ed09fbfc38ba54

Generation Details

I used the following command to create the result (with the NPM package): openapi-generator-cli generate -g typescript-fetch -i http://localhost:8010/api-docs, where the localhosted url consists of the snippet above (and a lot more).

Steps to reproduce
  1. Run the command above with the snippet as the source
  2. See the RelationNode.ts file, and that it has an import to itself.
Related issues/PRs

I see there has been multiple fixes for ordinary circular dependencies, but it doesn't look like it checks if the required type already is in the file.

Suggest a fix

A possible fix might be to check if the file you want to import from is the file you're already in before you add the import statements at the top.

I found this line, which might not be working as expected: https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptClientCodegen.java#L391C19-L391C19

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 modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptClientCodegen.java around the referenced import-handling line. Reproduce with the typescript-fetch generator and the linked OpenAPI schema, then inspect the generated RelationNode.ts file. Done means the generated file has no self-imports and passes tsc.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi, typescript
Domain
devtools, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 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.