Skip to content

Resource model break when specifying an array with unique: true #97

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

Open
rene84 opened this issue Jan 16, 2023 · 4 comments
Open

Resource model break when specifying an array with unique: true #97

rene84 opened this issue Jan 16, 2023 · 4 comments

Comments

@rene84
Copy link

rene84 commented Jan 16, 2023

I've noticed when specifying an array with unique: true translates the model into a Set instead of an Array (makes sense)

but then when passing an actual array as example input, the resource provider fails. Consider for example:

        "Tags": {
            "type": "array",
            "maxItems": 50,
            "uniqueItems": true,
            "insertionOrder": false,
            "description": "An array of key-value pairs to apply to this resource.",
            "items": {
                "$ref": "#/definitions/Tag"
            }
        },

with input:

"Tags": [{
            "Key": "name",
            "Value": "value"
        }]

gives error

"message": "Error: Error: Unsupported type: object [Tag] for tags (Error)",

Removing the uniqueItems fixes the issue and the main thing I notice is that in the generated code the type changes from a Set to an Array

@eduardomourar
Copy link
Contributor

I think this is related to some issues in the class-transformer library. Maybe those workarounds are relevant: typestack/class-transformer#288 (comment) and typestack/class-transformer#495 (comment).

Could you post an snippet of your models.ts file? I believe you can modify your generated models.ts with the provided workaround in order to figure out which change has to be made to the TypeScript code generator.

@rene84
Copy link
Author

rene84 commented Jan 17, 2023

Sure. It looks like this:

    @Expose({ name: 'Tags' })
    @Transform(
        (value: any, obj: any) =>
            transformValue(Tag, 'tags', value, obj, [Set]),
        {
            toClassOnly: true,
        }
    )
    tags?: Optional<Set<Tag>>;

With uniqueItems: false it looks like this

    @Expose({ name: 'Tags' })
    @Type(() => Tag)
    tags?: Optional<Array<Tag>>;

@eduardomourar
Copy link
Contributor

You can find a test reproducing what you faced here.

@eduardomourar
Copy link
Contributor

This has been fixed by #98.

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

No branches or pull requests

2 participants