swagger-api / swagger-node

Swagger module for node.js
http://swagger.io
Apache License 2.0
3.97k stars 585 forks source link

Hello World Endpoint throwing Exception #348

Closed BigMrSunshine closed 8 years ago

BigMrSunshine commented 8 years ago

I followed the instructions on the quick start page, selecting express as the framework.

When I hit the endpoint with curl, I get a truncated json response message:

{"message":"Res

The error below shows up on the console.

Thanks!

SyntaxError: Response validation failed: value expected to be an array/object but is not
    at Object.parse (native)
    at validateValue (/hdd/home/cpetty/projects/swagger/hello-world/node_modules/swagger-tools/middleware/swagger-validator.js:127:20)
    at ServerResponse.res.end (/hdd/home/cpetty/projects/swagger/hello-world/node_modules/swagger-tools/middleware/swagger-validator.js:254:9)
    at ServerResponse.send (/hdd/home/cpetty/projects/swagger/hello-world/node_modules/express/lib/response.js:204:10)
    at ServerResponse.json (/hdd/home/cpetty/projects/swagger/hello-world/node_modules/express/lib/response.js:249:15)
    at hello (/hdd/home/cpetty/projects/swagger/hello-world/api/controllers/hello_world.js:43:7)
    at swaggerRouter (/hdd/home/cpetty/projects/swagger/hello-world/node_modules/swagger-tools/middleware/swagger-router.js:407:20)
    at swagger_router (/hdd/home/cpetty/projects/swagger/hello-world/node_modules/swagger-node-runner/fittings/swagger_router.js:31:5)
    at null.<anonymous> (/hdd/home/cpetty/projects/swagger/hello-world/node_modules/bagpipes/lib/bagpipes.js:171:7)
    at bound (domain.js:280:14)
BigMrSunshine commented 8 years ago

I did add some logging in swagger-validator when the validation fails:

`

  if (isModel) {
    if (_.isString(val)) {
      try {
        val = JSON.parse(val);
      } catch (err) {
        console.log("Petty - val: " + val);
        console.log("Petty - err: " + err);
        err.failedValidation = true;
        err.message = 'Value expected to be an array/object but is not';

        throw err;
      }
    }`
Petty - val: Hello, Scott!
Petty - err: SyntaxError: Unexpected token H

Thanks

theganyo commented 8 years ago

@BigMrSunshine -

It looks like the template is actually wrong and some new validation logic in a library picked it up. Quick fix, change your swagger.yaml to have this:

definitions:
  HelloWorldResponse:
    type: string

Instead of this:

definitions:
  HelloWorldResponse:
    required:
      - message
    properties:
      message:
        type: string
BigMrSunshine commented 8 years ago

That worked! I don't know the etiquette around this stuff. Should I close it? Fork and submit pull request. I'm kind of new to the github community. Thanks

webron commented 8 years ago

If you find something wrong with the doc and want to fix it, then yeah, submitting a PR would be greatly appreciated.

dreallday commented 8 years ago

@theganyo Thank you! This worked perfectly.

BigMrSunshine commented 8 years ago

@theganyo The pattern, of describing a simple string response as a message object is used several places in several templates. Are we sure this new validation logic is valid and the templates should be changed (via a PR) as you described above?

fehguy commented 8 years ago

updated the templates

BigMrSunshine commented 8 years ago

Thanks!