Tsoa: TSOA - @Example does not allow require json file

Created on 14 May 2019  路  3Comments  路  Source: lukeautry/tsoa

I am trying to auto-generate my api documentation as much as possible. Using @Example annotation, I believe it would be good to do the following for example :

@Example<IApiResponse>(require('path_to_json_file'))

`Currently even when doing something as below it does not allow to pass an object and for the documentation to be auto-generated:

@Example<IApiResponse>({ apiVersion: 'test', context: [], data: A})

Error in console :

`> tsoa swagger

Generate swagger error.
TypeError: Cannot read property 'forEach' of undefined
at MethodGenerator.getExamplesValue (/home/anar/dev/repositories/flexible-platform/filters-api/node_modules/tsoa/dist/metadataGeneration/methodGenerator.js:176:29)
at MethodGenerator.getMethodSuccessExamples (/home/anar/dev/repositories/flexible-platform/filters-api/node_modules/tsoa/dist/metadataGeneration/methodGenerator.js:169:21)
at MethodGenerator.getMethodSuccessResponse (/home/anar/dev/repositories/flexible-platform/filters-api/node_modules/tsoa/dist/metadataGeneration/methodGenerator.js:144:29)
at MethodGenerator.Generate (/home/anar/dev/repositories/flexible-platform/filters-api/node_modules/tsoa/dist/metadataGeneration/methodGenerator.js:34:29)
at /home/anar/dev/repositories/flexible-platform/filters-api/node_modules/tsoa/dist/metadataGeneration/controllerGenerator.js:40:58
at Array.map ()
at ControllerGenerator.buildMethods (/home/anar/dev/repositories/flexible-platform/filters-api/node_modules/tsoa/dist/metadataGeneration/controllerGenerator.js:40:14)
at ControllerGenerator.Generate (/home/anar/dev/repositories/flexible-platform/filters-api/node_modules/tsoa/dist/metadataGeneration/controllerGenerator.js:29:27)
at /home/anar/dev/repositories/flexible-platform/filters-api/node_modules/tsoa/dist/metadataGeneration/metadataGenerator.js:59:58
`

Most helpful comment

works fine when I do this manually:
Note that two of these were enums and had to match that contract.
_Also note that this would not work if I imported the model. I had to copy and paste it to every place I called @Example._

    @Example<IResponse>({
    kindOfName: "",
    stuffId: 0,
    thingName: "things",
    data: {
      nameSpace: "woot",
      ids: [
        0
      ],
      type: "whistles",
      someOtherIds: [
        0
      ],
      channels: [
        "more:{someId}", "another:{wootId}", "everything:{Idfun}"
      ]
    }
  })

All 3 comments

Worked for me for a few tries then just died with the same error and won't work again.
Hope this helps!

@Example(ResponseModel)
public async create(

export const ResponseModel = {
    kindOfName: "",
    stuffId: 0,
    thingName: "things",
    data: {
      nameSpace: "woot",
      ids: [
        0
      ],
      type: "whistles",
      someOtherIds: [
        0
      ],
      channels: [
        "more:{someId}", "another:{wootId}", "everything:{Idfun}"
      ]
    }
  }
  • (Windows 10 / Visual Code / git bash)

works fine when I do this manually:
Note that two of these were enums and had to match that contract.
_Also note that this would not work if I imported the model. I had to copy and paste it to every place I called @Example._

    @Example<IResponse>({
    kindOfName: "",
    stuffId: 0,
    thingName: "things",
    data: {
      nameSpace: "woot",
      ids: [
        0
      ],
      type: "whistles",
      someOtherIds: [
        0
      ],
      channels: [
        "more:{someId}", "another:{wootId}", "everything:{Idfun}"
      ]
    }
  })

Yeah, I think this is sort of just by design - the way tsoa works is to analyze the actual code wrapped inside of the Example decorator; a require is a dynamic runtime thing and tsoa won't try to resolve a JSON file and start to resolve it. Closing this for now, but feel free to open a PR if you have an idea of how to better handle it.

Was this page helpful?
0 / 5 - 0 ratings