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

[typescript] fixture: broken Array<Array> #19548

Merged
merged 5 commits into from
Sep 17, 2024

Conversation

joscha
Copy link
Contributor

@joscha joscha commented Sep 8, 2024

Full disclosure: I haven't been able to fix this issue, yet. I can reproduce it and I created a fixture that shows the problem, but I need some help figuring out where exactly the issue occurs.

This introduces a fixture that shows an issue with the typescript generator.

Currently, for a definition like this (reduced for brevity, see array_list.yaml for full version):

      ListPaged:
        type: 'object'
        properties:
          data:
            type: 'array'
            items:
              $ref: '#/components/schemas/List'
      List:
        type: 'object'
        properties:
          id:
            type: 'integer'

(an array of objects with the name List).

as Typescript type like:

export class ListPaged {
    'data'?: Array<Array>;
    // ...
}

is produced. This is both incorrect as well as syntactically invalid (missing generic for the inner Array type).
It should be:

export class ListPaged {
    'data'?: Array<List>;
    // ...
}

Interestingly, if the name of List is changed to something that is not close to a reserved word, like L for example, it generates correctly. E.g.:

export class ListPaged {
    'data'?: Array<L>;
    // ...
}

Thus I am assuming there is somewhere in the model generation code where the word List is interpreted as being an array type.
Running the fixture with typescript-fetch shows that the ListPaged type gets generated correctly for this generator.

PR checklist

import { HttpFile } from '../http/http';

export class ListPaged {
'data'?: Array<Array>;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be:

Suggested change
'data'?: Array<Array>;
'data'?: Array<List>;

@macjohnny
Copy link
Member

the issue occurs because of a type mapping

this happens with any model named like a native type. the typemapping can be overridden by parameter, so i would suggest to close this PR

@joscha
Copy link
Contributor Author

joscha commented Sep 9, 2024

the issue occurs because of a type mapping

I was wondering if that was the case, but couldn't find it. Thanks for the code pointer.

this happens with any model named like a native type. the typemapping can be overridden by parameter, so i would suggest to close this PR

When you say "native type" you mean a native Java type that symbol in swagger matches to?

Given that:

  • List is an object in my example above, which is incompatible with Array
  • There is no native List type in neither Typescript nor Javascript
  • The generated code doesn't transpile because of a type error (the missing generic for Array)
  • The type information is incorrect
  • Other typescript generators don't have that issue
  • Often we don't control the openapi definition, we just get it from a vendor and whatever code is produced should just work out of the box
  • We can automatically determine that a symbol with the name List that is of type: "object" is not a an array
  • It is super hard to debug when you use the generator CLI as an end user, as an implementation detail (the type mapping) leaks through into the generated code

my thoughts are that we should probably fix this in the generator? WDYT?

@macjohnny
Copy link
Member

I dont know why the List type mapping was needed/added, maybe @TiFu knows?

@joscha
Copy link
Contributor Author

joscha commented Sep 17, 2024

I dont know why the List type mapping was needed/added, maybe @TiFu knows?

@TiFu any idea? Would love to fix this one for good.

@macjohnny
Copy link
Member

I guess this could be a leftover, IIRC there was such a List mapping in

typeMapping = new HashMap<String, String>();
typeMapping.put("Set", "Set");
typeMapping.put("set", "Set");
typeMapping.put("Array", "Array");
typeMapping.put("array", "Array");
typeMapping.put("boolean", "boolean");
typeMapping.put("string", "string");
typeMapping.put("int", "number");
typeMapping.put("float", "number");
typeMapping.put("number", "number");
typeMapping.put("long", "number");
typeMapping.put("short", "number");
typeMapping.put("char", "string");
typeMapping.put("double", "number");
typeMapping.put("object", "object");
typeMapping.put("integer", "number");
typeMapping.put("Map", "any");
typeMapping.put("map", "any");
typeMapping.put("date", "string");
typeMapping.put("DateTime", "string");
typeMapping.put("binary", "any");
typeMapping.put("File", "any");
typeMapping.put("file", "any");
typeMapping.put("ByteArray", "string");
typeMapping.put("UUID", "string");
typeMapping.put("URI", "string");
typeMapping.put("Error", "Error");
typeMapping.put("AnyType", "any");
as well, but now there isn't, so I guess we can remove it. @joscha do you want to do that in your PR and update the samples?

@joscha
Copy link
Contributor Author

joscha commented Sep 17, 2024

I guess this could be a leftover, IIRC there was such a List mapping in

typeMapping = new HashMap<String, String>();
typeMapping.put("Set", "Set");
typeMapping.put("set", "Set");
typeMapping.put("Array", "Array");
typeMapping.put("array", "Array");
typeMapping.put("boolean", "boolean");
typeMapping.put("string", "string");
typeMapping.put("int", "number");
typeMapping.put("float", "number");
typeMapping.put("number", "number");
typeMapping.put("long", "number");
typeMapping.put("short", "number");
typeMapping.put("char", "string");
typeMapping.put("double", "number");
typeMapping.put("object", "object");
typeMapping.put("integer", "number");
typeMapping.put("Map", "any");
typeMapping.put("map", "any");
typeMapping.put("date", "string");
typeMapping.put("DateTime", "string");
typeMapping.put("binary", "any");
typeMapping.put("File", "any");
typeMapping.put("file", "any");
typeMapping.put("ByteArray", "string");
typeMapping.put("UUID", "string");
typeMapping.put("URI", "string");
typeMapping.put("Error", "Error");
typeMapping.put("AnyType", "any");
as well, but now there isn't, so I guess we can remove it. @joscha do you want to do that in your PR and update the samples?

Great! Yes, give me an hour or so and I'll put it up!

@joscha
Copy link
Contributor Author

joscha commented Sep 17, 2024

@macjohnny changes are in, PTAL.

@macjohnny macjohnny merged commit 0b084cd into OpenAPITools:master Sep 17, 2024
15 checks passed
@joscha joscha deleted the joscha/issue-array-of-lists branch September 17, 2024 13:07
@joscha
Copy link
Contributor Author

joscha commented Sep 17, 2024

Thank you for 🚤 @macjohnny!

@macjohnny
Copy link
Member

@joscha thanks for your contribution!

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

Successfully merging this pull request may close these issues.

2 participants