node-restify icon indicating copy to clipboard operation
node-restify copied to clipboard

res.send(200, 0) sends 'null' instead of 0 or other falsy values with application/json

Open ziacik opened this issue 5 years ago • 4 comments

  • [x] Used appropriate template for the issue type
  • [x] Searched both open and closed issues for duplicates of this issue
  • [x] Title adequately and concisely reflects the feature or the bug

Bug Report

Restify Version

8.3.3

Node.js Version

11.15.0

Expected behaviour

The client should get 0 when res.contentType = 'application/json'; res.send(200, 0) is done and false when res.contentType = 'application/json'; res.send(200, false) is done.

Actual behaviour

The client gets null.

Repro case

server.get('/zero', function(req, res, next) {
  res.contentType = 'application/json';
  res.send(200, 0);
  next();
});

Cause

var data = body ? JSON.stringify(body) : 'null'; in formatJSON formatter returns null if any falsy object is in the body. Now 0 isn't a valid JSON object, but it is a valid JSON value and the parsers are able to parse it. Sending any other number actually send the number, not the null, so I think sending actual 0 would be consistent.

Are you willing and able to fix this?

Yes.

ziacik avatar Jun 22 '19 14:06 ziacik

Hi there, I am waiting for this fix to be merged. Can anyone tell me when it would happen?

jakednoh avatar Nov 07 '19 21:11 jakednoh

Same here, waiting for this to be fixed

ToniIltanen avatar Jan 20 '20 10:01 ToniIltanen

It doesn't make sense to return null instead of 0. I have an endpoint return the count size, it might be 0. Anyone know a fix? Thanks.

lenaw avatar Jul 07 '21 14:07 lenaw

It doesn't make sense to return null instead of 0. I have an endpoint return the count size, it might be 0. Anyone know a fix? Thanks.

Don't know if you still have it, but I return an object instead { count: result } instead. Not ideal, but helps

luisfix avatar Jun 17 '22 13:06 luisfix