Skip to content

[BUG] [typescript-fetch] Circular import generated #6140

@ajaska

Description

@ajaska

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?
Description

The typescript-fetch generator is generating a circular import. In my provided reproducible example, it's

models/index.ts -> models/HTTPValidationError.ts -> models/index.ts

The actual code is:

// models/HTTPValidationError.ts
import {
    ValidationError,
    ValidationErrorFromJSON,
    ValidationErrorFromJSONTyped,
    ValidationErrorToJSON,
} from './';

This produces some annoying warning messages but doesn't seem to actually break the code.

openapi-generator version

4.3.0

OpenAPI declaration file content or url
Click to expand
{
  "openapi": "3.0.2",
  "info": {
    "title": "Some API",
    "version": "0.1.0"
  },
  "paths": {
  },
  "components": {
    "schemas": {
      "HTTPValidationError": {
        "title": "HTTPValidationError",
        "type": "object",
        "properties": {
          "detail": {
            "title": "Detail",
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/ValidationError"
            }
          }
        }
      },
      "ValidationError": {
        "title": "ValidationError",
        "required": ["loc", "msg", "type"],
        "type": "object",
        "properties": {
          "loc": {
            "title": "Location",
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "msg": {
            "title": "Message",
            "type": "string"
          },
          "type": {
            "title": "Error Type",
            "type": "string"
          }
        }
      }
    }
  }
}
Command line used for generation

openapi-generator generate -g typescript-fetch -i test-input.json -o api-client

Steps to reproduce

Run command line with provided input.

Related issues/PRs

Nope

Suggest a fix

I'm pretty sure this can be fixed by re-working the import statements in modelGeneric.mustache and modelOneOf.mustache. Instead of importing from ./, it should be possible to import directly from the thing you're importing. For example, in the code above, it could import from ./ValidationError instead of ./.

A diff might look like (forgive me, I'm not entirely familiar with mustache syntax):

diff --git a/modules/openapi-generator/src/main/resources/typescript-fetch/modelGeneric.mustache b/modules/openapi-generator/src/main/resources/typescript-fetch/modelGeneric.mustache
index bd593da1..263f80ac 100644
--- a/modules/openapi-generator/src/main/resources/typescript-fetch/modelGeneric.mustache
+++ b/modules/openapi-generator/src/main/resources/typescript-fetch/modelGeneric.mustache
@@ -1,13 +1,13 @@
 import { exists, mapValues } from '../runtime';
 {{#hasImports}}
+{{#imports}}
 import {
-    {{#imports}}
     {{{.}}},
     {{.}}FromJSON,
     {{.}}FromJSONTyped,
     {{.}}ToJSON,
-    {{/imports}}
-} from './';
+} from './{{.}}';
+{{/imports}}
 
 {{/hasImports}}
 {{#discriminator}}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions