orlandos-nl / BSON

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

UInt decoding #48

Closed graphiclife closed 6 years ago

graphiclife commented 6 years ago

There seems to be a weird issue encoding UInts. Consider the following example:

// Code
import BSON

do {
  struct Foo: Codable {
    var record: UInt64

    init(record: UInt64) {
      self.record = record
    }
  }

  let firstFoo = Foo(record: 283467841569)
  let document = try BSONEncoder().encode(firstFoo)
  let secondFoo = try BSONDecoder().decode(Foo.self, from: document)

  print("Decoded: \(secondFoo.record)")
} catch {
  print("Error: \(error)")
}

// Output:
Error: dataCorrupted(Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "record", intValue: nil)], debugDescription: "BSON number <283467841569> does not fit in UInt", underlyingError: nil))

Running swift:

Welcome to Apple Swift version 4.1.2 (swiftlang-902.0.54 clang-902.0.39.2). Type :help for assistance.
  1> print(UInt.max)
18446744073709551615

283467841569 should fit fine in a UInt.

Joannis commented 6 years ago

Which BSON version is installed? I've seen/fixed this bug before, although I might've overlooked something like a tag or some code.

graphiclife commented 6 years ago

Should be the latest one, retrieved through SPM, 5.2.3.

graphiclife commented 6 years ago

Yes, it seems the latest commit in which you fixed the uint bug is not tagged for release :)

Joannis commented 6 years ago

FYI; I tagged the release 👍