swagger-api / swagger-node

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

Incomplete JSON received on 500 internal server error. #494

Open nsisodiya opened 7 years ago

nsisodiya commented 7 years ago

When there is an error, I get incomplete JSON. Please check following.

$ curl -v http://localhost:8080/customers
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
> GET /customers HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.51.0
> Accept: */*
>
< HTTP/1.1 500 Internal Server Error
< X-Powered-By: Express
< Access-Control-Allow-Origin: *
< Content-Type: application/json
< Content-Length: 415
< ETag: W/"19f-uHyycOMnbdUwEPMuBR+/96A0/+U"
< Date: Tue, 25 Apr 2017 09:52:24 GMT
< Connection: keep-alive
<
* Excess found in a non pipelined read: excess = 1383, size = 415, maxdownload = 415, bytecount = 0
* Curl_http_done: called premature == 0
* Connection #0 to host localhost left intact
{"message":"Response validation failed: failed schema validation","code":"SCHEMA_VALIDATION_FAILED","failedValidation":true,"results":{"errors":[{"code":"INVALID_TYPE","message":"Expected type string but found type integer","path":["customers","1","mobile"]},{"code":"INVALID_TYPE","message":"Expected type string but found type integer","path":["customers","0","mobile"]}],"warnings":[]},"originalResponse":{"type"%
nsisodiya commented 7 years ago
Error: Response validation failed: failed schema validation
    at throwErrorWithCode (/Users/narendrasisodiya/Coding/Billexo/billexo-api/node_modules/swagger-tools/lib/validators.js:121:13)
    at Object.module.exports.validateAgainstSchema (/Users/narendrasisodiya/Coding/Billexo/billexo-api/node_modules/swagger-tools/lib/validators.js:176:7)
    at /Users/narendrasisodiya/Coding/Billexo/billexo-api/node_modules/swagger-tools/middleware/swagger-validator.js:141:22
    at /Users/narendrasisodiya/Coding/Billexo/billexo-api/node_modules/async/lib/async.js:356:13
    at async.forEachOf.async.eachOf (/Users/narendrasisodiya/Coding/Billexo/billexo-api/node_modules/async/lib/async.js:233:13)
    at _asyncMap (/Users/narendrasisodiya/Coding/Billexo/billexo-api/node_modules/async/lib/async.js:355:9)
    at Object.map (/Users/narendrasisodiya/Coding/Billexo/billexo-api/node_modules/async/lib/async.js:337:20)
    at validateValue (/Users/narendrasisodiya/Coding/Billexo/billexo-api/node_modules/swagger-tools/middleware/swagger-validator.js:134:11)
    at ServerResponse.res.end (/Users/narendrasisodiya/Coding/Billexo/billexo-api/node_modules/swagger-tools/middleware/swagger-validator.js:252:9)
    at ServerResponse.send (/Users/narendrasisodiya/Coding/Billexo/billexo-api/node_modules/express/lib/response.js:211:10)
    at ServerResponse.json (/Users/narendrasisodiya/Coding/Billexo/billexo-api/node_modules/express/lib/response.js:256:15)
    at Model.<anonymous> (/Users/narendrasisodiya/Coding/Billexo/billexo-api/api/controllers/customers.js:12:11)
    at bound (domain.js:280:14)
    at Model.runBound (domain.js:293:12)
    at Model.tryCatcher (/Users/narendrasisodiya/Coding/Billexo/billexo-api/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/Users/narendrasisodiya/Coding/Billexo/billexo-api/node_modules/bluebird/js/release/promise.js:512:31)

Also, I get this error log in console. My main query is - "Why I am getting incomplete JSON". because of this, I am finding difficulty in debugging.

micmro commented 7 years ago

Looks like this come from the 'swagger-validator' pipe: https://github.com/apigee-127/swagger-tools/blob/master/middleware/swagger-validator.js#L152-L163, it validates the response JSON against the Response model in your Swagger-definition.

hmaurer commented 7 years ago

Any update on this?

EugeneHerasymchuk commented 6 years ago

I am sure that you defined the format of response using for example:

200:
          description: "A list of customers"
          schema:
            $ref: "#/definitions/Customers"

and Customers should be defined as an array for example:

 Customers:
    type: "array"
    items:
      $ref: "#/definitions/Customer"
Customer:
    required:
    - "_id"
    - "name"
    properties:
      _id:
        type: "integer"
        description: "The generated unique identifier"
      name:
        type: "string"
        description: "Name of the customer"
      description:
        type: "string"
        description: "Description text" 

And take a look at your controller that is responsible for endpoint /customers. The format of the data should be the same as you defined.