https://kotlinlang.org logo
#android
Title
# android
p

paulex

03/16/2020, 2:54 PM
Good-day guys, So i'm using openapi to generate some kotlin-client code, however i want to be able to use the models as Entity for android ROOM, is there a way to go around this? The models are data classes so i can't extend them
c

Cody Engel

03/16/2020, 3:05 PM
Not to my knowledge... When I used OpenAPI in the past we typically mapped the objects generated by the tool into ones we had had control over which would then be used to persist locally
Even if it is possible, I would be cautious as the spec changing could result in the entity changing which would require a migration anyway
p

paulex

03/16/2020, 3:30 PM
Thanks for your response.... So this is somewhat correct?
Copy code
@Entity(tableName = "deliveries")
data class DeliveryEntity(val delivery: Delivery)
Where
Delivery
is the generated model @Cody Engel
c

Cody Engel

03/16/2020, 3:41 PM
I don't think
Delivery
is a valid data type for SQLite
But the basic idea is correct though... I would probably make the primary constructor a secondary one, the primary one would have the relevant values from
Delivery
as properties
So something like...
Copy code
@Entity(tableName = "person")
data class PersonEntity(val firstName: String, val lastName: String) {
    constructor(person: Person) : this(person.firstName, person.lastName)
}
p

paulex

03/17/2020, 10:06 AM
I think for
Delivery
it can be an
@Embedded
type for SQLite, however i'm yet to try it out.. However, i think your solution is quite cleaner, aside that i have to write so much properties for the PersonEntity which already exist in Person...
c

Cody Engel

03/17/2020, 1:32 PM
Embedded would work, but it'd essentially put you in the same spot as just making the generated model an Entity. If the spec changes you'll need to do a database migration (or allow for the database to be cleared on upgrade).
p

paulex

03/18/2020, 9:08 PM
@Cody Engel Alright, thanks much!
🙇🏻‍♂️ 1
5 Views