This repository has been archived by the owner on Jan 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 109
Including attributes into relationship object #181
Comments
Hey @mareapp I'm not sure if this is valid JSONAPI, however I'm currently achieving the same example by creating my own public struct ToOneLinkage<T: Resource> : Relationship {
public typealias Linked = T
public var name: String
public var serializedName: String
public var isReadOnly: Bool = false
private var shouldSerializeAttributes: Bool = false
public func serializeAttributes() -> ToOneLinkage {
var newField = self
newField.shouldSerializeAttributes = true
return newField
}
public init(_ name: String, to linkedType: T.Type) {
self.name = name
self.serializedName = name
}
public func serialize(from resource: Resource,
into serializedData: inout [String: Any],
withKeyFormatter keyFormatter: KeyFormatter,
withValueFormatters valueFormatters: ValueFormatterRegistry,
withOptions options: SerializationOptions) {
let key = keyFormatter.format(self)
if options.contains(.IncludeToOne) {
let linkedResource: Linked?
let fieldValue = resource.value(forField: self.name)
if let linkingObjects = fieldValue as? RLMLinkingObjects {
if linkingObjects.count > 1 {
fatalError("Should only use `ToOneLinkage` for to one linkage...")
}
linkedResource = linkingObjects.firstObject() as? Linked
} else {
linkedResource = fieldValue as? Linked
}
var serializedResourceData: [String: Any] = [:]
// Serialize type
serializedResourceData["type"] = Linked.resourceType
// Serialize ID
if let resourceId = linkedResource?.id {
serializedResourceData["id"] = resourceId
}
// Serialize Attributes
if self.shouldSerializeAttributes, let fields = linkedResource?.fields {
for field in fields where !field.isReadOnly && field is Attribute {
field.serialize(from: linkedResource!,
into: &serializedResourceData,
withKeyFormatter: keyFormatter,
withValueFormatters: valueFormatters,
withOptions: options)
}
}
let serializedRelationship = [ "data": linkedResource != nil ? serializedResourceData : NSNull() as Any]
if serializedData["relationships"] == nil {
serializedData["relationships"] = [key: serializedRelationship]
} else {
var relationships = serializedData["relationships"] as! [String: Any]
relationships[key] = serializedRelationship
serializedData["relationships"] = relationships
}
}
}
Which can be used in place of your |
Thanks for answer, yeah its true my back end has not followed standard about JSON API but only when need POST operation, so I dont have any problem with GET but when I need to send this kind of request body it is problem , by the way I tried something like your example but didnt work for me |
Not sure if the above can be archived without modifying or writing your own Spine/Spine/SerializeOperation.swift Line 169 in 13c650d
|
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
I am trying to implement save(POST) to my back-end, here is my request body:
Does your lib support including attributes into relationship object, like in this json where relationship object has user with attributes. Please could you give me code example how to do this if possible?
Thanks in advance
The text was updated successfully, but these errors were encountered: