question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

undefined is not supported in model properties

See original GitHub issue

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

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

  1. create a controller like the one above
  2. 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:closed
  • Created 3 years ago
  • Comments:7

github_iconTop GitHub Comments

3reactions
WoHcommented, Jan 21, 2021

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 using

interface User {
  name?: string;
}

is 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.

0reactions
haedaalcommented, May 26, 2021

Ran into same issue. Anyone holding this issue?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found