mulesoft-labs / osprey-method-handler

Middleware for validating requests and responses based on a RAML method object
Other
16 stars 16 forks source link

acceptHandler: (TypeError: Cannot convert undefined or null to object) #35

Closed gb-jlofton closed 6 years ago

gb-jlofton commented 6 years ago

While updating dependencies I ran into an issue going from version 0.4.1 --> 0.5.0.

It looks like in the latest version in the acceptHandler it requires that responses contain a body. Where as previous versions did not require a body, is this an intended change?

Object.keys(responses || {})
    .filter(function (code) {
      return code >= 200 && code < 300
    })
    .forEach(function (code) {
      var response = responses[code]
      var body = response ? response.body : {}

      Object.keys(body).forEach(function (type) {
        accepts[type] = true
      })
    })

Root Cause This line in particular is assuming if there is a response that there is a body nested within that response, and if there is not a body in the response, the body variable is set to null or undefined

var body = response ? response.body : {}

and then the body gets here and then fails

Object.keys(body).forEach(function (type) {

Problem

/Users/jlofton/Desktop/goBalto/bloodhound/node_modules/osprey-method-handler/osprey-method-handler.js:167
      Object.keys(body).forEach(function (type) {
             ^

TypeError: Cannot convert undefined or null to object
    at Function.keys (<anonymous>)
    at /Users/jlofton/Desktop/goBalto/bloodhound/node_modules/osprey-method-handler/osprey-method-handler.js:167:14
    at Array.forEach (<anonymous>)
    at acceptsHandler (/Users/jlofton/Desktop/goBalto/bloodhound/node_modules/osprey-method-handler/osprey-method-handler.js:163:6)
    at ospreyMethodHandler (/Users/jlofton/Desktop/goBalto/bloodhound/node_modules/osprey-method-handler/osprey-method-handler.js:127:21)
    at /Users/jlofton/Desktop/goBalto/bloodhound/node_modules/osprey/lib/server.js:30:12
    at /Users/jlofton/Desktop/goBalto/bloodhound/node_modules/osprey-resources/osprey-resources.js:58:20
    at Array.forEach (<anonymous>)

Suggested Fix The previous version of this line works

var body = response && response.body || {}

Again not sure if this is an intended change or not, thanks!

jstoiko commented 6 years ago

Fixed in v0.12.1