tsoa not generating when given an OR operator in response

Created on 6 May 2019  路  7Comments  路  Source: lukeautry/tsoa

When I create a controller that returns

 Promise<User | null>

The swagger generated code returns

Promise<any>

I understand that type aliases are not allowed in the response, but what is the correct syntax to allow returning null?

Most helpful comment

I have exactly this problem, but with undefined. The workaround of making a union doesn't work either, because as soon as it sees undefined in the union, it blows up with:

There was a problem resolving type of 'Undefineable<Account>'. Generate routes error. Error: Unknown type: UndefinedKeyword At: /Users/davidaktary/Projects/singularity/projects/discovery/server/src/api/controllers/undefineable.d.ts:1:1. This was caused by 'T | undefined'

All 7 comments

Any updates? Is there a maintainer for this repo?

I've also tried giving another interface

<User | NoUser> still generates Promise<any>

The issue is actually in swagger, which turns this into object

However this is dealt with here

@oreporan we're actively working on fixing this. There's a new issue number (https://github.com/lukeautry/tsoa/issues/393) that we're using to track this so I'm going to close this issue. And there's already a PR out with a proposal #400. Please note that union types can not be represented in Swagger 2.0, but they can be in OpenAPI 3.0. So once PR #400 merges, you'll need change tsoa.json's "swagger"."specVersion" to be 3

@oreporan I'm happy to report that the we added our first draft of unions and intersections in v2.4.11. Please consider upgrading to it and let us know how it works for you. :) Please note: you must have config.swagger.specVersion set to 3 for this to work since oneOf and allOf are not supported in Swagger (but they are in OpenAPI 3.0).

I'm not 100% confident that we handled the Promise<User | null> case in a manner that you would like. But if you can't get it to work, please let us know and we can reopen this ticket. @WoH should be tagged since he understands that code best.

Hi everyone , I've got that problem.

TSOA (v. 2.5.11) config:

{
  "swagger": {
    "basePath": "/api/v1",
    "entryFile": "./src/main.ts",
    "noImplicitAdditionalProperties": "silently-remove-extras",
    "specVersion": 3,
    "outputDirectory": ".",
    "controllerPathGlobs": ["./src/controllers/**/*.ts"]
  },
  "routes": {
    "authenticationModule": "./src/utils/authentication.ts",
    "basePath": "/api/v1",
    "entryFile": "./src/main.ts",
    "routesDir": "./src/server",
    "middleware": "express"
  }
}

Part of controller where caused exception

@Route("/jobs")
@Tags('Jobs')
export class JobsController extends Controller {

  @Get('/{id}')
  @Security("jwt")
  public async getJob(@Path('id') jobId: string): Promise<Job | null> { // here
    return getJobById(jobId);
  }
}

And that's my output:
image

Just a reminder: The workaround is to make it a union with undefined. But yes, #479 tracks the feature enhancement to allow a union with null.

I have exactly this problem, but with undefined. The workaround of making a union doesn't work either, because as soon as it sees undefined in the union, it blows up with:

There was a problem resolving type of 'Undefineable<Account>'. Generate routes error. Error: Unknown type: UndefinedKeyword At: /Users/davidaktary/Projects/singularity/projects/discovery/server/src/api/controllers/undefineable.d.ts:1:1. This was caused by 'T | undefined'

Was this page helpful?
0 / 5 - 0 ratings

Related issues

eluft84 picture eluft84  路  6Comments

jakubzloczewski picture jakubzloczewski  路  6Comments

williamdes picture williamdes  路  5Comments

fahrradflucht picture fahrradflucht  路  4Comments

jeremyVignelles picture jeremyVignelles  路  3Comments