Skip to content
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

Typescript union of number generates string enum in swagger #1068

Closed
2 of 4 tasks
tsimbalar opened this issue Aug 31, 2021 · 3 comments
Closed
2 of 4 tasks

Typescript union of number generates string enum in swagger #1068

tsimbalar opened this issue Aug 31, 2021 · 3 comments
Labels
good first issue This issue could be an easy PR for those looking to help contribute help wanted

Comments

@tsimbalar
Copy link
Contributor

Sorting

  • I'm submitting a ...

    • bug report
    • feature request
    • support request
  • I confirm that I

    • used the search to make sure that a similar issue hasn't already been submit

Expected Behavior

Given a type defined as

export interface MyAnswersRequest {
  readonly answers: {
    readonly answer1: AnswerValue;
  };
}

export type AnswerValue = 0 | 1 | 2 | 3;

I would expect it to generate OpenApi.yaml schemas such as :

        AnswerValue:
            type: number
            enum:
                - 0
                - 1
                - 2
                - 3
        MyAnswersRequest:
            properties:
                answers:
                    properties:
                        answer1:
                            $ref: '#/components/schemas/AnswerValue'
                    required:
                        - answer1
                    type: object
            required:
                - answers
            type: object
            additionalProperties: false

Current Behavior

Instead, the generated enum members are generated as strings (with single quotes)

        AnswerValue:
            type: number
            enum:
                - '0'
                - '1'
                - '2'
                - '3'

Our OpenApi linter (Spectral) complains (and it seems right) that even though the type of this enum is number , the listed acceptable values are strings

Note that this only seems to impact the generated swagger file, but validation of incoming requests work properly.

Possible Solution

Steps to Reproduce

Context (Environment)

Version of the library: "@tsoa/runtime": "3.9.0", / "@tsoa/cli": "3.9.0",
Version of NodeJS: v14.16.0

  • Confirm you were using yarn not npm: [ ]

Detailed Description

Breaking change?

This might be a breaking change for people who rely on unions of number ... but this would only impact the generated swagger file, not the runtime behavior.

@WoH WoH added the help wanted label Sep 1, 2021
@WoH
Copy link
Collaborator

WoH commented Sep 5, 2021

I suspect this may happen when we transform to yaml. Does a spec JSON have numbers?

@tsimbalar
Copy link
Contributor Author

I just gave it a try by switching yaml to false in tsoa.json.

The generated swagger.json looks like this :

"AnswerValue": {
				"type": "number",
				"enum": [
					"0",
					"1",
					"2",
					"3"
				]
			},

so the enum members are generated as strings , not numbers in the JSON as well.

@WoH WoH added the good first issue This issue could be an easy PR for those looking to help contribute label Sep 14, 2021
FlorianRappl added a commit to FlorianRappl/tsoa that referenced this issue Oct 31, 2021
WoH pushed a commit that referenced this issue Nov 1, 2021
* Fixed usage of correct types in enums #1068

* Apply review suggestion

* Apply change also to OAS2 as per review suggestion
@tsimbalar
Copy link
Contributor Author

I can confirm this is now fixed as of tsoa/cli v3.14.1
probably thanks to changes in :

@WoH WoH closed this as completed Dec 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue This issue could be an easy PR for those looking to help contribute help wanted
Projects
None yet
Development

No branches or pull requests

2 participants