Tsoa: TS type aliases are not supported

Created on 24 Feb 2018  Â·  15Comments  Â·  Source: lukeautry/tsoa

Here is an example:

import { Controller, Get, Route } from 'tsoa';

export type Foo = number | string | Date;

@Route('test')
export class Test extends Controller {
  @Get('test1')
  public async getTest1(): Promise<Foo> {
    return 8;
  }
}

Running tsoa routes throws a TypeError:

enerate routes error.
 TypeError: Cannot read property 'text' of undefined
    at /Users/eryk/work/unito/dev/connectors/node_modules/tsoa/dist/metadataGeneration/resolveType.js:260:79
    at Array.map (<anonymous>)
    at getLiteralType (/Users/eryk/work/unito/dev/connectors/node_modules/tsoa/dist/metadataGeneration/resolveType.js:260:27)
    at resolveType (/Users/eryk/work/unito/dev/connectors/node_modules/tsoa/dist/metadataGeneration/resolveType.js:85:23)
    at Object.resolveType (/Users/eryk/work/unito/dev/connectors/node_modules/tsoa/dist/metadataGeneration/resolveType.js:73:20)
    at MethodGenerator.Generate (/Users/eryk/work/unito/dev/connectors/node_modules/tsoa/dist/metadataGeneration/methodGenerator.js:30:34)
    at /Users/eryk/work/unito/dev/connectors/node_modules/tsoa/dist/metadataGeneration/controllerGenerator.js:38:58
    at Array.map (<anonymous>)
    at ControllerGenerator.buildMethods (/Users/eryk/work/unito/dev/connectors/node_modules/tsoa/dist/metadataGeneration/controllerGenerator.js:38:14)
    at ControllerGenerator.Generate (/Users/eryk/work/unito/dev/connectors/node_modules/tsoa/dist/metadataGeneration/controllerGenerator.js:27:27)
help wanted

Most helpful comment

I am getting the exact same thing as @JakeStoeffler, any plan on supporting custom types for defining path params?

@lukeautry

All 15 comments

I also can't do simple things like:

type IdType = string;

and then use it as a path param (trying to have my controller be db-agnostic):

@Delete('{testId}')
async deleteTest(testId: IdType) {
  return Promise.resolve();
}

This gives the following error when running tsoa routes:

Generate routes error.
 Error: @Path('testId') Can't support 'refObject' type. 
 in 'TestController.deleteTest'
    at new GenerateMetadataError (test-proj/node_modules/tsoa/dist/metadataGeneration/exceptions.js:17:28)
    at test-proj/node_modules/tsoa/dist/metadataGeneration/methodGenerator.js:57:23
    at Array.map (<anonymous>)
    at MethodGenerator.buildParameters (test-proj/node_modules/tsoa/dist/metadataGeneration/methodGenerator.js:50:47)
    at MethodGenerator.Generate (test-proj/node_modules/tsoa/dist/metadataGeneration/methodGenerator.js:39:30)
    at test-proj/node_modules/tsoa/dist/metadataGeneration/controllerGenerator.js:38:58
    at Array.map (<anonymous>)
    at ControllerGenerator.buildMethods (test-proj/node_modules/tsoa/dist/metadataGeneration/controllerGenerator.js:38:14)
    at ControllerGenerator.Generate (test-proj/node_modules/tsoa/dist/metadataGeneration/controllerGenerator.js:27:27)
    at test-proj/node_modules/tsoa/dist/metadataGeneration/metadataGenerator.js:60:58

Can reproduce.

@erykwarren What would you expect the swagger definition to look like for your type Foo ?

I am getting the exact same thing as @JakeStoeffler, any plan on supporting custom types for defining path params?

@lukeautry

We just ran into the same issue, any updates on it?

@simllll from what I can tell of reading the code, tsoa simply never supported type aliases. That being said, we’d certainly be open to a PR to support this.

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

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

Accidentally closed it because I was scrolling on mobile. I’ve reopened it.

@erykwarren @simllll @fbarbare @Esya @Superd22 @JakeStoeffler @ronjouch @james-ingold, @bgsandan, @WretchedDade, @AlexChambers, @HarryGogonis @adikaladik @AlexChambers -- We're super excited to share that we have a beta release of the type alias feature available and we'd love you to try it so we can consider releasing it to everyone. It's simple to do. All you have to do is install it with the tag. So like this:

npm install — save tsoa@v3beta

or

yarn add tsoa@v3beta

Please let us know in this thread if you like it or if you find any bugs and we can cut the latest release so that everyone can get this awesome feature. :)

Thanks goes out to @WoH for his incredible work on this contribution. 🎉 🎈 🥇

Hi @dgreene1. Hope you're welcoming some feedback on this issue :) I've tried testing the V3 beta but I am still getting the same error as in the example given by Jake above (ie path param has custom type). Not sure if this issue attempted to solve exactly this use case or if it was specific to return types, or I'm being a noob and doing something completely wrong.

export class IotService extends Controller {
@Get('iot/templates')
public async allTemplates(token: ApigeeToken, @Query() query: string): Promise blah blah
}
Error: @Path('token') Can't support 'refObject' type.

Cheers,
Dave

Dan is currently busy so I hope it's ok if I respond on his behalf:
I assume the formatting is somewhat off in your example, so I'll translate it to what I will assume was the intended format:

export class IotService extends Controller {

  @Get('iot/templates')
  public async allTemplates(token: ApigeeToken, @Query() query: string): Promise<IotTemplate[]> {
    // blah blah
  }
}

The issue occurs because tsoa has no way to know where your expected parameter token: ApigeeToken comes from and how it is supposed to pass it into your method, so it assumes it may come from the path. However, path can only contain "simple" types that can be serialized from the path string, ApigeeToken seems to be an interface or an alias to an interface, so there is no way to do that, which is why tsoa errors. The token however doesn't seem to be a path parameter from my understanding. You might want to take a look at the security decorator to reject unauthorized requests.

Thanks for the quick response Wolfgang. I have controller like actions in my route and tried decorating my service which is completely wrong. I have now created new controllers which work as expected.
A quick question however - is it correct that the only way at present to introduce other middlewares (like caching, cors etc per route) is by using a custom template and not relying on the generated route.ts?

Mostly correct. You're free to register middlewares before you register the routes created by tsoa.
However, there is no decorator/annotation to apply middleware at the Controller/Method level that's shipped out of the box with tsoa (exception: @Security).
One of the reason is that it's complicated to know what parameters/responses should be documented for a given middleware.

See https://github.com/lukeautry/tsoa/issues/62

However, we are getting way off track here, so in case you want to discuss this further or have additional support requests, please open a new issue.

Hi,
I'm using the (ts-mongoose) library for defining
typesafe schema for my Node app. In ts-mongoose the schema may be accessed
as a TS type with a helper like so

const mySchema = createSchema({name: Type.string(), age: Type.number});
type MyProps = ExtractProps<typeof mySchema>;

where ExtractProps is a complicated looking helper provided by ts-mongoose.
But using MyProps as the return type Promise\ leads to tsoa@v3beta crashing with the following trace

There was a problem resolving type of 'DeepExtractObjProps<T[DefinitionField]>'.
There was a problem resolving type of 'ExtractProps<typeof userSchema>'.
There was a problem resolving type of 'UserProps'.
Generate routes error.
 TypeError: Cannot read property '0' of undefined
    at /<snip>/node_modules/tsoa/dist/metadataGeneration/typeResolver.js:186:133
    at Array.map (<anonymous>)
    at TypeResolver.resolve (/<snip>/node_modules/tsoa/dist/metadataGeneration/typeResolver.js:186:83)
    at TypeResolver.getTypeAliasReference (/<snip>/node_modules/tsoa/dist/metadataGeneration/typeResolver.js:440:312)
    at TypeResolver.getReferenceType (/<snip>/node_modules/tsoa/dist/metadataGeneration/typeResolver.js:424:38)
    at TypeResolver.resolve (/<snip>/node_modules/tsoa/dist/metadataGeneration/typeResolver.js:276:34)
    at TypeResolver.resolve (/<snip>/node_modules/tsoa/dist/metadataGeneration/typeResolver.js:106:119)
    at TypeResolver.resolve (/<snip>/node_modules/tsoa/dist/metadataGeneration/typeResolver.js:266:118)
    at MethodGenerator.Generate (/<snip>/node_modules/tsoa/dist/metadataGeneration/methodGenerator.js:53:76)
    at /<snip>/node_modules/tsoa/dist/metadataGeneration/controllerGenerator.js:41:58

How can I solve this, ideally without duplicating definitions?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sundowndev picture sundowndev  Â·  4Comments

alxwrd picture alxwrd  Â·  5Comments

amcdnl picture amcdnl  Â·  6Comments

jfrconley picture jfrconley  Â·  3Comments

williamdes picture williamdes  Â·  5Comments