OpenAPITools / OpenAPITools/openapi-generator

[BUG][typescript] Missing import statements in model with circular oneOf

Open
#23,286 0 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?
  • 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?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When using a circular oneOf schema, the typescript generator does not generate the import statements anymore.

openapi-generator version

This is a regression that I noticed after upgrading from 6.5 to 7.20.

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: Repro
  version: 1.0.0
paths: {}
components:
  schemas:
    Repro:
      type: object
      properties:
        content:
          oneOf:
            - type: string
            - type: array
              items:
                $ref: '#/components/schemas/Repro'
Generation Details
java -jar openapi-generator-cli.jar generate -i /tmp/repro.yaml -g typescript -o /tmp/out
Steps to reproduce
  1. Generate the provided schema using the options above.
  2. Verify that Repro and ReproContent models are created
  3. Observe that ReproContent is missing the import statement to Repro
Related issues/PRs

I've found several somewhat similar issues (on other typescript generators, though). This one may be relevant:
https://github.com/openapitools/openapi-generator/issues/21441

Search used: https://github.com/openapitools/openapi-generator/issues?q=is%3Aissue%20typescript%20import%20oneOf%20sort%3Acreated-desc

Suggest a fix

I can't submit a PR myself right now, but I've tried to run an LLM over the issue so please take the following with a grain of salt:

When generating union types for schemas using oneOf, the TypeScriptClientCodegen.java attempts to filter out unused imports and only keep imports related to the schemas specified within the oneOf definition. It is however performing an exact match between the import name (e.g. Repro) and the string representation of the oneOf elements (e.g. Array<Repro>).

When modifying the code to instead perform a substring match, it seems to resolve the problem for me (though I have NOT verified potential side-effects):

diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptClientCodegen.java
index 085461f446e..0981ba5f5d5 100644
--- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptClientCodegen.java
+++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptClientCodegen.java
@@ -394,8 +394,11 @@ public class TypeScriptClientCodegen extends AbstractTypeScriptClientCodegen imp
                 // For oneOfs only import $refs within the oneOf
                 TreeSet<String> oneOfRefs = new TreeSet<>();
                 for (String im : cm.imports) {
-                    if (cm.oneOf.contains(im)) {
-                        oneOfRefs.add(im);
+                    for (String oneOf : cm.oneOf) {
+                        if (oneOf.contains(im)) {
+                            oneOfRefs.add(im);
+                            break;
+                        }
                     }
                 }
                 cm.imports = oneOfRefs;

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 modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptClientCodegen.java and reproduce the issue using the provided OpenAPI YAML and generation command. Inspect the generated ReproContent model and verify that it imports Repro without removing unrelated imports; confirm the result against the circular oneOf example.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, typescript
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.