Hey folks! I defined a serializable class UserSeri...
# ktor
m
Hey folks! I defined a serializable class UserSerializer that has a property "password" with nullable type. However, when I instantiate the UserSerializer class in my route handler, I get the error "No value passed for parameter 'password'". I'd like to know what I'm doing wrong. Below is the UserSerializer class and where it's being used:
Copy code
@Serializable
data class UserSerializer(
    val email: String,
    val handle: String,
    val password: String?,
    val dateCreated: String,
    val dateModified: String,
    @Contextual
    val id: EntityID<Int>?
)
Copy code
UserResponse(
    status = "success",
    message = "Login successful",
    // UserSerializer class instantiation
    data = UserSerializer(
        id = user!![Users.id],
        email = user!![Users.email],
        handle = user!![Users.handle],
        dateCreated = user!![Users.dateCreated].toString(),
        dateModified = user!![Users.dateModified].toString()
    )
)
m
If you want to be able to omit it in the constructor, you'll need to provide a default value:
val password: String? = null
Parameters that have default value are best added to the end of the function so there's no confusion when invoking the function without named arguments.
m
I did now I'm faced with another error:
Copy code
kotlinx.serialization.SerializationException: Serializer for class 'EntityID' is not found.
Mark the class as @Serializable or provide the serializer explicitly.
This is coming from the id property. Kotlinx.serialization doesn't deserialize/serialize EntityID types by default. How do I solve this?
m
I'd have thought that someone already had already implemented that but couldn't find anything either. So in all likelihood you'll have to implement your own. There's some good documentation on that out there, though: https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/serializers.md#custom-serializers-for-a-generic-type
m
Hmm.. thanks for the link. I just went through it. I'd definitely try to implement my own custom serializer.