lukeautry / tsoa

Build OpenAPI-compliant REST APIs using TypeScript and Node
MIT License
3.32k stars 481 forks source link

Unable to assign null to properties defined as number | null | undefined after updating to version 6.2.0 #1612

Closed maximebroubrou closed 1 month ago

maximebroubrou commented 2 months ago

Description:

After updating TSOA from version 6.0.0 to 6.2.0, we encountered a validation error when attempting to PATCH properties that were previously defined using TypeScript utility types (e.g., Partial, Pick) and are explicitly allowed to be null (defined as number | null). We resolved this issue by changing the type definitions to explicitly define each property without embedded utility types, but it raises concerns about TSOA's handling of such TypeScript constructs in the latest version.

To Reproduce:

Steps to reproduce the behavior:

Original type definition using embedded utility types:

  1. Set up anEntity with the columns:

    @Column({ type: 'boolean', nullable: false, default: false })
    aProperty1: boolean;
    
    @Column({ type: 'text', nullable: false, array: true, default: [] })
    aProperty2: string[];
    
    @Column({ type: 'float', nullable: true, default: null })
    aProperty3: number | null;
  2. Set up a body update type:

    export type aBodyUpdateArgsType = Partial<
    Pick<
    UpdateArgs<anEntity>,
    'aProperty1' | 'aProperty2' | 'aProperty3'
    >
    >;
  3. Set up a TSOA route to update this entity, using a PATCH method:

    @Patch()
    @Body() body: aBodyUpdateArgsType[],
    ...
  4. Launch a server API and: Attempt to PATCH the aProperty1 property with a value of null : it works Attempt to PATCH the aProperty3 property with a value of null : Validation Failed with the following error:

    {
    "message": "Validation Failed",
    "details": [
        "Field \"body.$0.aProperty3\" has invalid value: Could not match the intersection against every type. Issues: [{\"body.$0.aProperty3\":{\"message\":\"invalid float number\",\"value\":null}}]"
    ]
    }
  5. Changing the type definitions to explicitly define each field, as shown below, resolves the issue:

export type aBodyUpdateArgsType = {
  aProperty1?: boolean | null;
  aProperty2?: string[];
  aProperty3?: number | null;
};

Additional Context:

This issue is particularly concerning because it affects our ability to use TypeScript utility types effectively with TSOA, limiting our approach to defining flexible and maintainable APIs. It also suggests a potential regression or change in how TSOA handles TypeScript type resolution in its latest version? πŸ€”

github-actions[bot] commented 2 months ago

Hello there maximebroubrou πŸ‘‹

Thank you for opening your very first issue in this project.

We will try to get back to you as soon as we can.πŸ‘€

jchadwick commented 2 months ago

Related to https://github.com/lukeautry/tsoa/issues/1515?

github-actions[bot] commented 1 month ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days