undefined is not supported in model properties
See original GitHub issueSorting
-
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
I would like to use models that can have the undefined
type:
import {Controller, Get, Route} from 'tsoa';
interface User {
name: string | undefined;
}
@Route('user')
export class UserController extends Controller {
@Get()
public async getUser(): Promise<User> {
return {name: undefined};
}
}
Current Behavior
I get an error when trying to generate the spec:
yarn tsoa spec
yarn run v1.22.4
$ /Users/cedric/dev/kraaft-firebase/app-engine/node_modules/.bin/tsoa spec
There was a problem resolving type of 'User'.
Generate swagger error.
Error: Unknown type: UndefinedKeyword
At: src/api/userController.ts:10:10.
This was caused by 'string | undefined'
at new GenerateMetadataError (/Users/cedric/dev/kraaft-firebase/app-engine/node_modules/tsoa/node_modules/@tsoa/cli/dist/metadataGeneration/exceptions.js:22:28)
at TypeResolver.resolve (/Users/cedric/dev/kraaft-firebase/app-engine/node_modules/tsoa/node_modules/@tsoa/cli/dist/metadataGeneration/typeResolver.js:308:19)
at /Users/cedric/dev/kraaft-firebase/app-engine/node_modules/tsoa/node_modules/@tsoa/cli/dist/metadataGeneration/typeResolver.js:113:95
at Array.map (<anonymous>)
at TypeResolver.resolve (/Users/cedric/dev/kraaft-firebase/app-engine/node_modules/tsoa/node_modules/@tsoa/cli/dist/metadataGeneration/typeResolver.js:112:45)
at TypeResolver.propertyFromSignature (/Users/cedric/dev/kraaft-firebase/app-engine/node_modules/tsoa/node_modules/@tsoa/cli/dist/metadataGeneration/typeResolver.js:761:143)
at /Users/cedric/dev/kraaft-firebase/app-engine/node_modules/tsoa/node_modules/@tsoa/cli/dist/metadataGeneration/typeResolver.js:727:161
at Array.map (<anonymous>)
at TypeResolver.getModelProperties (/Users/cedric/dev/kraaft-firebase/app-engine/node_modules/tsoa/node_modules/@tsoa/cli/dist/metadataGeneration/typeResolver.js:727:124)
at TypeResolver.getModelReference (/Users/cedric/dev/kraaft-firebase/app-engine/node_modules/tsoa/node_modules/@tsoa/cli/dist/metadataGeneration/typeResolver.js:552:31)
Possible Solution
The attribute could be treated as if it was optional.
Steps to Reproduce
- create a controller like the one above
- generate the spec:
yarn tsoa spec
Context (Environment)
Version of the library: 3.4.0 Version of NodeJS: v10.21.0
- Confirm you were using yarn not npm: [x]
Detailed Description
Breaking change?
No
Issue Analytics
- State:
- Created 3 years ago
- Comments:7
Top Results From Across the Web
Cannot Read Property of Undefined in JavaScript - Rollbar
Undefined means that a variable has been declared but has not been assigned a value. In JavaScript, properties and functions can only belong...
Read more >How to solve the following TypeError: Cannot read properties ...
Error says you can't read properties of undefined, which means they're trying to access properties of an object, where the object is ...
Read more >ReferenceError: reference to undefined property "x" - JavaScript
The JavaScript warning "reference to undefined property" occurs when a script attempted to access an object property which doesn't exist.
Read more >How to Prevent the Error: Cannot Read Property '0' of Undefined
A guide on how to prevent the error "cannot Read Property '0' of Undefined", covering techniques such as try, catch, using const over...
Read more >Error & Warning Details - typegoose
The function used to create a custom model name (via the modelOptions "option" prop) must return a string and it should not be...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
OpenAPI 2/3 does not support an
undefined
type, only optional properties. We still want to distinguish between null and undefined, so modeling this in TS usingis the best way to represent this currently. The proper way to solve this would be to add a Tsoa.UndefinedType, which marks the property as optional if it is a property type and handles the case where it is not, for example a route handler:
public getStuff(): undefined
.Ran into same issue. Anyone holding this issue?