Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Cannot set property 'examples' of undefined #735

Closed
2 of 4 tasks
WWL-MikeRoberts opened this issue Jun 17, 2020 · 2 comments · Fixed by #765
Closed
2 of 4 tasks

TypeError: Cannot set property 'examples' of undefined #735

WWL-MikeRoberts opened this issue Jun 17, 2020 · 2 comments · Fixed by #765
Labels
bug good first issue This issue could be an easy PR for those looking to help contribute

Comments

@WWL-MikeRoberts
Copy link

Sorting

  • I'm submitting a ...

    • bug report
    • feature request
    • support request
  • I confirm that I

    • used the search to make sure that a similar issue hasn't already been submit

Expected Behavior

swagger.json is generated?

Current Behavior

> tsoa swagger

Generate swagger error.
 TypeError: Cannot set property 'examples' of undefined
    at {root}\node_modules\tsoa\dist\swagger\specGenerator3.js:290:92
    at Array.forEach (<anonymous>)
    at SpecGenerator3.buildOperation ({root}\node_modules\tsoa\dist\swagger\specGenerator3.js:273:26)
    at SpecGenerator3.buildMethod ({root}\node_modules\tsoa\dist\swagger\specGenerator3.js:244:60)
    at {root}\node_modules\tsoa\dist\swagger\specGenerator3.js:237:23
    at Array.forEach (<anonymous>)
    at {root}\node_modules\tsoa\dist\swagger\specGenerator3.js:232:18
    at Array.forEach (<anonymous>)
    at SpecGenerator3.buildPaths ({root}\node_modules\tsoa\dist\swagger\specGenerator3.js:227:35)
    at SpecGenerator3.GetSpec ({root}\node_modules\tsoa\dist\swagger\specGenerator3.js:54:25)

Possible Solution

Change line 299 from

(swaggerResponses[res.name].content || {})['application/json']['examples'] = examples;

to

(swaggerResponses[res.name].content || {"application/json": {}})['application/json']['examples'] = examples;

or maybe throw an exception when this if check on line 287 fails

      if (res.schema && !isVoidType(res.schema)) {

Steps to Reproduce

not exactly sure what steps are needed.

Context (Environment)

Version of the library: 3.2.1
Version of NodeJS: 13.12.0

  • Confirm you were using yarn not npm: [ ]

Detailed Description

This issue appears to have been introduced in commit 75149bc when an if check was put around adding a content property to swaggerResponses[res.name] in the buildOperation function in SpecGenerator3. I don't know if there is something else wrong with my setup that is causing res.schema to not be set but this looks like a valid scenario from what I can tell.

protected buildOperation(controllerName: string, method: Tsoa.Method): Swagger.Operation3 {
    const swaggerResponses: { [name: string]: Swagger.Response3 } = {};

    method.responses.forEach((res: Tsoa.Response) => {
      swaggerResponses[res.name] = {
        description: res.description,
      };
      if (res.schema && !isVoidType(res.schema)) {
        swaggerResponses[res.name].content = {
          'application/json': {
            schema: this.getSwaggerType(res.schema),
          } as Swagger.Schema3,
        };
      }
      if (res.examples) {
        const examples = res.examples.reduce<Swagger.Example['examples']>((acc, ex, currentIndex) => {
          return { ...acc, [`Example ${currentIndex + 1}`]: { value: ex } };
        }, {});
        /* tslint:disable:no-string-literal */
        (swaggerResponses[res.name].content || {})['application/json']['examples'] = examples;
      }
    });

    return {
      operationId: this.getOperationId(method.name),
      responses: swaggerResponses,
    };
  }

If swaggerResponses[res.name].content is not set (due to res.schema not being set) then an empty object is used and since that empty object does not have an 'application/json' property an exception is thrown trying to set it's examples property.

Breaking change?

@WoH WoH added bug good first issue This issue could be an easy PR for those looking to help contribute labels Jun 24, 2020
@github-actions
Copy link

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days

@github-actions github-actions bot added the Stale label Jul 25, 2020
@WoH WoH removed the Stale label Jul 25, 2020
@WoH
Copy link
Collaborator

WoH commented Jul 25, 2020

I think setting an example on a void response isn't a good thing to do, but we should avoid crashing. I'll take a look.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug good first issue This issue could be an easy PR for those looking to help contribute
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants