restify / restify/node-restify

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

Open
#1,791 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
10.7k
Forks
975
Avg merge
1d 6h
Merged PRs (30d)
5

Description

  • 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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the formatJSON formatter identified in the issue and reproduce the behavior with the /zero route using application/json. Check the response for 0 and false, then verify that other numeric values still work. Done means falsy JSON values are returned as their actual values rather than null.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
api
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.