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] Array model of alias to array model is missing inner type argument #4968

Closed
5 of 6 tasks
bodograumann opened this issue Jan 10, 2020 · 4 comments · Fixed by #4981
Closed
5 of 6 tasks

Comments

@bodograumann
Copy link
Contributor

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

When defining a data scheme, which has a property that contains an array of another data scheme, which is an alias of an array, the resulting property definition is Array<Array>, i.e. the inner generic has no type parameter.

openapi-generator version

I have tested this with master.

OpenAPI declaration file content or url

Example declaration gist: https://gist.github.com/bodograumann/0ec60e9b2b6d8824fba49fa74c2f88a9

Command line used for generation
java -jar ~/Programme/openapi-generator/modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -i array-of-array-alias.openapi.yaml -g typescript-node -o src
Steps to reproduce

Generate the code with the typescript-node, typescript-inversify or typescript-axios template.
Then run tsc --noEmit to typecheck the generated code. The following error will be produced:

src/models/Zoo.ts:15:26 - error TS2314: Generic type 'Array<T>' requires 1 type argument(s).

15     'enclosures'?: Array<Array>;

Instead of Array<Array>, the type should be Array<Array<string>>. Indeed, if instead of the reference to Enclosure, the inner array is included directly in the outer array's items-definition, the correct type is generated. The linked gist contains a second api definition, which demonstrates that fact.

It also does not work with the new typescript template from #802

Related issues/PRs

I have not found any related issues.

In #4569 there was some talk about removing isAlias. If that would be done, I guess that would probably impact this issue as well.

Suggest a fix

Seems like the whole alias handling seems to be volatile and complicated. Maybe defaulting to using the literal name and then optionally inlining would improve the situation? Not sure what the plans for aliases are.

@auto-labeler
Copy link

auto-labeler bot commented Jan 10, 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.

@bodograumann
Copy link
Contributor Author

The code generation seems to also work properly with the java spring template.

I have traced the source of the wrong type declaration to the codegen's getTypeDeclaration method, specifically the ModelUtils.isArrayScheme case.
Compare typescript and java:

if (ModelUtils.isArraySchema(p)) {
ArraySchema ap = (ArraySchema) p;
Schema inner = ap.getItems();
return getSchemaType(p) + "<" + getTypeDeclaration(inner) + ">";

if (ModelUtils.isArraySchema(p)) {
Schema<?> items = getSchemaItems((ArraySchema) p);
return getSchemaType(p) + "<" + getTypeDeclaration(ModelUtils.unaliasSchema(this.openAPI, items)) + ">";

The differences are the calls to getSchemaItems and ModelUtils.unaliasSchema in java. The former only adds some error catching, but the latter is the crux of the problem.
A quick check confirms that adding the call to ModelUtils.unaliasSchema fixes the problem in the typescript templates. Note that there are several subclasses for the various typescript variants which reimplement the getTypeDeclaration method and contain the same error.

I think I should be able to cook up a pull request. Guess for #802 a separate one needs to be created as it only exist in its own branch.

@amakhrov
Copy link
Contributor

amakhrov commented Feb 5, 2020

@bodograumann this looks related to #4563 . Is it so, or is it a separate issue that only manifests itself in Typescript generators?

@bodograumann
Copy link
Contributor Author

@amakhrov You are correct, that this seems to be the same issue. Not sure which other languages are affected though. php maybe, but there the types are only annotations in comments.
The issue should not affect languages without types, like javascript.

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.

2 participants