Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fixed BigInt sent as json #1773

Merged
merged 1 commit into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/request-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -666,9 +666,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