typescript-rest-swagger icon indicating copy to clipboard operation
typescript-rest-swagger copied to clipboard

Faild to generate swagger with 'type' fields

Open khotkevych opened this issue 7 years ago • 2 comments
trafficstars

For next example:

export type TypeExmaple = number[] | number[][];

export class Test {
    field: TypeExmaple;
}

Swagger generation:

@Path('/test')
@Accept('application/json')
@Produces('application/json')
export class TestSwagger {
    @POST
    submit(_test: Test): void {
    }
}

Is failing with next error:

Error generate parameter method: 'TestSwagger.submit' argument: _test TypeError: Cannot read property 'text' of undefined

If used without type:

export class Test {
    field: number[] | number[][];
}

Compilation succeed.

Is it a way to generate swagger file with type?

version used: "0.0.22"

khotkevych avatar Nov 23 '18 11:11 khotkevych

I have the similar problem if I use custum types: @Path('/linker/investors') @GET async getLinkerInvestors(): Promise<(User | VCGroup)[]> { return this.adminService.getLinkerInvestors() } It return an error .nvm/versions/node/v9.3.0/lib/node_modules/typescript-rest-swagger/dist/metadata/resolveType.js:37 throw new Error("Unknown type: " + ts.SyntaxKind[typeNode.kind]);

kosach avatar Nov 30 '18 06:11 kosach

If you look at resolveType, it seems like many types are manually supported in order to boil down to actual primitives.

I'm not very familiar with the typescript API, and didn't get far in trying to add support here. Also just a nice-to-have for my project, so will probably just shelve for now -- I'm just leaving return type off my service method signatures so typescript-rest-swagger doesn't pick them up. The only problem there is without a return type it automatically sets up a default empty 204 response.

I think a good workaround would be to have an option of whether to try and parse return type from the method signature. Then we could still type our service methods however we need to, and could use @Response for docs.

Proposed API

enableReturnAnalysis | boolean | Turn off to skip automatic response generation from method return analysis. Defaults to true.

greghart avatar Jan 26 '19 22:01 greghart