Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
5 tasks done
ajaska opened this issue May 2, 2020 · 8 comments · Fixed by #10798
Closed
5 tasks done

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

ajaska opened this issue May 2, 2020 · 8 comments · Fixed by #10798

Comments

@ajaska
Copy link

ajaska commented May 2, 2020

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}}
@auto-labeler
Copy link

auto-labeler bot commented May 2, 2020

👍 Thanks for opening this issue!
🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

@amakhrov
Copy link
Contributor

amakhrov commented May 3, 2020

Note that even with direct imports from the individual model files (as you suggested), it's possible to get circular reference if actual models in api-spec have such a circular reference.

@ajaska
Copy link
Author

ajaska commented May 3, 2020

True, though I guess that would sort of be expected at that point.

Do you think this is worth fixing for this case? It only seems to cause warnings and not errors as long as the dependencies aren't truly circular (i.e. actually circularly referential), so I suppose I could live with just ignoring the warnings, but it also seems like a pretty small and easy fix?

@amakhrov
Copy link
Contributor

amakhrov commented May 3, 2020

Yeah I don't see how such a change can hurt in any way. Would you want to make a PR with the fix?

@dngconsulting
Copy link
Contributor

Hello, any update on this ? Living with a warning is a bit painful...

@gerardo-navarro
Copy link

gerardo-navarro commented May 24, 2021

Hi, I also stumbled upon this today. Are there any plans to address this warning? Thanks for the update and all the help.

@lingster
Copy link
Contributor

I'm sure the maintainers would welcome a PR.

BeryJu added a commit to BeryJu/openapi-generator that referenced this issue Nov 6, 2021
@BeryJu
Copy link
Contributor

BeryJu commented Nov 6, 2021

I've just created a PR to fix this issue based on the work of @ajaska , and you can try it using this docker container ghcr.io/beryju/openapi-generator (based on master so might be buggy)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants