Skip to content

Commit dca78b3

Browse files
bigmontzrobsdedude
andauthored
Validate the ZoneId of the DateTime with ZoneId (#961)
The validation of the DateTime was only being done in the new patched protocol while unpacking the struct. This changes force any new DateTime with ZoneID to have a valid ZoneId. This changes also treats struct unpacking errors and defers the occurred to the moment the object is manipulate. For instance, a DateTime with invalid ZoneId returned in a Record will not break the records consumption until any code try to interacts with the broken DateTime. Co-authored-by: Robsdedude <dev@rouvenbauer.de>
1 parent 551f51c commit dca78b3

19 files changed

+179
-71
lines changed

packages/bolt-connection/src/bolt/transformer.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
*/
1919

2020
import { structure } from '../packstream'
21+
import { internal } from 'neo4j-driver-core'
22+
23+
const { util } = internal
2124

2225
/**
2326
* Class responsible for applying the expected {@link TypeTransformer} to
@@ -43,11 +46,15 @@ export default class Transformer {
4346
* @returns {<T>|structure.Structure} The driver object or the structure if the transformer was not found.
4447
*/
4548
fromStructure (struct) {
46-
if (struct instanceof structure.Structure && this._transformersPerSignature.has(struct.signature)) {
47-
const { fromStructure } = this._transformersPerSignature.get(struct.signature)
48-
return fromStructure(struct)
49+
try {
50+
if (struct instanceof structure.Structure && this._transformersPerSignature.has(struct.signature)) {
51+
const { fromStructure } = this._transformersPerSignature.get(struct.signature)
52+
return fromStructure(struct)
53+
}
54+
return struct
55+
} catch (error) {
56+
return util.createBrokenObject(error)
4957
}
50-
return struct
5158
}
5259

5360
/**

packages/bolt-connection/test/bolt/__snapshots__/bolt-protocol-v5x0.test.js.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ exports[`#unit BoltProtocolV5x0 .unpack() should not unpack with wrong size (Nod
3636

3737
exports[`#unit BoltProtocolV5x0 .unpack() should not unpack with wrong size (Node with more fields) 1`] = `"Wrong struct size for Node, expected 4 but was 5"`;
3838

39-
exports[`#unit BoltProtocolV5x0 .unpack() should not unpack with wrong size (Path with less fields) 1`] = `"Wrong struct size for Node, expected 4 but was 3"`;
39+
exports[`#unit BoltProtocolV5x0 .unpack() should not unpack with wrong size (Path with less fields) 1`] = `"Wrong struct size for Path, expected 3 but was 2"`;
4040

41-
exports[`#unit BoltProtocolV5x0 .unpack() should not unpack with wrong size (Path with more fields) 1`] = `"Wrong struct size for Node, expected 4 but was 3"`;
41+
exports[`#unit BoltProtocolV5x0 .unpack() should not unpack with wrong size (Path with more fields) 1`] = `"Wrong struct size for Path, expected 3 but was 4"`;
4242

4343
exports[`#unit BoltProtocolV5x0 .unpack() should not unpack with wrong size (Point with less fields) 1`] = `"Wrong struct size for Point2D, expected 3 but was 2"`;
4444

packages/bolt-connection/test/bolt/bolt-protocol-v1.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,8 @@ describe('#unit BoltProtocolV1', () => {
568568

569569
buffer.reset()
570570

571-
expect(() => protocol.unpack(buffer)).toThrowErrorMatchingSnapshot()
571+
const unpacked = protocol.unpack(buffer)
572+
expect(() => unpacked instanceof structure.Structure).toThrowErrorMatchingSnapshot()
572573
})
573574

574575
it.each([
@@ -580,7 +581,7 @@ describe('#unit BoltProtocolV1', () => {
580581
['Date', new structure.Structure(0x44, [1])],
581582
['LocalDateTime', new structure.Structure(0x64, [1, 2])],
582583
['DateTimeWithZoneOffset', new structure.Structure(0x46, [1, 2, 3])],
583-
['DateTimeWithZoneId', new structure.Structure(0x66, [1, 2, 'America/Sao Paulo'])]
584+
['DateTimeWithZoneId', new structure.Structure(0x66, [1, 2, 'America/Sao_Paulo'])]
584585
])('should unpack future structs as structs (%s)', (_, struct) => {
585586
const buffer = alloc(256)
586587
const protocol = new BoltProtocolV1(

packages/bolt-connection/test/bolt/bolt-protocol-v2.test.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ describe('#unit BoltProtocolV2', () => {
154154
['Time', new Time(1, 1, 1, 1, 1)],
155155
['Date', new Date(1, 1, 1)],
156156
['LocalDateTime', new LocalDateTime(1, 1, 1, 1, 1, 1, 1)],
157-
['DateTimeWithZoneId', new DateTime(1, 1, 1, 1, 1, 1, 1, undefined, 'America/Sao Paulo')],
157+
['DateTimeWithZoneId', new DateTime(1, 1, 1, 1, 1, 1, 1, undefined, 'America/Sao_Paulo')],
158158
['DateTime', new DateTime(1, 1, 1, 1, 1, 1, 1, 1)],
159159
['Point2D', new Point(1, 1, 1)],
160160
['Point3D', new Point(1, 1, 1, 1)]
@@ -380,7 +380,7 @@ describe('#unit BoltProtocolV2', () => {
380380
],
381381
[
382382
'DateTimeWithZoneId with more fields',
383-
new structure.Structure(0x66, [1, 2, 'America/Sao Paulo', 'Brasil'])
383+
new structure.Structure(0x66, [1, 2, 'America/Sao_Paulo', 'Brasil'])
384384
]
385385
])('should not unpack with wrong size (%s)', (_, struct) => {
386386
const buffer = alloc(256)
@@ -396,7 +396,8 @@ describe('#unit BoltProtocolV2', () => {
396396

397397
buffer.reset()
398398

399-
expect(() => protocol.unpack(buffer)).toThrowErrorMatchingSnapshot()
399+
const unpacked = protocol.unpack(buffer)
400+
expect(() => unpacked instanceof structure.Structure).toThrowErrorMatchingSnapshot()
400401
})
401402

402403
it.each([
@@ -442,8 +443,8 @@ describe('#unit BoltProtocolV2', () => {
442443
],
443444
[
444445
'DateTimeWithZoneId',
445-
new structure.Structure(0x66, [1, 2, 'America/Sao Paulo']),
446-
new DateTime(1970, 1, 1, 0, 0, 1, 2, undefined, 'America/Sao Paulo')
446+
new structure.Structure(0x66, [1, 2, 'America/Sao_Paulo']),
447+
new DateTime(1970, 1, 1, 0, 0, 1, 2, undefined, 'America/Sao_Paulo')
447448
]
448449
])('should unpack spatial types and temporal types (%s)', (_, struct, object) => {
449450
const buffer = alloc(256)

packages/bolt-connection/test/bolt/bolt-protocol-v3.test.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ describe('#unit BoltProtocolV3', () => {
359359
['Time', new Time(1, 1, 1, 1, 1)],
360360
['Date', new Date(1, 1, 1)],
361361
['LocalDateTime', new LocalDateTime(1, 1, 1, 1, 1, 1, 1)],
362-
['DateTimeWithZoneId', new DateTime(1, 1, 1, 1, 1, 1, 1, undefined, 'America/Sao Paulo')],
362+
['DateTimeWithZoneId', new DateTime(1, 1, 1, 1, 1, 1, 1, undefined, 'America/Sao_Paulo')],
363363
['DateTime', new DateTime(1, 1, 1, 1, 1, 1, 1, 1)],
364364
['Point2D', new Point(1, 1, 1)],
365365
['Point3D', new Point(1, 1, 1, 1)]
@@ -585,7 +585,7 @@ describe('#unit BoltProtocolV3', () => {
585585
],
586586
[
587587
'DateTimeWithZoneId with more fields',
588-
new structure.Structure(0x66, [1, 2, 'America/Sao Paulo', 'Brasil'])
588+
new structure.Structure(0x66, [1, 2, 'America/Sao_Paulo', 'Brasil'])
589589
]
590590
])('should not unpack with wrong size (%s)', (_, struct) => {
591591
const buffer = alloc(256)
@@ -601,7 +601,8 @@ describe('#unit BoltProtocolV3', () => {
601601

602602
buffer.reset()
603603

604-
expect(() => protocol.unpack(buffer)).toThrowErrorMatchingSnapshot()
604+
const unpacked = protocol.unpack(buffer)
605+
expect(() => unpacked instanceof structure.Structure).toThrowErrorMatchingSnapshot()
605606
})
606607

607608
it.each([
@@ -647,8 +648,8 @@ describe('#unit BoltProtocolV3', () => {
647648
],
648649
[
649650
'DateTimeWithZoneId',
650-
new structure.Structure(0x66, [1, 2, 'America/Sao Paulo']),
651-
new DateTime(1970, 1, 1, 0, 0, 1, 2, undefined, 'America/Sao Paulo')
651+
new structure.Structure(0x66, [1, 2, 'America/Sao_Paulo']),
652+
new DateTime(1970, 1, 1, 0, 0, 1, 2, undefined, 'America/Sao_Paulo')
652653
]
653654
])('should unpack spatial types and temporal types (%s)', (_, struct, object) => {
654655
const buffer = alloc(256)

packages/bolt-connection/test/bolt/bolt-protocol-v4x0.test.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ describe('#unit BoltProtocolV4x0', () => {
277277
['Time', new Time(1, 1, 1, 1, 1)],
278278
['Date', new Date(1, 1, 1)],
279279
['LocalDateTime', new LocalDateTime(1, 1, 1, 1, 1, 1, 1)],
280-
['DateTimeWithZoneId', new DateTime(1, 1, 1, 1, 1, 1, 1, undefined, 'America/Sao Paulo')],
280+
['DateTimeWithZoneId', new DateTime(1, 1, 1, 1, 1, 1, 1, undefined, 'America/Sao_Paulo')],
281281
['DateTime', new DateTime(1, 1, 1, 1, 1, 1, 1, 1)],
282282
['Point2D', new Point(1, 1, 1)],
283283
['Point3D', new Point(1, 1, 1, 1)]
@@ -503,7 +503,7 @@ describe('#unit BoltProtocolV4x0', () => {
503503
],
504504
[
505505
'DateTimeWithZoneId with more fields',
506-
new structure.Structure(0x66, [1, 2, 'America/Sao Paulo', 'Brasil'])
506+
new structure.Structure(0x66, [1, 2, 'America/Sao_Paulo', 'Brasil'])
507507
]
508508
])('should not unpack with wrong size (%s)', (_, struct) => {
509509
const buffer = alloc(256)
@@ -519,7 +519,8 @@ describe('#unit BoltProtocolV4x0', () => {
519519

520520
buffer.reset()
521521

522-
expect(() => protocol.unpack(buffer)).toThrowErrorMatchingSnapshot()
522+
const unpacked = protocol.unpack(buffer)
523+
expect(() => unpacked instanceof structure.Structure).toThrowErrorMatchingSnapshot()
523524
})
524525

525526
it.each([
@@ -565,8 +566,8 @@ describe('#unit BoltProtocolV4x0', () => {
565566
],
566567
[
567568
'DateTimeWithZoneId',
568-
new structure.Structure(0x66, [1, 2, 'America/Sao Paulo']),
569-
new DateTime(1970, 1, 1, 0, 0, 1, 2, undefined, 'America/Sao Paulo')
569+
new structure.Structure(0x66, [1, 2, 'America/Sao_Paulo']),
570+
new DateTime(1970, 1, 1, 0, 0, 1, 2, undefined, 'America/Sao_Paulo')
570571
]
571572
])('should unpack spatial types and temporal types (%s)', (_, struct, object) => {
572573
const buffer = alloc(256)

packages/bolt-connection/test/bolt/bolt-protocol-v4x1.test.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ describe('#unit BoltProtocolV4x1', () => {
151151
['Time', new Time(1, 1, 1, 1, 1)],
152152
['Date', new Date(1, 1, 1)],
153153
['LocalDateTime', new LocalDateTime(1, 1, 1, 1, 1, 1, 1)],
154-
['DateTimeWithZoneId', new DateTime(1, 1, 1, 1, 1, 1, 1, undefined, 'America/Sao Paulo')],
154+
['DateTimeWithZoneId', new DateTime(1, 1, 1, 1, 1, 1, 1, undefined, 'America/Sao_Paulo')],
155155
['DateTime', new DateTime(1, 1, 1, 1, 1, 1, 1, 1)],
156156
['Point2D', new Point(1, 1, 1)],
157157
['Point3D', new Point(1, 1, 1, 1)]
@@ -377,7 +377,7 @@ describe('#unit BoltProtocolV4x1', () => {
377377
],
378378
[
379379
'DateTimeWithZoneId with more fields',
380-
new structure.Structure(0x66, [1, 2, 'America/Sao Paulo', 'Brasil'])
380+
new structure.Structure(0x66, [1, 2, 'America/Sao_Paulo', 'Brasil'])
381381
]
382382
])('should not unpack with wrong size (%s)', (_, struct) => {
383383
const buffer = alloc(256)
@@ -393,7 +393,8 @@ describe('#unit BoltProtocolV4x1', () => {
393393

394394
buffer.reset()
395395

396-
expect(() => protocol.unpack(buffer)).toThrowErrorMatchingSnapshot()
396+
const unpacked = protocol.unpack(buffer)
397+
expect(() => unpacked instanceof structure.Structure).toThrowErrorMatchingSnapshot()
397398
})
398399

399400
it.each([
@@ -439,8 +440,8 @@ describe('#unit BoltProtocolV4x1', () => {
439440
],
440441
[
441442
'DateTimeWithZoneId',
442-
new structure.Structure(0x66, [1, 2, 'America/Sao Paulo']),
443-
new DateTime(1970, 1, 1, 0, 0, 1, 2, undefined, 'America/Sao Paulo')
443+
new structure.Structure(0x66, [1, 2, 'America/Sao_Paulo']),
444+
new DateTime(1970, 1, 1, 0, 0, 1, 2, undefined, 'America/Sao_Paulo')
444445
]
445446
])('should unpack spatial types and temporal types (%s)', (_, struct, object) => {
446447
const buffer = alloc(256)

packages/bolt-connection/test/bolt/bolt-protocol-v4x2.test.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ describe('#unit BoltProtocolV4x2', () => {
150150
['Time', new Time(1, 1, 1, 1, 1)],
151151
['Date', new Date(1, 1, 1)],
152152
['LocalDateTime', new LocalDateTime(1, 1, 1, 1, 1, 1, 1)],
153-
['DateTimeWithZoneId', new DateTime(1, 1, 1, 1, 1, 1, 1, undefined, 'America/Sao Paulo')],
153+
['DateTimeWithZoneId', new DateTime(1, 1, 1, 1, 1, 1, 1, undefined, 'America/Sao_Paulo')],
154154
['DateTime', new DateTime(1, 1, 1, 1, 1, 1, 1, 1)],
155155
['Point2D', new Point(1, 1, 1)],
156156
['Point3D', new Point(1, 1, 1, 1)]
@@ -376,7 +376,7 @@ describe('#unit BoltProtocolV4x2', () => {
376376
],
377377
[
378378
'DateTimeWithZoneId with more fields',
379-
new structure.Structure(0x66, [1, 2, 'America/Sao Paulo', 'Brasil'])
379+
new structure.Structure(0x66, [1, 2, 'America/Sao_Paulo', 'Brasil'])
380380
]
381381
])('should not unpack with wrong size (%s)', (_, struct) => {
382382
const buffer = alloc(256)
@@ -392,7 +392,8 @@ describe('#unit BoltProtocolV4x2', () => {
392392

393393
buffer.reset()
394394

395-
expect(() => protocol.unpack(buffer)).toThrowErrorMatchingSnapshot()
395+
const unpacked = protocol.unpack(buffer)
396+
expect(() => unpacked instanceof structure.Structure).toThrowErrorMatchingSnapshot()
396397
})
397398

398399
it.each([
@@ -438,8 +439,8 @@ describe('#unit BoltProtocolV4x2', () => {
438439
],
439440
[
440441
'DateTimeWithZoneId',
441-
new structure.Structure(0x66, [1, 2, 'America/Sao Paulo']),
442-
new DateTime(1970, 1, 1, 0, 0, 1, 2, undefined, 'America/Sao Paulo')
442+
new structure.Structure(0x66, [1, 2, 'America/Sao_Paulo']),
443+
new DateTime(1970, 1, 1, 0, 0, 1, 2, undefined, 'America/Sao_Paulo')
443444
]
444445
])('should unpack spatial types and temporal types (%s)', (_, struct, object) => {
445446
const buffer = alloc(256)

packages/bolt-connection/test/bolt/bolt-protocol-v4x3.test.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ describe('#unit BoltProtocolV4x3', () => {
363363
['Time', new Time(1, 1, 1, 1, 1)],
364364
['Date', new Date(1, 1, 1)],
365365
['LocalDateTime', new LocalDateTime(1, 1, 1, 1, 1, 1, 1)],
366-
['DateTimeWithZoneId', new DateTime(1, 1, 1, 1, 1, 1, 1, undefined, 'America/Sao Paulo')],
366+
['DateTimeWithZoneId', new DateTime(1, 1, 1, 1, 1, 1, 1, undefined, 'America/Sao_Paulo')],
367367
['DateTime', new DateTime(1, 1, 1, 1, 1, 1, 1, 1)],
368368
['Point2D', new Point(1, 1, 1)],
369369
['Point3D', new Point(1, 1, 1, 1)]
@@ -589,7 +589,7 @@ describe('#unit BoltProtocolV4x3', () => {
589589
],
590590
[
591591
'DateTimeWithZoneId with more fields',
592-
new structure.Structure(0x66, [1, 2, 'America/Sao Paulo', 'Brasil'])
592+
new structure.Structure(0x66, [1, 2, 'America/Sao_Paulo', 'Brasil'])
593593
]
594594
])('should not unpack with wrong size (%s)', (_, struct) => {
595595
const buffer = alloc(256)
@@ -605,7 +605,8 @@ describe('#unit BoltProtocolV4x3', () => {
605605

606606
buffer.reset()
607607

608-
expect(() => protocol.unpack(buffer)).toThrowErrorMatchingSnapshot()
608+
const unpacked = protocol.unpack(buffer)
609+
expect(() => unpacked instanceof structure.Structure).toThrowErrorMatchingSnapshot()
609610
})
610611

611612
it.each([
@@ -651,8 +652,8 @@ describe('#unit BoltProtocolV4x3', () => {
651652
],
652653
[
653654
'DateTimeWithZoneId',
654-
new structure.Structure(0x66, [1, 2, 'America/Sao Paulo']),
655-
new DateTime(1970, 1, 1, 0, 0, 1, 2, undefined, 'America/Sao Paulo')
655+
new structure.Structure(0x66, [1, 2, 'America/Sao_Paulo']),
656+
new DateTime(1970, 1, 1, 0, 0, 1, 2, undefined, 'America/Sao_Paulo')
656657
]
657658
])('should unpack spatial types and temporal types (%s)', (_, struct, object) => {
658659
const buffer = alloc(256)
@@ -848,7 +849,7 @@ describe('#unit BoltProtocolV4x3', () => {
848849
],
849850
[
850851
'DateTimeWithZoneId with more fields',
851-
new structure.Structure(0x69, [1, 2, 'America/Sao Paulo', 'Brasil'])
852+
new structure.Structure(0x69, [1, 2, 'America/Sao_Paulo', 'Brasil'])
852853
]
853854
])('should not unpack with wrong size (%s)', (_, struct) => {
854855
const packable = protocol.packable(struct)
@@ -857,7 +858,8 @@ describe('#unit BoltProtocolV4x3', () => {
857858

858859
buffer.reset()
859860

860-
expect(() => protocol.unpack(buffer)).toThrowErrorMatchingSnapshot()
861+
const unpacked = protocol.unpack(buffer)
862+
expect(() => unpacked instanceof structure.Structure).toThrowErrorMatchingSnapshot()
861863
})
862864

863865
it.each([
@@ -900,7 +902,7 @@ describe('#unit BoltProtocolV4x3', () => {
900902
],
901903
[
902904
'DateTimeWithZoneId/0x66',
903-
new structure.Structure(0x66, [1, 2, 'America/Sao Paulo'])
905+
new structure.Structure(0x66, [1, 2, 'America/Sao_Paulo'])
904906
]
905907
])('should unpack deprecated temporal types as unknown structs (%s)', (_, struct) => {
906908
const packable = protocol.packable(struct)
@@ -948,7 +950,7 @@ describe('#unit BoltProtocolV4x3', () => {
948950
],
949951
[
950952
'DateTimeWithZoneId/0x69',
951-
new structure.Structure(0x69, [1, 2, 'America/Sao Paulo'])
953+
new structure.Structure(0x69, [1, 2, 'America/Sao_Paulo'])
952954
]
953955
])('should unpack utc temporal types as unknown structs (%s)', (_, struct) => {
954956
const packable = protocol.packable(struct)
@@ -969,8 +971,8 @@ describe('#unit BoltProtocolV4x3', () => {
969971
],
970972
[
971973
'DateTimeWithZoneId',
972-
new structure.Structure(0x66, [1, 2, 'America/Sao Paulo']),
973-
new DateTime(1970, 1, 1, 0, 0, 1, 2, undefined, 'America/Sao Paulo')
974+
new structure.Structure(0x66, [1, 2, 'America/Sao_Paulo']),
975+
new DateTime(1970, 1, 1, 0, 0, 1, 2, undefined, 'America/Sao_Paulo')
974976
]
975977
])('should unpack temporal types without utc fix (%s)', (_, struct, object) => {
976978
const packable = protocol.packable(struct)
@@ -984,7 +986,7 @@ describe('#unit BoltProtocolV4x3', () => {
984986
})
985987

986988
it.each([
987-
['DateTimeWithZoneId', new DateTime(1, 1, 1, 1, 1, 1, 1, undefined, 'America/Sao Paulo')],
989+
['DateTimeWithZoneId', new DateTime(1, 1, 1, 1, 1, 1, 1, undefined, 'America/Sao_Paulo')],
988990
['DateTime', new DateTime(1, 1, 1, 1, 1, 1, 1, 1)]
989991
])('should pack temporal types (no utc) (%s)', (_, object) => {
990992
const packable = protocol.packable(object)

0 commit comments

Comments
 (0)