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] Unique item arrays (i.e. sets) containing primitive types aren't initialized properly #19520

Closed
5 of 6 tasks
davidomid opened this issue Sep 4, 2024 · 0 comments · Fixed by #19521
Closed
5 of 6 tasks

Comments

@davidomid
Copy link
Contributor

davidomid commented Sep 4, 2024

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

Take the following snippet from an OpenAPI spec:

"ExampleModel": {
    "required": [
        "uniqueStringsSet"
    ],
    "type": "object",
    "properties": {
        "uniqueStringsSet": {
            "uniqueItems": true,
            "type": "array",
            "items": {
                "type": "string"
            }
        }
    },
    "additionalProperties": false
}

When typescript-fetch generates the TypeScript for ExampleModel, multiple things happen at the same time:

  • The uniqueStringSet property is correctly declared as type Set<string>.
  • The ExampleModelFromJSONTyped function is incorrectly generated. In this function, the uniqueStringSet property is just assigned the value of json['uniqueStringsSet'] (which is an array), instead of first setting it to new Set(json['uniqueStringsSet']).

The end result is the following:

  • The client thinks the property is of type Set<string> (correct).
  • The actual type is Array (incorrect).
  • If you try to access a property such as size on the value, the result will be undefined (as that doesn't exist on an array), which is a runtime bug.
  • If you try to access an array property such as length, the TypeScript compiler will complain, as it expects you to use size instead (length doesn't exist on Set), which is a compile-time bug.

There doesn't appear to be a workaround for this, except for manually setting the value of uniqueStringSet after the ExampleModel object has been created. This suggested workaround doesn't work for typescript-fetch.

openapi-generator version

7.8.0, although the bug appears to be in previous versions too.

OpenAPI declaration file content or url

N/A, please see example above.

Generation Details

You can reproduce this with the default client generation settings. For example, I run this command to generate my client:

docker run --rm -v "${PWD}:/local" openapitools/openapi-generator-cli generate \
    -i [MY OpenAPI SPEC URI HERE] \
    -g typescript-fetch \
    -o /local/generated
Steps to reproduce

Generate a typescript-fetch client from an OpenAPI spec containing the above structure.

For my own API spec, I declared the following class in my C# .NET API:

public class ExampleModel
{
    [Required]
    public ISet<string> UniqueStringsSet { get; set; }
}
Related issues/PRs

This looks like a similar issue for typescript-rxjs, but it was opened 2 and a half years ago and not resolved. The provided workaround doesn't seem to work for typescript-fetch (explained here):

Not 100% sure if these other issues are related, but they might be:

Suggest a fix

Draft PR here (WIP): #19521

Essentially the bug seems to be caused by isPrimitiveType being true for an array when it contains primitive types. This therefore means the templating is bypassed for the new Set() logic.

davidomid added a commit to davidomid/openapi-generator that referenced this issue Sep 4, 2024
davidomid added a commit to davidomid/openapi-generator that referenced this issue Sep 4, 2024
@davidomid davidomid changed the title [BUG] [typescript-fetch] Unique item arrays (i.e. sets) containing primitive types aren't converted to the correct type [BUG] [typescript-fetch] Unique item arrays (i.e. sets) containing primitive types aren't initialized properly Sep 4, 2024
macjohnny pushed a commit that referenced this issue Sep 5, 2024
…e values aren't initialized properly (#19521)

* Fix for #19520

* Removed redundant Array<any> cast

* Fixed modelGeneric.mustache

* Updated samples
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant