Hello guys. I am using kotlinx.serialization in my ktor api and have a data class as below.
Copy code
@Serializable
data class User(
var email: String = "",
@JsonIgnore var password: String = ""
)
Which annotation can i use on the password field to ignore it "only" in the http response but not within the application.
For example
Ignore it when:
Copy code
get {
call.respond(User("email", "some password"))
}
Don't ignore it when:
Copy code
fun checkUserPassword(email: String) {
val user = getUserPasswordFromDatabaseByEmail<User>(email)
// The print below does not work (I want it to work since it is not yet part of the response)
println(user.password)
}
m
Matias Reparaz
10/04/2020, 2:06 AM
And using 2 different classes? One with pass and another without it?
👍 2
✔️ 1
n
Nicodemus Ojwee
10/04/2020, 8:54 AM
That is what i had thought of. I didn't want to create another class but i guess it is the only option now.
👌 1
m
Matteo Mirk
10/06/2020, 8:27 AM
This is the simplest idea that comes to mind:
Copy code
get {
call.respond(User("email", "some password").copy(password = ""))
}
you could even encapsulate the copying with empty values in an instance method or extension function declared in the router file/module.