thiagobustamante / typescript-rest-swagger

Swagger tools for typescript-rest
156 stars 57 forks source link

@Reponse/@Example extracted "examples" broken for strings #105

Open tnrich opened 4 years ago

tnrich commented 4 years ago

@ngraef I'm adding an example response to one of my Get Routes like so:

@Response<string>(200, "Response example", "example String here")

However when debugging swaggerGen, I get the following message in the console:

  typescript-rest-swagger:metadata:methods Example extracted for ExportController.formattedDNA: undefined +0ms

I get the same message when I try using the @Example decorator like this:

@Example<string>("Another Response example")
/**
   * This route returns a string of the requested sequence in the desired format (genbank, fasta, teselagenJSON)
   */
  @GET
  @Path("sequence/:format/:sequenceId")
  @Security(["USER"])
  @Response<string>(200, "Response example", "example String here")
  @Example<string>("Another Response example")
  public async formattedDNA(
    /**
     * This must be one of 
     * ```
     * fasta | genbank | json
     * ```
     */
    @PathParam("format") format: string,
    /**
     * This needs to be the id of the desired sequence
     */
    @PathParam("sequenceId") sequenceId: number
  ): Promise<string> {
    const { request: req } = this.serviceCtx;
    const [seq] = await getSeqsInFormat({
      req,
      format,
      sequenceId
    });
    return seq;
  }

It seems like it is probably not handling simple values like strings correctly? Am I doing something wrong?

Thanks

tnrich commented 4 years ago

This seems like part of a larger issue. It appears that all "examples" get automatically assigned an "application/json" key here:

//src/swagger/generator.ts
if (res.examples) {
    operation.responses[res.status]['examples'] = { 'application/json': res.examples };
}

I'm not quite sure why that would be? That seems a bit constrictive. Here's what it looks like in the generated docs: image