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

BSONTypeConversionError on trying to decode Date from timestamp in MongoDB #74

Closed surajthomask closed 1 year ago

surajthomask commented 1 year ago

We were trying to update the Vapor dependencies in our project, and encountered an issue with conversion of timestamp in MongoDB to Date objects. Version 8.0.1 works just fine, but updating to 8.0.6 breaks the decoding and throws the following error. {"reason":"BSONTypeConversionError<Optional<Primitive>>(from: Optional(695638132.716054), to: Foundation.Date)"}

An example model is given below;

          struct Item: Model {

            @Field var _id: ObjectId
            @Field var name: String
            @Field var updatedAt: Date

            init(name: String) {

                self._id = ObjectId()

                self.name = name

                self.updatedAt = Date()
            }
        }

v8.0.1 allows data insertion as well as retrieval without any issue. v8.0.6 breaks the data retrieval from the DB.

Would appreciate a quick look into this.

Joannis commented 1 year ago

Hey @surajthomask,

Is your date stored as a Double in the database?

Joannis commented 1 year ago

If you need me urgently in the future, please reach out through our discord server. There are a lot of github emails that pass through my mailbox daily.

surajthomask commented 1 year ago

Hi @Joannis, Thanks for the response. As of 8.0.1, the framework was storing the date as double in the server on it's own. As far as I can see there were no custom handling of data type other than specifying the variable type to be Double in the model. When we updated the dependencies to 8.0.6, it started throwing this error.