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

TS type aliases are not supported #204

Closed
erykwarren opened this issue Feb 24, 2018 · 15 comments
Closed

TS type aliases are not supported #204

erykwarren opened this issue Feb 24, 2018 · 15 comments

Comments

@erykwarren
Copy link

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)
@JakeStoeffler
Copy link

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

@Superd22
Copy link

Can reproduce.

@Esya
Copy link

Esya commented May 18, 2018

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

@fbarbare
Copy link

fbarbare commented Nov 6, 2018

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

@lukeautry

@simllll
Copy link
Contributor

simllll commented Jul 10, 2019

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

@dgreene1
Copy link
Collaborator

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

@github-actions
Copy link

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 github-actions bot added the Stale label Sep 13, 2019
@dgreene1 dgreene1 removed the Stale label Sep 13, 2019
This was referenced Sep 28, 2019
@WoH WoH added this to the 3.x milestone Oct 8, 2019
@github-actions
Copy link

github-actions bot commented Nov 8, 2019

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 github-actions bot added the Stale label Nov 8, 2019
@dgreene1 dgreene1 added help wanted and removed Stale labels Nov 8, 2019
@dgreene1 dgreene1 closed this as completed Nov 8, 2019
@dgreene1 dgreene1 reopened this Nov 8, 2019
@dgreene1
Copy link
Collaborator

dgreene1 commented Nov 8, 2019

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

@dgreene1
Copy link
Collaborator

dgreene1 commented Jan 8, 2020

@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. 🎉 🎈 🥇

@davebgreen
Copy link

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<IotTemplate[]> {
blah blah
}
Error: @path('token') Can't support 'refObject' type.

Cheers,
Dave

@WoH
Copy link
Collaborator

WoH commented Mar 14, 2020

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.

@davebgreen
Copy link

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?

@WoH
Copy link
Collaborator

WoH commented Mar 15, 2020

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

@arciel
Copy link

arciel commented Apr 8, 2020

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<MyProps> in a controller route
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?

@WoH WoH closed this as completed Apr 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants