File tree 2 files changed +28
-1
lines changed
2 files changed +28
-1
lines changed Original file line number Diff line number Diff line change @@ -662,7 +662,8 @@ RequestBase.prototype.send = function (data) {
662
662
// merge
663
663
if ( isObject_ && isObject ( this . _data ) ) {
664
664
for ( const key in data ) {
665
- if ( typeof data [ key ] == "bigint" ) throw new Error ( "Cannot serialize BigInt value to json" ) ;
665
+ if ( typeof data [ key ] == 'bigint' && ! data [ key ] . toJSON )
666
+ throw new Error ( 'Cannot serialize BigInt value to json' ) ;
666
667
if ( hasOwn ( data , key ) ) this . _data [ key ] = data [ key ] ;
667
668
}
668
669
}
Original file line number Diff line number Diff line change @@ -132,6 +132,32 @@ describe('req.send(Object) as "json"', function () {
132
132
done ( ) ;
133
133
} ) ;
134
134
135
+ describe ( 'when BigInts have a .toJSON property' , function ( ) {
136
+ before ( function ( ) {
137
+ // eslint-disable-next-line node/no-unsupported-features/es-builtins
138
+ BigInt . prototype . toJSON = function ( ) {
139
+ return this . toString ( ) ;
140
+ } ;
141
+ } ) ;
142
+
143
+ it ( 'should accept BigInt properties' , ( done ) => {
144
+ request
145
+ . post ( `${ uri } /echo` )
146
+ . send ( { number : 1n } )
147
+ . end ( ( error , res ) => {
148
+ res . should . be . json ( ) ;
149
+ res . text . should . equal ( '{"number":"1"}' ) ;
150
+ done ( ) ;
151
+ } ) ;
152
+ } ) ;
153
+
154
+ after ( function ( ) {
155
+ // eslint-disable-next-line node/no-unsupported-features/es-builtins
156
+ delete BigInt . prototype . toJSON ;
157
+ } ) ;
158
+ } ) ;
159
+
160
+
135
161
it ( 'should error for BigInt primitive' , ( done ) => {
136
162
try {
137
163
request
You can’t perform that action at this time.
0 commit comments