wvteijlingen / Spine

A Swift library for working with JSON:API APIs. It supports mapping to custom model classes, fetching, advanced querying, linking and persisting.
MIT License
264 stars 109 forks source link

linkResource returns Spine.SerializerError.topLevelEntryMissing #132

Closed koraykoska closed 7 years ago

koraykoska commented 7 years ago

I have a profile model like that:

class Profile: Resource {
    // ...
    var followers: LinkedResourceCollection?
    var followees: LinkedResourceCollection?
    // ...

    override class var resourceType: ResourceType {
        return "profiles"
    }

    override class var fields: [Field] {
        return fieldsFromDictionary([
            // ...
            "followers": ToManyRelationship(Profile.self),
            "followees": ToManyRelationship(Profile.self)
            // ...
        ])
    }
}

Now if I have two profiles, which are already saved, and I want to link them together (so profile1 starts following profile2), I get a Spine.SerializerError.topLevelEntryMissing.

// ...

// Try to link profile2 into the followees to-many-relationship of profile1
profile1.followees?.linkResource(profile2)

SpineHelper.sharedInstance.spine.save(profile).onSuccess(callback: { (profile) in
    print("Linked...")
}).onFailure(callback: { (error) in
    print("******* ERROR *******")
    print(error)
})

The result is always the same:

ERROR serializerError(Spine.SerializerError.topLevelEntryMissing)

Am I doing something wrong? Doing self-joined relationships should be compliant with JSONAPI as well as naming to-many-relationships other than the pluralized resource name, so Spine should support this.

wvteijlingen commented 7 years ago

Can you post here the response from the server? If you enable logging Spine will output it to the console: Spine.setLogLevel(.logDebug, forDomain: .networking)

koraykoska commented 7 years ago

Ok I am sorry, the server responded with a wrongly rendered 500 Internal Server error and I didn't recognize it because the tests passed. This is an issue with my backend, not with Spine. Thank you for the tip.