tsoa icon indicating copy to clipboard operation
tsoa copied to clipboard

The @Example decorator doesn't work with Date constructor arguments

Open AudunWA opened this issue 3 years ago • 3 comments

The @Example decorator doesn't work when providing Date objects with the (year: number, month: number, date?: number, hours?: number, minutes?: number, seconds?: number, ms?: number) constructor.

Sorting

  • I'm submitting a ...

    • [x] bug report
    • [ ] feature request
    • [ ] support request
  • I confirm that I

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

Expected Behavior

I expected the following example to be generated:

"examples": {
	"Example 1": {
		"value": {
			"date": "2021-11-01T09:00:00.000Z"
		}
	}
}

The TypeScript code that would generate this json would be:

@Example<Data>({
	date: new Date(2021, 10, 1, 9, 0, 0, 0)
})

Current Behavior

Currently, tsoa fails with the following error:

Generate routes error.
 RangeError: Invalid time value
    at Date.toISOString (<anonymous>)
    at Object.getInitializerValue (.../node_modules/@tsoa/cli/src/metadataGeneration/initializer-value.ts:35:33)
    at .../node_modules/@tsoa/cli/src/metadataGeneration/initializer-value.ts:48:37
    at Array.forEach (<anonymous>)
    at Object.getInitializerValue (.../node_modules/@tsoa/cli/src/metadataGeneration/initializer-value.ts:47:32)
    at .../node_modules/@tsoa/cli/src/metadataGeneration/initializer-value.ts:12:51
    at Array.map (<anonymous>)
    at Object.getInitializerValue (.../node_modules/@tsoa/cli/src/metadataGeneration/initializer-value.ts:12:36)
    at .../node_modules/@tsoa/cli/src/metadataGeneration/initializer-value.ts:48:37
    at Array.forEach (<anonymous>)

Possible Solution

By manually changing this part of initializer-value.ts:

if (argsValue.length > 0) {
  date = new Date(argsValue as any);
}

to

if (argsValue.length > 0) {
  // @ts-ignore
  date = new Date(...argsValue)
}

it works.

Steps to Reproduce

  1. Add the following @Example decorator to a endpoint in a controller:
@Example<Data>({
	date: new Date(2021, 10, 1, 9, 0, 0, 0)
})
  1. Run tsoa spec
  2. Observe that tsoa crashes.

Context (Environment)

Version of the library: 3.11.2 Version of NodeJS: v14.17.0

  • Confirm you were using yarn not npm: [ ]

Detailed Description

Breaking change?

I haven't tested with other variants of the Date constructor, so it might break some of those.

AudunWA avatar Nov 05 '21 10:11 AudunWA

Hello there AudunWA 👋

Welcome to tsoa !!💖🥳

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

We will try to get back to you as soon as we can.👀

github-actions[bot] avatar Nov 05 '21 10:11 github-actions[bot]

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

github-actions[bot] avatar Dec 06 '21 00:12 github-actions[bot]

Not stale

AudunWA avatar Dec 06 '21 08:12 AudunWA