Skip to content

Commit

Permalink
Merge pull request #1773 from NikoRaisanen/bugfix-handle-bigint
Browse files Browse the repository at this point in the history
fix: fixed BigInt sent as json
  • Loading branch information
titanism committed Aug 15, 2023
2 parents 4691583 + 259a43f commit a62866a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/request-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -662,9 +662,12 @@ RequestBase.prototype.send = function (data) {
// merge
if (isObject_ && isObject(this._data)) {
for (const key in data) {
if (typeof data[key] == "bigint") throw new Error("Cannot serialize BigInt value to json");
if (hasOwn(data, key)) this._data[key] = data[key];
}
} else if (typeof data === 'string') {
}
else if (typeof data === 'bigint') throw new Error("Cannot send value of type BigInt");
else if (typeof data === 'string') {
// default to x-www-form-urlencoded
if (!type) this.type('form');
type = this._header['content-type'];
Expand Down
26 changes: 26 additions & 0 deletions test/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,32 @@ describe('req.send(Object) as "json"', function () {
});
});

it('should error for BigInt object', (done) => {
try {
request
.post(`${uri}/echo`)
.type('json')
.send({number: 1n})
throw new Error('Should have thrown error for object with BigInt')
} catch (error) {
assert.strictEqual(error.message, 'Cannot serialize BigInt value to json');
}
done();
});

it('should error for BigInt primitive', (done) => {
try {
request
.post(`${uri}/echo`)
.type('json')
.send(1n)
throw new Error('Should have thrown error for BigInt primitive')
} catch (error) {
assert.strictEqual(error.message, 'Cannot send value of type BigInt');
}
done();
});

describe('when called several times', () => {
it('should merge the objects', (done) => {
request
Expand Down

0 comments on commit a62866a

Please sign in to comment.