Skip to content

Commit e61eef0

Browse files
committed
Fix bigint json serialization and remove wrong null/undefined checks
1 parent cfbbcc8 commit e61eef0

16 files changed

+134
-55
lines changed

bolt-connection/src/bolt/request-message.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* limitations under the License.
1818
*/
1919

20-
import { int, internal } from 'neo4j-driver-core'
20+
import { int, internal, json } from 'neo4j-driver-core'
2121

2222
const {
2323
constants: { ACCESS_MODE_READ, FETCH_ALL },
@@ -79,7 +79,7 @@ export default class RequestMessage {
7979
return new RequestMessage(
8080
RUN,
8181
[query, parameters],
82-
() => `RUN ${query} ${JSON.stringify(parameters)}`
82+
() => `RUN ${query} ${json.stringify(parameters)}`
8383
)
8484
}
8585

@@ -131,7 +131,7 @@ export default class RequestMessage {
131131
return new RequestMessage(
132132
BEGIN,
133133
[metadata],
134-
() => `BEGIN ${JSON.stringify(metadata)}`
134+
() => `BEGIN ${json.stringify(metadata)}`
135135
)
136136
}
137137

@@ -171,7 +171,7 @@ export default class RequestMessage {
171171
RUN,
172172
[query, parameters, metadata],
173173
() =>
174-
`RUN ${query} ${JSON.stringify(parameters)} ${JSON.stringify(metadata)}`
174+
`RUN ${query} ${json.stringify(parameters)} ${json.stringify(metadata)}`
175175
)
176176
}
177177

@@ -191,13 +191,13 @@ export default class RequestMessage {
191191
*/
192192
static pull ({ stmtId = NO_STATEMENT_ID, n = FETCH_ALL } = {}) {
193193
const metadata = buildStreamMetadata(
194-
stmtId || NO_STATEMENT_ID,
194+
stmtId === null || stmtId === undefined ? NO_STATEMENT_ID : stmtId,
195195
n || FETCH_ALL
196196
)
197197
return new RequestMessage(
198198
PULL,
199199
[metadata],
200-
() => `PULL ${JSON.stringify(metadata)}`
200+
() => `PULL ${json.stringify(metadata)}`
201201
)
202202
}
203203

@@ -209,13 +209,13 @@ export default class RequestMessage {
209209
*/
210210
static discard ({ stmtId = NO_STATEMENT_ID, n = FETCH_ALL } = {}) {
211211
const metadata = buildStreamMetadata(
212-
stmtId || NO_STATEMENT_ID,
212+
stmtId === null || stmtId === undefined ? NO_STATEMENT_ID : stmtId,
213213
n || FETCH_ALL
214214
)
215215
return new RequestMessage(
216216
DISCARD,
217217
[metadata],
218-
() => `DISCARD ${JSON.stringify(metadata)}`
218+
() => `DISCARD ${json.stringify(metadata)}`
219219
)
220220
}
221221

@@ -230,7 +230,7 @@ export default class RequestMessage {
230230
return new RequestMessage(
231231
ROUTE,
232232
[routingContext, databaseName],
233-
() => `ROUTE ${JSON.stringify(routingContext)} ${databaseName}`
233+
() => `ROUTE ${json.stringify(routingContext)} ${databaseName}`
234234
)
235235
}
236236
}

bolt-connection/src/bolt/response-handler.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* See the License for the specific language governing permissions and
1717
* limitations under the License.
1818
*/
19-
import { newError } from 'neo4j-driver-core'
19+
import { newError, json } from 'neo4j-driver-core'
2020

2121
// Signature bytes for each response message type
2222
const SUCCESS = 0x70 // 0111 0000 // SUCCESS <metadata>
@@ -92,13 +92,13 @@ export default class ResponseHandler {
9292
switch (msg.signature) {
9393
case RECORD:
9494
if (this._log.isDebugEnabled()) {
95-
this._log.debug(`${this} S: RECORD ${JSON.stringify(msg)}`)
95+
this._log.debug(`${this} S: RECORD ${json.stringify(msg)}`)
9696
}
9797
this._currentObserver.onNext(payload)
9898
break
9999
case SUCCESS:
100100
if (this._log.isDebugEnabled()) {
101-
this._log.debug(`${this} S: SUCCESS ${JSON.stringify(msg)}`)
101+
this._log.debug(`${this} S: SUCCESS ${json.stringify(msg)}`)
102102
}
103103
try {
104104
const metadata = this._transformMetadata(payload)
@@ -109,7 +109,7 @@ export default class ResponseHandler {
109109
break
110110
case FAILURE:
111111
if (this._log.isDebugEnabled()) {
112-
this._log.debug(`${this} S: FAILURE ${JSON.stringify(msg)}`)
112+
this._log.debug(`${this} S: FAILURE ${json.stringify(msg)}`)
113113
}
114114
try {
115115
const error = newError(payload.message, payload.code)
@@ -125,7 +125,7 @@ export default class ResponseHandler {
125125
break
126126
case IGNORED:
127127
if (this._log.isDebugEnabled()) {
128-
this._log.debug(`${this} S: IGNORED ${JSON.stringify(msg)}`)
128+
this._log.debug(`${this} S: IGNORED ${json.stringify(msg)}`)
129129
}
130130
try {
131131
if (this._currentFailure && this._currentObserver.onError) {

bolt-connection/src/bolt/stream-observers.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* See the License for the specific language governing permissions and
1717
* limitations under the License.
1818
*/
19-
import { newError, error, Integer, Record } from 'neo4j-driver-core'
19+
import { newError, error, Integer, Record, json } from 'neo4j-driver-core'
2020
import { ALL } from './request-message'
2121
import RawRoutingTable from './routing-table-raw'
2222

@@ -268,7 +268,7 @@ class ResultStreamObserver extends StreamObserver {
268268

269269
// Extract server generated query id for use in requestMore and discard
270270
// functions
271-
if (meta.qid) {
271+
if (meta.qid !== null && meta.qid !== undefined) {
272272
this._queryId = meta.qid
273273

274274
// remove qid from metadata object
@@ -392,7 +392,7 @@ class LoginObserver extends StreamObserver {
392392

393393
onNext (record) {
394394
this.onError(
395-
newError('Received RECORD when initializing ' + JSON.stringify(record))
395+
newError('Received RECORD when initializing ' + json.stringify(record))
396396
)
397397
}
398398

@@ -429,7 +429,7 @@ class ResetObserver extends StreamObserver {
429429
this.onError(
430430
newError(
431431
'Received RECORD when resetting: received record is: ' +
432-
JSON.stringify(record),
432+
json.stringify(record),
433433
PROTOCOL_ERROR
434434
)
435435
)
@@ -500,7 +500,7 @@ class ProcedureRouteObserver extends StreamObserver {
500500
'Illegal response from router. Received ' +
501501
this._records.length +
502502
' records but expected only one.\n' +
503-
JSON.stringify(this._records),
503+
json.stringify(this._records),
504504
PROTOCOL_ERROR
505505
)
506506
)
@@ -533,7 +533,7 @@ class RouteObserver extends StreamObserver {
533533
this.onError(
534534
newError(
535535
'Received RECORD when resetting: received record is: ' +
536-
JSON.stringify(record),
536+
json.stringify(record),
537537
PROTOCOL_ERROR
538538
)
539539
)

bolt-connection/src/connection/connection-channel.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
import { Chunker, Dechunker, ChannelConfig, Channel } from '../channel'
21-
import { newError, error } from 'neo4j-driver-core'
21+
import { newError, error, json } from 'neo4j-driver-core'
2222
import Connection from './connection'
2323
import Bolt from '../bolt'
2424

@@ -247,7 +247,7 @@ export default class ChannelConnection extends Connection {
247247

248248
if (this._log.isErrorEnabled()) {
249249
this._log.error(
250-
`${this} experienced a fatal error ${JSON.stringify(this._error)}`
250+
`${this} experienced a fatal error ${json.stringify(this._error)}`
251251
)
252252
}
253253

bolt-connection/src/rediscovery/routing-table.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,14 @@
1616
* See the License for the specific language governing permissions and
1717
* limitations under the License.
1818
*/
19-
import { newError, error, Integer, int, internal } from 'neo4j-driver-core'
19+
import {
20+
newError,
21+
error,
22+
Integer,
23+
int,
24+
internal,
25+
json
26+
} from 'neo4j-driver-core'
2027

2128
const {
2229
constants: { ACCESS_MODE_WRITE: WRITE, ACCESS_MODE_READ: READ },
@@ -189,7 +196,7 @@ function parseServers (rawRoutingTable, routerAddress) {
189196
}
190197
} catch (error) {
191198
throw newError(
192-
`Unable to parse servers entry from router ${routerAddress} from addresses:\n${JSON.stringify(
199+
`Unable to parse servers entry from router ${routerAddress} from addresses:\n${json.stringify(
193200
rawRoutingTable.servers
194201
)}\nError message: ${error.message}`,
195202
PROTOCOL_ERROR
@@ -217,7 +224,7 @@ function calculateExpirationTime (rawRoutingTable, routerAddress) {
217224
return expires
218225
} catch (error) {
219226
throw newError(
220-
`Unable to parse TTL entry from router ${routerAddress} from raw routing table:\n${JSON.stringify(
227+
`Unable to parse TTL entry from router ${routerAddress} from raw routing table:\n${json.stringify(
221228
rawRoutingTable
222229
)}\nError message: ${error.message}`,
223230
PROTOCOL_ERROR

bolt-connection/test/bolt/request-message.test.js

+25-7
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
import RequestMessage from '../../src/bolt/request-message'
21-
import { internal, int } from 'neo4j-driver-core'
21+
import { internal, int, json } from 'neo4j-driver-core'
2222

2323
const {
2424
bookmark: { Bookmark },
@@ -46,7 +46,7 @@ describe('#unit RequestMessage', () => {
4646
expect(message.signature).toEqual(0x10)
4747
expect(message.fields).toEqual([query, parameters])
4848
expect(message.toString()).toEqual(
49-
`RUN ${query} ${JSON.stringify(parameters)}`
49+
`RUN ${query} ${json.stringify(parameters)}`
5050
)
5151
})
5252

@@ -103,7 +103,7 @@ describe('#unit RequestMessage', () => {
103103
expect(message.signature).toEqual(0x11)
104104
expect(message.fields).toEqual([expectedMetadata])
105105
expect(message.toString()).toEqual(
106-
`BEGIN ${JSON.stringify(expectedMetadata)}`
106+
`BEGIN ${json.stringify(expectedMetadata)}`
107107
)
108108
})
109109
})
@@ -156,7 +156,7 @@ describe('#unit RequestMessage', () => {
156156
expect(message.signature).toEqual(0x10)
157157
expect(message.fields).toEqual([query, parameters, expectedMetadata])
158158
expect(message.toString()).toEqual(
159-
`RUN ${query} ${JSON.stringify(parameters)} ${JSON.stringify(
159+
`RUN ${query} ${json.stringify(parameters)} ${json.stringify(
160160
expectedMetadata
161161
)}`
162162
)
@@ -175,7 +175,7 @@ describe('#unit RequestMessage', () => {
175175
function verify (message, signature, metadata, name) {
176176
expect(message.signature).toEqual(signature)
177177
expect(message.fields).toEqual([metadata])
178-
expect(message.toString()).toEqual(`${name} ${JSON.stringify(metadata)}`)
178+
expect(message.toString()).toEqual(`${name} ${json.stringify(metadata)}`)
179179
}
180180

181181
it('should create PULL message', () => {
@@ -195,6 +195,15 @@ describe('#unit RequestMessage', () => {
195195
)
196196
})
197197

198+
it('should create PULL message with qid=0n and n', () => {
199+
verify(
200+
RequestMessage.pull({ stmtId: 0n, n: 1023 }),
201+
0x3f,
202+
{ n: int(1023), qid: int(0n) },
203+
'PULL'
204+
)
205+
})
206+
198207
it('should create DISCARD message', () => {
199208
verify(RequestMessage.discard(), 0x2f, { n: int(-1) }, 'DISCARD')
200209
})
@@ -216,6 +225,15 @@ describe('#unit RequestMessage', () => {
216225
'DISCARD'
217226
)
218227
})
228+
229+
it('should create DISCARD message with qid=0n and n', () => {
230+
verify(
231+
RequestMessage.discard({ stmtId: 0n, n: 1023 }),
232+
0x2f,
233+
{ n: int(1023), qid: int(0n) },
234+
'DISCARD'
235+
)
236+
})
219237
})
220238

221239
describe('BoltV4.3', () => {
@@ -228,7 +246,7 @@ describe('#unit RequestMessage', () => {
228246
expect(message.signature).toEqual(0x66)
229247
expect(message.fields).toEqual([requestContext, database])
230248
expect(message.toString()).toEqual(
231-
`ROUTE ${JSON.stringify(requestContext)} ${database}`
249+
`ROUTE ${json.stringify(requestContext)} ${database}`
232250
)
233251
})
234252

@@ -237,7 +255,7 @@ describe('#unit RequestMessage', () => {
237255

238256
expect(message.signature).toEqual(0x66)
239257
expect(message.fields).toEqual([{}, null])
240-
expect(message.toString()).toEqual(`ROUTE ${JSON.stringify({})} ${null}`)
258+
expect(message.toString()).toEqual(`ROUTE ${json.stringify({})} ${null}`)
241259
})
242260
})
243261
})

bolt-connection/test/bolt/stream-observer.test.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
ProcedureRouteObserver
2424
} from '../../src/bolt/stream-observers'
2525
import { RawRoutingTable } from '../../src/bolt'
26-
import { error, newError, Record } from 'neo4j-driver-core'
26+
import { error, newError, Record, json } from 'neo4j-driver-core'
2727

2828
const { PROTOCOL_ERROR } = error
2929

@@ -267,7 +267,7 @@ describe('#unit RouteObserver', () => {
267267
const record = new Record(['a'], ['b'])
268268
const expectedError = newError(
269269
'Received RECORD when resetting: received record is: ' +
270-
JSON.stringify(record),
270+
json.stringify(record),
271271
PROTOCOL_ERROR
272272
)
273273

@@ -287,7 +287,7 @@ describe('#unit RouteObserver', () => {
287287
const record = new Record(['a'], ['b'])
288288
const expectedErrorMessage =
289289
'Received RECORD when resetting: received record is: ' +
290-
JSON.stringify(record)
290+
json.stringify(record)
291291

292292
newRouteObserver({
293293
onError: null,
@@ -342,7 +342,7 @@ describe('#unit ProcedureRouteObserver', () => {
342342
let onErrorCalled = false
343343
const expectedError = newError(
344344
'Illegal response from router. Received 0 records but expected only one.\n' +
345-
JSON.stringify([]),
345+
json.stringify([]),
346346
PROTOCOL_ERROR
347347
)
348348
const observer = newRouteObserver({
@@ -362,7 +362,7 @@ describe('#unit ProcedureRouteObserver', () => {
362362
let onProtocolErrorCalled = false
363363
const expectedErrorMessage =
364364
'Illegal response from router. Received 0 records but expected only one.\n' +
365-
JSON.stringify([])
365+
json.stringify([])
366366

367367
newRouteObserver({
368368
onError: null,
@@ -380,7 +380,7 @@ describe('#unit ProcedureRouteObserver', () => {
380380
const record = new Record(['a'], ['b'])
381381
const expectedError = newError(
382382
'Illegal response from router. Received 2 records but expected only one.\n' +
383-
JSON.stringify([record, record]),
383+
json.stringify([record, record]),
384384
PROTOCOL_ERROR
385385
)
386386
const observer = newRouteObserver({
@@ -403,7 +403,7 @@ describe('#unit ProcedureRouteObserver', () => {
403403
const record = new Record(['a'], ['b'])
404404
const expectedErrorMessage =
405405
'Illegal response from router. Received 2 records but expected only one.\n' +
406-
JSON.stringify([record, record])
406+
json.stringify([record, record])
407407

408408
const observer = newRouteObserver({
409409
onError: null,

0 commit comments

Comments
 (0)