Skip to content
This repository has been archived by the owner on Aug 4, 2023. It is now read-only.

validation errors are swallowed #355

Closed
seanpk opened this issue Feb 23, 2016 · 8 comments
Closed

validation errors are swallowed #355

seanpk opened this issue Feb 23, 2016 · 8 comments

Comments

@seanpk
Copy link
Contributor

seanpk commented Feb 23, 2016

if I call initializeMiddelware with a bad spec, I just get an ugly traceback, e.g.:

Error: Swagger document(s) failed validation so the server cannot start
    at /tmp/my-project/node_modules/swagger-tools/index.js:74:13
    at /tmp/my-project/node_modules/swagger-tools/lib/specs.js:1231:7
    at /tmp/my-project/node_modules/swagger-tools/lib/specs.js:1073:29
    at validateAgainstSchema (/tmp/my-project/node_modules/swagger-tools/lib/specs.js:712:14)
    at validateStructurally (/tmp/my-project/node_modules/swagger-tools/lib/specs.js:1045:3)
    at Specification.validate (/tmp/my-project/node_modules/swagger-tools/lib/specs.js:1229:3)
    at Object.initializeMiddleware (/tmp/my-project/node_modules/swagger-tools/index.js:133:17)
    at /tmp/my-project/middleware/api-specs.js:47:22
    at /tmp/my-project/node_modules/lodash/index.js:3073:15
    at baseForOwn (/tmp/my-project/node_modules/lodash/index.js:2046:14)
    at /tmp/my-project/node_modules/lodash/index.js:3043:18
    at Function.<anonymous> (/tmp/my-project/node_modules/lodash/index.js:3346:13)
    at init (/tmp/my-project/middleware/api-specs.js:44:7)
    at initConsumer (/tmp/my-project/node_modules/blueoak-server/lib/loader.js:262:37)
    at /tmp/my-project/node_modules/blueoak-server/node_modules/async/lib/async.js:181:20
    at iterate (/tmp/my-project/node_modules/blueoak-server/node_modules/async/lib/async.js:262:13)

But, if I add err.failedValidation = true; to the error condition in index.js (I can create a pull request if necessary, but I'll comment with the code snippet), I get some useful output:

API Errors:

  #/responses/NotFound: Missing required property: description
  #/responses/NotFound: Additional properties not allowed: $ref
  #/responses/InvalidRequestData: Missing required property: description
  #/responses/InvalidRequestData: Additional properties not allowed: $ref
  #/responses/InvalidApiKey: Missing required property: description
  #/responses/InvalidApiKey: Additional properties not allowed: $ref
  #/responses/CreatedClaim: Missing required property: description
  #/responses/CreatedClaim: Additional properties not allowed: $ref
  #/parameters/QueryInsuredName: Not a valid parameter definition
    #/parameters/QueryInsuredName: Missing required property: schema
    #/parameters/QueryInsuredName: Missing required property: type
  #/parameters/QueryDate: Not a valid parameter definition
    #/parameters/QueryDate: Missing required property: schema
    #/parameters/QueryDate: Missing required property: type
  #/parameters/QueryById: Not a valid parameter definition
    #/parameters/QueryById: Missing required property: schema
    #/parameters/QueryById: Missing required property: type
  #/parameters/QueryAddress: Not a valid parameter definition
    #/parameters/QueryAddress: Missing required property: schema
    #/parameters/QueryAddress: Missing required property: type
  #/parameters/PathId: Not a valid parameter definition
    #/parameters/PathId: Missing required property: schema
    #/parameters/PathId: Missing required property: type

13 errors and 0 warnings

I assume the nice output is the desired behavior ... will you publish a fix?

@seanpk
Copy link
Contributor Author

seanpk commented Feb 23, 2016

fix in index.js

//...
  args.push(function (err, results) {
    if (results && results.errors.length + _.reduce(results.apiDeclarations || [], function (count, apiDeclaration) {
      return count += (apiDeclaration ? apiDeclaration.errors.length : 0);
    }, 0) > 0) {
      err = new Error('Swagger document(s) failed validation so the server cannot start');
      err.failedValidation = true; // <--- this is the addition
      err.results = results;
    }

    debug('  Validation: %s', err ? 'failed' : 'succeeded');
//...

@whitlockjc
Copy link
Member

I think this is a duplicate of #327. If you agree, do you mind closing this?

@seanpk
Copy link
Contributor Author

seanpk commented Feb 24, 2016

@whitlockjc - thanks for the quick reply!

I had looked at #327 before, but I think there is a difference:
While it would be great to recover from a validation error programmatically (#327), it is OK for my case to ask the swagger developers to manually recover from the error by fixing their spec and restarting the server (the validation failure happens at server startup anyway).
All that I'm looking for is the validation errors to be printed to the console instead of the obtuse traceback.

As far as I can tell (and definitely for my case), adding the one line referenced in my previous comment does the trick.
i.e.:

err.failedValidation = true;

@whitlockjc
Copy link
Member

Let's keep it open then. I realize that err.failedValidation dictates some things and maybe I neglected to set it in some place.

@seanpk
Copy link
Contributor Author

seanpk commented Feb 24, 2016

ok, yeah ... in this case, a new Error is created with the message Swagger document(s) failed validation so the server cannot start, so .failedValidation needs to be added to that error before control continues to the rest of the code

@whitlockjc
Copy link
Member

Do you want to create a PR? I could help you out if need be. If not, I can whip this up pretty quickly.

seanpk pushed a commit to seanpk/swagger-tools that referenced this issue Feb 24, 2016
whitlockjc added a commit that referenced this issue Feb 24, 2016
#355 Set failedValidation in initializeMiddleware call
@whitlockjc
Copy link
Member

swagger-tools@0.10.1 is out with this fix in it. Thanks for your help.

@seanpk
Copy link
Contributor Author

seanpk commented Feb 25, 2016 via email

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants