Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deserialize not-constructor params (native) #118

Closed
qcho opened this issue Feb 22, 2019 · 4 comments
Closed

Deserialize not-constructor params (native) #118

qcho opened this issue Feb 22, 2019 · 4 comments

Comments

@qcho
Copy link

qcho commented Feb 22, 2019

Hi.

I'm using the native driver. It seems the deserialization is not working for "not constructor" fields.

When i check the database. The data is saved properly!

> db.document2.find()
{ "_id" : 10, "field1" : "Hola", "field2" : "Mundo" }
    data class Document2(@BsonId val id: Int) {
        var field1: String? = null
        var field2: String? = null
    }

    @Test fun testMongoRecord() {
        val client = KMongo.createClient()
        val database = client.getDatabase("test")

        run {
            val record = Document2(10)
            record.field1 = "Hola"
            record.field2 = "Mundo"

            val col = database.getCollection<Document2>()
            col.drop()
            col.insertOne(record)

            val fromDb = col.find().first()!!
            assertEquals(10, fromDb.id)
            assertEquals("Hola", fromDb.field1) <-- FAILS field1 is null
            assertEquals("Mundo", fromDb.field2) <-- FAILS field2 is null
        }
[...]

I hoped it would use the public setters

@qcho
Copy link
Author

qcho commented Feb 22, 2019

Also, I found this workaround. But I'm working with 30+ fields, creating this placeholder properties is error-prone.

    class Document2(
            @BsonId val id: Int,
            _field1: String? = null,
            _field2: String? = null
    ) {
        var field1: String? = _field1
        var field2: String? = _field2
    }
`

@zigzago
Copy link
Member

zigzago commented Feb 22, 2019

@qcho I think you forgot to use the .coroutine extension applied (your choice) to the client, database or collection. Look at these tests: https://github.com/Litote/kmongo/blob/master/kmongo-coroutine-core-tests/src/main/kotlin/org/litote/kmongo/coroutine/ReactiveStreamsFindOneTest.kt where

val col by lazy { rule.col.coroutine }

@zigzago
Copy link
Member

zigzago commented Feb 22, 2019

Ok. I have not seen that the properties are not in the constructor. I'm going to check that.

@zigzago
Copy link
Member

zigzago commented Feb 22, 2019

Thanks for reporting, the fix is in the snapshot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants