mtennoe / swagger-typescript-codegen

A Swagger Codegenerator tailored for typescript.
Apache License 2.0
140 stars 52 forks source link

Response type not rendered when swagger responses is empty #96

Closed Shahab96 closed 4 years ago

Shahab96 commented 4 years ago

I've found that if you have a swagger file where one or more of your operations has it's responses defined as an empty object, the generated api client returns Promise with no type, resulting in a TS error.

Example:

"/someoperation" : {
      "post" : {
        "operationId" : "SomeOperation",
        "consumes" : [ "application/json" ],
        "produces" : [ "application/json" ],
        "parameters" : [ ],
        "responses" : { },
     }
}

Result:

/**
 *
 * @method
 * @name ApiClient#SomeOperation
 */
SomeOperation(parameters: {} & CommonRequestOptions): Promise;

Typescript requires that promises have some type eg Promise. In this situation the type should be voice i.e

SomeOperation(parameters: {} & CommonRequestOptions): Promise<void>;
Shahab96 commented 4 years ago

https://github.com/mtennoe/swagger-typescript-codegen/blob/master/templates/method.mustache#L70

Perhaps a check could be added here, and render void if the return type is undefined or null?

mtennoe commented 4 years ago

Yo! Hm, before we look into a fix, I am not sure this is a supported scenario in the Swagger spec? A request would have to have at least one response, for instance a 204, even for fire-and-forget requests? (I am not seeing any mention of it being either optional or required in the specs, but I would assume its mandatory)

Shahab96 commented 4 years ago

You're right. Having at least one response is mandatory.

https://swagger.io/docs/specification/describing-responses/

Thank you!