Skip to content

Commit a62866a

Browse files
authoredAug 15, 2023
Merge pull request #1773 from NikoRaisanen/bugfix-handle-bigint
fix: fixed BigInt sent as json
2 parents 4691583 + 259a43f commit a62866a

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed
 

‎src/request-base.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -662,9 +662,12 @@ RequestBase.prototype.send = function (data) {
662662
// merge
663663
if (isObject_ && isObject(this._data)) {
664664
for (const key in data) {
665+
if (typeof data[key] == "bigint") throw new Error("Cannot serialize BigInt value to json");
665666
if (hasOwn(data, key)) this._data[key] = data[key];
666667
}
667-
} else if (typeof data === 'string') {
668+
}
669+
else if (typeof data === 'bigint') throw new Error("Cannot send value of type BigInt");
670+
else if (typeof data === 'string') {
668671
// default to x-www-form-urlencoded
669672
if (!type) this.type('form');
670673
type = this._header['content-type'];

‎test/json.js

+26
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,32 @@ describe('req.send(Object) as "json"', function () {
119119
});
120120
});
121121

122+
it('should error for BigInt object', (done) => {
123+
try {
124+
request
125+
.post(`${uri}/echo`)
126+
.type('json')
127+
.send({number: 1n})
128+
throw new Error('Should have thrown error for object with BigInt')
129+
} catch (error) {
130+
assert.strictEqual(error.message, 'Cannot serialize BigInt value to json');
131+
}
132+
done();
133+
});
134+
135+
it('should error for BigInt primitive', (done) => {
136+
try {
137+
request
138+
.post(`${uri}/echo`)
139+
.type('json')
140+
.send(1n)
141+
throw new Error('Should have thrown error for BigInt primitive')
142+
} catch (error) {
143+
assert.strictEqual(error.message, 'Cannot send value of type BigInt');
144+
}
145+
done();
146+
});
147+
122148
describe('when called several times', () => {
123149
it('should merge the objects', (done) => {
124150
request

0 commit comments

Comments
 (0)
Please sign in to comment.