Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strange behaviour of object used in an API call #111

Open
sterien7 opened this issue Sep 28, 2020 · 1 comment
Open

Strange behaviour of object used in an API call #111

sterien7 opened this issue Sep 28, 2020 · 1 comment

Comments

@sterien7
Copy link

sterien7 commented Sep 28, 2020

I have problems trying to do the following in my app in Kotlin
I have 2 objects, User and UserRelationship

import moe.banana.jsonapi2.JsonApi
import moe.banana.jsonapi2.Resource

JsonApi(type = "users")
class User : Resource() {

    @field:Json(name = "related-user-relationships")
    var relatedUserRelationships: HasMany<UserRelationship>? = null
    
    val relatedUserRelationshipsCollection: List<UserRelationship>?
        get() {
            return relatedUserRelationships?.get(this.document)
        }
}

@JsonApi(type = "user-relationships")
class UserRelationship() : Resource() {
    @field:Json(name = "is-favorite")
    var isFavorite: Boolean = false

    var user: HasOne<User>? = null
    @field:Json(name = "related-user")
    var relatedUser: HasOne<User>? = null
    
    val userObject: User?
        get() {
            return user?.get(this.document)
        }

    val relatedUserObject: User?
        get() {
            return relatedUser?.get(this.document)
        }
}

The task needed it to search if userA has a relationship with userB and then update it
so the code I execute is

var userRelationship = userA.relatedUserRelationshipsCollection?.firstOrNull { it.relatedUserObject?.id == userB.id } ?: UserRelationship()
userRelationship.user = HasOne(userA)
userRelationship.relatedUser = HasOne(userB)
userRelationship.isFavorite = !userRelationship.isFavorite

and after that I execute the http request

@POST("user-relationships")
 fun addUserRelationship(@Body userRelationship: UserRelationship): Observable<UserRelationship>

The request executes without a problem and returns the relationship object.
The problem is than now
userA.document = null
but userB.document is not null

any ideas

@sterien7
Copy link
Author

I came up with an idea.
I though what if I had the exact object copied to a new one and I created a method

fun <T : Serializable> deepCopy(obj: T?): T? {
        if (obj == null) return null
        val baos = ByteArrayOutputStream()
        val oos  = ObjectOutputStream(baos)
        oos.writeObject(obj)
        oos.close()
        val bais = ByteArrayInputStream(baos.toByteArray())
        val ois  = ObjectInputStream(bais)
        @Suppress("unchecked_cast")
        return ois.readObject() as T
    }

after using this method in order to create the UserRelationship that I POST to the server, everything worked.
So I think it has something to do with retrofit and the way it handles the data sent over HTTP

Is that helpfull?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant