retrofit2-kotlinx-serialization-converter icon indicating copy to clipboard operation
retrofit2-kotlinx-serialization-converter copied to clipboard

Support for path/query parameters

Open JavierSegoviaCordoba opened this issue 4 years ago • 6 comments

I am using strongly typed ids (currently with data class until inline class support is available):

@Serializable
data class UserId(val value: Long)

@Serializable
data class User(val id: UserId, val name: String)

interface UsersService {

    @GET("users")
    suspend fun getUsers(): List<User> // this works

    @GET("users/{id}")
    suspend fun getUser(@Path("id") id: UserId): User // this doesn't work, because the path is `.../users/UserId(value=1)`
}

JavierSegoviaCordoba avatar Jan 01 '21 16:01 JavierSegoviaCordoba

As a workaround, I added toString method to UserId

JavierSegoviaCordoba avatar Jan 01 '21 16:01 JavierSegoviaCordoba

this does not affect this library and it is not a bug either, but it is a misuse

xanscale avatar Nov 16 '21 19:11 xanscale

this does not affect this library and it is not a bug either, but it is a misuse

@xanscale, can you elaborate why this is a misuse?

My guess is something into the direction of "the interpolation of the placeholders in the @GET strings should not rely on details of the underlying serializer".

sschuberth avatar May 02 '22 13:05 sschuberth

@sschuberth first this things impact retrofit and not this converter

second, retrofit call toString() to create the final string, adding "toString" is not a woraround but the correct way

xanscale avatar May 02 '22 13:05 xanscale

@JakeWharton you can close this UNissue

xanscale avatar May 02 '22 13:05 xanscale

The issue is with inline classes and value classes, based on they are a wrapper over a type, having to manually write toString for them is just boilerplate IMO.

Basically is simplifying this article from Jake on Retrofit

https://jakewharton.com/inline-classes-make-great-database-ids/

Not sure how they are working at this moment in Sqldelight tho.

JavierSegoviaCordoba avatar May 02 '22 15:05 JavierSegoviaCordoba