File tree Expand file tree Collapse file tree 3 files changed +43
-1
lines changed Expand file tree Collapse file tree 3 files changed +43
-1
lines changed Original file line number Diff line number Diff line change @@ -158,6 +158,32 @@ extension BSONBinary: BSONValue {
158158 * - `DecodingError` if `json` is a partial match or is malformed.
159159 */
160160 internal init ? ( fromExtJSON json: JSON , keyPath: [ String ] ) throws {
161+ if let uuidJSON = try json. unwrapObject ( withKey: " $uuid " , keyPath: keyPath) {
162+ guard let uuidString = uuidJSON. stringValue else {
163+ throw DecodingError . _extendedJSONError (
164+ keyPath: keyPath,
165+ debugDescription: " Expected value for key $uuid \" \( uuidJSON) \" to be a string "
166+ + " but got some other value "
167+ )
168+ }
169+ guard let uuid = UUID ( uuidString: uuidString) else {
170+ throw DecodingError . _extendedJSONError (
171+ keyPath: keyPath,
172+ debugDescription: " Invalid UUID string: \( uuidString) "
173+ )
174+ }
175+
176+ do {
177+ self = try BSONBinary ( from: uuid)
178+ return
179+ } catch {
180+ throw DecodingError . _extendedJSONError (
181+ keyPath: keyPath,
182+ debugDescription: error. localizedDescription
183+ )
184+ }
185+ }
186+
161187 // canonical and relaxed extended JSON
162188 guard let binary = try json. unwrapObject ( withKey: " $binary " , keyPath: keyPath) else {
163189 return nil
Original file line number Diff line number Diff line change 3939 "canonical_bson" : " 1D000000057800100000000473FFD26444B34C6990E8E7D1DFC035D400" ,
4040 "canonical_extjson" : " {\" x\" : { \" $binary\" : {\" base64\" : \" c//SZESzTGmQ6OfR38A11A==\" , \" subType\" : \" 04\" }}}"
4141 },
42+ {
43+ "description" : " subtype 0x04 UUID" ,
44+ "canonical_bson" : " 1D000000057800100000000473FFD26444B34C6990E8E7D1DFC035D400" ,
45+ "canonical_extjson" : " {\" x\" : { \" $binary\" : {\" base64\" : \" c//SZESzTGmQ6OfR38A11A==\" , \" subType\" : \" 04\" }}}" ,
46+ "degenerate_extjson" : " {\" x\" : { \" $uuid\" : \" 73ffd264-44b3-4c69-90e8-e7d1dfc035d4\" }}"
47+ },
4248 {
4349 "description" : " subtype 0x05" ,
4450 "canonical_bson" : " 1D000000057800100000000573FFD26444B34C6990E8E7D1DFC035D400" ,
8187 "description" : " subtype 0x02 length negative one" ,
8288 "bson" : " 130000000578000600000002FFFFFFFFFFFF00"
8389 }
90+ ],
91+ "parseErrors" : [
92+ {
93+ "description" : " $uuid wrong type" ,
94+ "string" : " {\" x\" : { \" $uuid\" : { \" data\" : \" 73ffd264-44b3-4c69-90e8-e7d1dfc035d4\" }}}"
95+ },
96+ {
97+ "description" : " $uuid invalid value" ,
98+ "string" : " {\" x\" : { \" $uuid\" : \" 73ffd264-44b3-90e8-e7d1dfc035d4\" }}"
99+ }
84100 ]
85101}
Original file line number Diff line number Diff line change @@ -243,7 +243,7 @@ final class BSONCorpusTests: BSONTestCase {
243243 }
244244 let description = " \( testFile. description) - \( test. description) "
245245 switch BSONType ( rawValue: UInt8 ( testFile. bsonType. dropFirst ( 2 ) , radix: 16 ) !) ! {
246- case . invalid: // "top level document" uses 0x00 for the bson type
246+ case . binary , . invalid: // "top level document" uses 0x00 for the bson type
247247 guard let testData = test. string. data ( using: . utf8) else {
248248 XCTFail ( " Unable to interpret canonical_bson as Data " )
249249 return
You can’t perform that action at this time.
0 commit comments