cloudformation-cli-typescript-plugin icon indicating copy to clipboard operation
cloudformation-cli-typescript-plugin copied to clipboard

Resource model break when specifying an array with unique: true

Open rene84 opened this issue 3 years ago • 4 comments

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

rene84 avatar Jan 16 '23 14:01 rene84

I think this is related to some issues in the class-transformer library. Maybe those workarounds are relevant: https://github.com/typestack/class-transformer/issues/288#issuecomment-614754298 and https://github.com/typestack/class-transformer/issues/495#issuecomment-756449822.

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.

eduardomourar avatar Jan 16 '23 19:01 eduardomourar

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>>;

rene84 avatar Jan 17 '23 09:01 rene84

You can find a test reproducing what you faced here.

eduardomourar avatar Jan 17 '23 11:01 eduardomourar

This has been fixed by https://github.com/aws-cloudformation/cloudformation-cli-typescript-plugin/pull/98.

eduardomourar avatar Mar 09 '23 08:03 eduardomourar