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-angular] generator generates invalid TypeScript for polymorphic types #5467

Closed
5 of 6 tasks
t03apt opened this issue Feb 27, 2020 · 7 comments
Closed
5 of 6 tasks

Comments

@t03apt
Copy link

t03apt commented Feb 27, 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?
  • [Optional] Bounty to sponsor the fix (example)
Description

[typescript-angular] generator generates invalid TypeScript for polymorphic types.
For example:

import { Circle } from '../model/models';
import { Rectangle } from '../model/models';
import { Rectangle | Circle } from '../model/models';

Expected:

import { Rectangle, Circle } from '../model/models';
openapi-generator version

4.2.3

How to reproduce

openapi-generator generate -i https://gist.githubusercontent.com/t03apt/71391e24045ef08dcad11bfae12037ae/raw/9c83bd41be70063d1436f84c842253018a6c8c46/Swashbuckle.AspNetCore.Test.WebSites.Basic.swagger.json -o ./generated -g typescript-angular --skip-validate-spec

@auto-labeler
Copy link

auto-labeler bot commented Feb 27, 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.

@macjohnny
Copy link
Member

related: #4130 (comment)

@szilanor
Copy link

any update on this?

AbstractTypeScriptClientCodegen.java

private Set<String> parseImports(CodegenModel cm) {
   Set<String> newImports = new HashSet<String>();
   if (cm.imports.size() > 0) {
      for (String name : cm.imports) {
         if (name.indexOf(" | ") >= 0) {
            String[] parts = name.split(" \\| ");
            for (String s : parts) {
               newImports.add(s);
            }
         } else {
               newImports.add(name);
         }
      }
   }
   return newImports;
}

private List<Map<String, String>> toTsImports(CodegenModel cm, Set<String> imports) {
   List<Map<String, String>> tsImports = new ArrayList<>();
   for (String im : imports) {
      if (!im.equals(cm.classname)) {
         HashMap<String, String> tsImport = new HashMap<>();
         // TVG: This is used as class name in the import statements of the model file
         tsImport.put("classname", im);
         tsImport.put("filename", toModelFilename(removeModelPrefixSuffix(im)));
         tsImports.add(tsImport);
      }
   }
   return tsImports;
}

I saw you have this splitting logic for the imports, but I still get the same error for oneOf types.

I am using openapi-gen 4.3.0

Swagger schema

"schema": {
   "type": "array",
   "items": {
      "oneOf": [
         { "$ref": "#/components/schemas/Circle" },
         { "$ref": "#/components/schemas/Rectangle" }
       ]
    }
}

The return type for the endpoints are valid, but the imports are still

import { Circle } from '../model/models';
import { Rectangle } from '../model/models';
import { Rectangle | Circle } from '../model/models';

@tomghyselinck
Copy link
Contributor

Can this be an issue in the mustache file(s)?

@macjohnny
Copy link
Member

Can this be an issue in the mustache file(s)?

I guess it is rather a problem in the type-generation in the java code

@felschr
Copy link

felschr commented Aug 19, 2020

This is also an issue with the typescript-fetch generator.

@macjohnny
Copy link
Member

fixed in #6789

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

No branches or pull requests

5 participants