swagger-api / swagger-node

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

Mock mode doesn't work when using Hapi Framework, throws 500 error #277

Closed staceymoore closed 9 years ago

staceymoore commented 9 years ago

Steps to replicate:

  1. Create a new project and choose Hapi as the framework.
  2. Replace the contents of swagger.yaml with these contents:

    swagger: '2.0'
    info:
     version: "0.0.1"
     title: Mock mode test
    host: localhost
    basePath: /
    schemes:
     - http
    consumes:
     - application/json
    produces:
     - application/json
    paths:
     /weather:
       get:
         responses:
           "200":
             description: Success
             schema:
               $ref: "#/definitions/WeatherResponse"
    definitions:
     WeatherResponse:
       required:
         - message
       properties:
         message:
           type: string
  3. Start the project in mock mode swagger project start -m
  4. Attempt to access http://127.0.0.1:10010/weather and you will receive a 500 error.

Here is the debug output for the error:

Debug: internal, error 
    TypeError: Cannot call method 'toString' of undefined
    at /hello-hapi/node_modules/swagger-hapi/node_modules/swagger-node-runner/node_modules/swagger-tools/middleware/swagger-validator.js:224:41
    at /hello-hapi/node_modules/swagger-hapi/node_modules/swagger-node-runner/node_modules/swagger-tools/node_modules/lodash-compat/index.js:2090:13
    at /hello-hapi/node_modules/swagger-hapi/node_modules/swagger-node-runner/node_modules/swagger-tools/node_modules/lodash-compat/index.js:3210:15
    at baseForOwn (/hello-hapi/node_modules/swagger-hapi/node_modules/swagger-node-runner/node_modules/swagger-tools/node_modules/lodash-compat/index.js:2181:14)
    at /hello-hapi/node_modules/swagger-hapi/node_modules/swagger-node-runner/node_modules/swagger-tools/node_modules/lodash-compat/index.js:3180:18
    at baseFind (/hello-hapi/node_modules/swagger-hapi/node_modules/swagger-node-runner/node_modules/swagger-tools/node_modules/lodash-compat/index.js:2089:7)
    at Function.<anonymous> (/hello-hapi/node_modules/swagger-hapi/node_modules/swagger-node-runner/node_modules/swagger-tools/node_modules/lodash-compat/index.js:3381:16)
    at Object.res.end (/hello-hapi/node_modules/swagger-hapi/node_modules/swagger-node-runner/node_modules/swagger-tools/middleware/swagger-validator.js:223:22)
    at sendResponse (/hello-hapi/node_modules/swagger-hapi/node_modules/swagger-node-runner/node_modules/swagger-tools/middleware/swagger-router.js:229:18)
    at mockResponse (/hello-hapi/node_modules/swagger-hapi/node_modules/swagger-node-runner/node_modules/swagger-tools/middleware/swagger-router.js:279:14)

This problem only happens when using Hapi. I tested the same code with both Express and Connect; it behaved as expected in both cases and returned the mock response.

theganyo commented 9 years ago

Thanks for the report! It appears that swagger-tools isn't setting statusCode on it's mock routes and Hapi doesn't set the statusCode by default either. Somebody should do that! :) I just filed an issue on swagger-tools: https://github.com/apigee-127/swagger-tools/issues/269.

whitlockjc commented 9 years ago

swagger-tools@0.9.4 has been released with this fix.

theganyo commented 9 years ago

Thanks, @whitlockjc! @staceymoore could you update your dependencies - ensuring you have that ^ and try again?

staceymoore commented 9 years ago

:+1: Solved! @whitlockjc @theganyo Thanks for the speedy fix!

Now I can kill that extra mock server that was running on express.

theganyo commented 9 years ago

Awesome! Thanks!

whitlockjc commented 9 years ago

Happy to help.