Kotlin deserialize problem (non-null fields)



I’m writing a backend service in Kotlin and Spring Boot, and for Fauna integration I’m using the Java driver (GitHub - fauna/faunadb-jvm: Scala and Java driver for FaunaDB).

One of my Kotlin data classes has a field that does not allow null values - a feature of Kotlin language (see here: Null safety | Kotlin) - and thus causes an exception to be thrown when data from Fauna is fetched and deserialization process and creation of instance of Kotlin data class is attempted.

The value of the field in Fauna is not null. It is a string value and it is correctly returned from Fauna and received in my application. The problem seems to lie in that the deserialization process first instantiates the data class with no values (because this is OK in Java) and then sets the values on every field afterwards. Could this be it? In Kotlin, when a field is non-nullable, the field needs to be set upon object creation (providing the value to the constructor normally).

If we compare to the Jackson deserialize/serialize library that is used to deserialize the request body from the frontend to my backend service, this is not a problem. So Jackson deserializer seems to handle this correctly, whereas Fauna Java driver deserialization does not.

Anyone faced this issue? Any input from anyone working @ Fauna?

Thanks in advance

Not sure… I made a ticket to investigate (tracked internally as DRV-640)

hi @Andreas_B,

In java application we can create a data class with a defined constructor:

If we don’t specify a constructor the class will be created with a default one as you suggested.

Did you specify a specific constructor for your Kotlin data class?
You can send an example of your code here as well.

Hey :wave:

@Andreas_B The approach that @parkhomenko suggested is correct. :+1:

Try setting up your data class like this, and it should just work:

import com.faunadb.client.types.FaunaConstructor
import com.faunadb.client.types.FaunaField

data class MyExampleDataClass @FaunaConstructor constructor(
    @FaunaField("name") val name: String,
    @FaunaField("content") val content: Map<String, String>,

Note the usage of @FaunaConstructor constructor :slight_smile:

1 Like