Skip to content

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

@ziacik

Description

@ziacik
  • Used appropriate template for the issue type
  • Searched both open and closed issues for duplicates of this issue
  • 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions