Skip to content

Type order in nullable boolean types affects deserialization behavior for requests #1820

@AverageCakeSlice

Description

@AverageCakeSlice

I ran into a bug today when attempting to send a PATCH request with a property containing the following type would always cause the type to be deserialized incorrectly.

export interface SomeModel {
    is_active?: boolean | null
}

This field would be backed by a ternary checkbox that can be set to true, false, or null explicitly. However, I noticed that all of my requests when attempting to set the value to null were instead being set to false. I confirmed that the payload being sent from the client did in fact contain null for the field, but then was being changed after the body was deserialized in the controller. After a bit of digging, I determined that the type needs to be both non-optional, AND that the order of the types in the union matters.

This change works as intended, although is a bit of a hack:

export interface SomeModel {
    is_active: null | boolean
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions