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

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

Open tnrich opened this issue 6 years ago • 1 comments

@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 avatar Feb 14 '20 19:02 tnrich

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

tnrich avatar Feb 14 '20 20:02 tnrich