How do you stop Retrofit looking for embedded objects in one-to-many relationships?

Viewed 179

If I have a one-to-many Android Room relationship between two tables, expressed like this (using an example cut and paste from the Android developers site):

@Entity
data class User(
    @PrimaryKey val userId: Long,
    val name: String,
    val age: Int
)

@Entity
data class Playlist(
    @PrimaryKey val playlistId: Long,
    val userCreatorId: Long,
    val playlistName: String
)

data class UserWithPlaylists(
    @Embedded val user: User,
    @Relation(
          parentColumn = "userId",
          entityColumn = "userCreatorId"
    )
    val playlists: List<Playlist>
)

then when I use Retrofit2 to get the nested one-to-many object UserWithPlaylists from an API it's expecting JSON that looks more or less like this:

{
    "user" : {
         "userID": 1,
         "name": "whatever",
         "age": 10
    },
    "playlists": [
        {
            "playlistId": 0,
            "playlistName": "whatever"
        },
        {
            "playlistId": 1,
            "playlistName": "whatever"
        }
    ]
}

But my api (and as far as I can tell, most APIs) doesn't embed the user details in "user {}" like that. It just shows the user details unembedded, like this:

{
    "userID": 1,
    "name": "whatever",
    "age": 10,
    "playlists": [
        {
            "playlistId": 0,
            "playlistName": "whatever"
        },
        {
            "playlistId": 1,
            "playlistName": "whatever"
        }
    ]
}

So Retrofit2 would be able to create the playlists, but it won't find a user{} object so I would end up with empty user objects but populated playlist objects.

How would I force Retrofit2 look for the user details unembedded?

Apologies for my roundabout way of asking this question. I don't know the proper terminology for all of this, which has made it difficult for me to search.

thanks!

John

0 Answers
Related