@@ -41,6 +41,48 @@ open class ExtendedJSONConversionTestCase: BSONTestCase {
41
41
expect ( decoded) . to ( equal ( test) )
42
42
}
43
43
44
+ func testExtendedJSONDecodingWithUserInfo( ) throws {
45
+ struct Foo : Decodable , Equatable {
46
+ let val : BSON
47
+ let bar : Bar
48
+
49
+ init ( from decoder: Decoder ) throws {
50
+ guard let info = decoder. userInfo [ . barInfo] as? BSON else {
51
+ throw TestError ( message: " userInfo not present " )
52
+ }
53
+ self . val = info
54
+
55
+ // test userinfo is propogated to sub containers
56
+ let container = try decoder. singleValueContainer ( )
57
+ self . bar = try container. decode ( Bar . self)
58
+ }
59
+ }
60
+
61
+ struct Bar : Decodable , Equatable {
62
+ let val : BSON
63
+
64
+ init ( from decoder: Decoder ) throws {
65
+ guard let info = decoder. userInfo [ . barInfo] as? BSON else {
66
+ throw TestError ( message: " userInfo not present " )
67
+ }
68
+ self . val = info
69
+ }
70
+ }
71
+
72
+ let obj = " {} " . data ( using: . utf8) !
73
+ let decoder = ExtendedJSONDecoder ( )
74
+
75
+ decoder. userInfo [ . barInfo] = BSON . bool ( true )
76
+ let boolDecoded = try decoder. decode ( Foo . self, from: obj)
77
+ expect ( boolDecoded. val) . to ( equal ( true ) )
78
+ expect ( boolDecoded. bar. val) . to ( equal ( true ) )
79
+
80
+ decoder. userInfo [ . barInfo] = BSON . string ( " hello world " )
81
+ let stringDecoded = try decoder. decode ( Foo . self, from: obj)
82
+ expect ( stringDecoded. val) . to ( equal ( " hello world " ) )
83
+ expect ( stringDecoded. bar. val) . to ( equal ( " hello world " ) )
84
+ }
85
+
44
86
func testExtendedJSONEncodingWithUserInfo( ) throws {
45
87
struct Foo : Codable , Equatable {
46
88
func encode( to encoder: Encoder ) throws {
0 commit comments