lukeautry / tsoa

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

Fix invalid implementation of nullable enum #1562

Closed iffa closed 6 months ago

iffa commented 7 months ago

What and why?

tsoa currently does not correctly implement OpenAPI v3+ enums when they are marked as nullable. According to the official documentation: https://swagger.io/docs/specification/data-models/enums/

A nullable enum can be defined as follows:

    type: string
    nullable: true  # <---
    enum:
      - asc
      - desc
      - null        # <--- without quotes, i.e. null not "null"

Note that null must be explicitly included in the list of enum values. Using nullable: true alone is not enough here.

This PR aims to fix this issue, so that tsoa more closely matches the specifications. This should fix e.g. an issue where openapi-typescript would not mark a field as nullable, because null is missing from the enum list.

All Submissions:

If this is a new feature submission:

I made this change mostly motivated by our own goals in using tsoa along with openapi-typescript in our product, and this was a pain point I encountered during my implementation, soo I thought I'd take a shot at fixing it.

Potential Problems With The Approach

Backwards-compatibility? Edge cases?

I don't know if the way I implemented this "fix" is correct, but it seems to work, and nothing else broke. My understanding of the tsoa codebase is still in the "wtf am I doing" range.

Test plan

Confirms that null is added to the OpenAPI enum field if nullable is also present, to match expected behavior from specifications.