orlandos-nl / BSON

Native Swift library for BSON (http://bsonspec.org)
https://orlandos.nl/docs/mongokitten/articles/bson
MIT License
109 stars 35 forks source link

Range issue! #34

Closed shial4 closed 2 years ago

shial4 commented 7 years ago

Codable haven an issue while unwraping back value from document.

If I have an object with wariable `var salt: [UInt8] = [0,1,2,3,4,5,68,122,254,255]

on getting back object with this error is returned

Initializing from document failed: dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: "BSON number <0> does not fit in UInt8", underlyingError: nil))
Could not initialize User from document

What I've notice the range in Codable.swift is wrong.

instead of unwraping full range back into Uint8 0,1,2…253,254,255 This https://github.com/OpenKitten/BSON/blob/0a0dd33ebe9b25c5c8ee488fe86dbb4c4fa48dd2/Sources/BSON/Codable.swift#L626 Constraints range to this 0 {1,2…253,254} 255 throwing an error when ever 0 or 255 appear.

That's true not only for UInt8 but others as well.

My example Model:

final class User: Model {
    var _id = ObjectId()
    var salt: [UInt8] //Temporary solution - store salt as hexString
    var password: String
    var login: String
}