From 2037637c4c2e7611e66a32c85f2d916d6cb327b7 Mon Sep 17 00:00:00 2001 From: Mihran Dovlatyan Date: Thu, 3 Aug 2017 14:54:46 +0200 Subject: [PATCH 01/10] Check if null values should be omited --- Spine/SerializeOperation.swift | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/Spine/SerializeOperation.swift b/Spine/SerializeOperation.swift index 8d73235c..cdd6dc5d 100644 --- a/Spine/SerializeOperation.swift +++ b/Spine/SerializeOperation.swift @@ -135,20 +135,23 @@ class SerializeOperation: Operation { serializedId = NSNull() } - let serializedRelationship = [ - "data": [ - "type": type, - "id": serializedId - ] - ] - - if serializedData["relationships"] == nil { - serializedData["relationships"] = [key: serializedRelationship] - } else { - var relationships = serializedData["relationships"] as! [String: Any] - relationships[key] = serializedRelationship - serializedData["relationships"] = relationships - } + if !options.contains(.OmitNullValues) || (serializedId as? String) != nil { + + let serializedRelationship = [ + "data": [ + "type": type, + "id": serializedId + ] + ] + + if serializedData["relationships"] == nil { + serializedData["relationships"] = [key: serializedRelationship] + } else { + var relationships = serializedData["relationships"] as! [String: Any] + relationships[key] = serializedRelationship + serializedData["relationships"] = relationships + } + } } /// Adds the given resources as a to to-many relationship to the serialized data. From 5a85c228b74eb1f387fafd91dea5f329256fa914 Mon Sep 17 00:00:00 2001 From: MihranDovlatyan Date: Thu, 3 Aug 2017 15:04:26 +0200 Subject: [PATCH 02/10] Check if null values should be omited (#1) --- Spine/SerializeOperation.swift | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/Spine/SerializeOperation.swift b/Spine/SerializeOperation.swift index 8d73235c..cdd6dc5d 100644 --- a/Spine/SerializeOperation.swift +++ b/Spine/SerializeOperation.swift @@ -135,20 +135,23 @@ class SerializeOperation: Operation { serializedId = NSNull() } - let serializedRelationship = [ - "data": [ - "type": type, - "id": serializedId - ] - ] - - if serializedData["relationships"] == nil { - serializedData["relationships"] = [key: serializedRelationship] - } else { - var relationships = serializedData["relationships"] as! [String: Any] - relationships[key] = serializedRelationship - serializedData["relationships"] = relationships - } + if !options.contains(.OmitNullValues) || (serializedId as? String) != nil { + + let serializedRelationship = [ + "data": [ + "type": type, + "id": serializedId + ] + ] + + if serializedData["relationships"] == nil { + serializedData["relationships"] = [key: serializedRelationship] + } else { + var relationships = serializedData["relationships"] as! [String: Any] + relationships[key] = serializedRelationship + serializedData["relationships"] = relationships + } + } } /// Adds the given resources as a to to-many relationship to the serialized data. From 778be4d83ab1015faa8f7227ed7ff0143bfa0e9c Mon Sep 17 00:00:00 2001 From: Mihran Dovlatyan Date: Tue, 17 Oct 2017 13:59:43 +0200 Subject: [PATCH 03/10] return nil, when linkData["data"] is nil --- Spine/DeserializeOperation.swift | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Spine/DeserializeOperation.swift b/Spine/DeserializeOperation.swift index 86a02e94..92e101b6 100644 --- a/Spine/DeserializeOperation.swift +++ b/Spine/DeserializeOperation.swift @@ -261,6 +261,11 @@ class DeserializeOperation: Operation { var resource: Resource? = nil if let linkData = serializedData["relationships"][key].dictionary { + + guard linkData["data"] != nil else { + return nil + } + let type = linkData["data"]?["type"].string ?? linkedType if let id = linkData["data"]?["id"].string { From 2a78f51804d580e70f73875eee93c46e07f7edf7 Mon Sep 17 00:00:00 2001 From: Mihran Dovlatyan Date: Thu, 16 Nov 2017 16:18:35 +0100 Subject: [PATCH 04/10] fixed OmitNullValues in addToManyRelationship --- Spine/SerializeOperation.swift | 42 +++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/Spine/SerializeOperation.swift b/Spine/SerializeOperation.swift index cdd6dc5d..0d002cdc 100644 --- a/Spine/SerializeOperation.swift +++ b/Spine/SerializeOperation.swift @@ -161,24 +161,28 @@ class SerializeOperation: Operation { /// - parameter key: The key to add to the serialized data. /// - parameter type: The resource type of the linked resource as defined on the parent resource. fileprivate func addToManyRelationship(_ linkedResources: ResourceCollection?, to serializedData: inout [String: Any], key: String, type: ResourceType) { - var resourceIdentifiers: [ResourceIdentifier] = [] - - if let resources = linkedResources?.resources { - resourceIdentifiers = resources.filter { $0.id != nil }.map { resource in - return ResourceIdentifier(type: resource.resourceType, id: resource.id!) - } - } - - let serializedRelationship = [ - "data": resourceIdentifiers.map { $0.toDictionary() } - ] - - if serializedData["relationships"] == nil { - serializedData["relationships"] = [key: serializedRelationship] - } else { - var relationships = serializedData["relationships"] as! [String: Any] - relationships[key] = serializedRelationship - serializedData["relationships"] = relationships - } + + if !options.contains(.OmitNullValues) || linkedResources != nil { + + var resourceIdentifiers: [ResourceIdentifier] = [] + + if let resources = linkedResources?.resources { + resourceIdentifiers = resources.filter { $0.id != nil }.map { resource in + return ResourceIdentifier(type: resource.resourceType, id: resource.id!) + } + } + + let serializedRelationship = [ + "data": resourceIdentifiers.map { $0.toDictionary() } + ] + + if serializedData["relationships"] == nil { + serializedData["relationships"] = [key: serializedRelationship] + } else { + var relationships = serializedData["relationships"] as! [String: Any] + relationships[key] = serializedRelationship + serializedData["relationships"] = relationships + } + } } } From 883470622e6d5ccef70054ad706a95f589d66a2c Mon Sep 17 00:00:00 2001 From: MihranDovlatyan Date: Thu, 16 Nov 2017 16:22:51 +0100 Subject: [PATCH 05/10] fixed OmitNullValues in addToManyRelationship (#2) --- Spine/SerializeOperation.swift | 42 +++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/Spine/SerializeOperation.swift b/Spine/SerializeOperation.swift index cdd6dc5d..0d002cdc 100644 --- a/Spine/SerializeOperation.swift +++ b/Spine/SerializeOperation.swift @@ -161,24 +161,28 @@ class SerializeOperation: Operation { /// - parameter key: The key to add to the serialized data. /// - parameter type: The resource type of the linked resource as defined on the parent resource. fileprivate func addToManyRelationship(_ linkedResources: ResourceCollection?, to serializedData: inout [String: Any], key: String, type: ResourceType) { - var resourceIdentifiers: [ResourceIdentifier] = [] - - if let resources = linkedResources?.resources { - resourceIdentifiers = resources.filter { $0.id != nil }.map { resource in - return ResourceIdentifier(type: resource.resourceType, id: resource.id!) - } - } - - let serializedRelationship = [ - "data": resourceIdentifiers.map { $0.toDictionary() } - ] - - if serializedData["relationships"] == nil { - serializedData["relationships"] = [key: serializedRelationship] - } else { - var relationships = serializedData["relationships"] as! [String: Any] - relationships[key] = serializedRelationship - serializedData["relationships"] = relationships - } + + if !options.contains(.OmitNullValues) || linkedResources != nil { + + var resourceIdentifiers: [ResourceIdentifier] = [] + + if let resources = linkedResources?.resources { + resourceIdentifiers = resources.filter { $0.id != nil }.map { resource in + return ResourceIdentifier(type: resource.resourceType, id: resource.id!) + } + } + + let serializedRelationship = [ + "data": resourceIdentifiers.map { $0.toDictionary() } + ] + + if serializedData["relationships"] == nil { + serializedData["relationships"] = [key: serializedRelationship] + } else { + var relationships = serializedData["relationships"] as! [String: Any] + relationships[key] = serializedRelationship + serializedData["relationships"] = relationships + } + } } } From 843be50eeb0f7d3610d830735e2029dbe34a52d6 Mon Sep 17 00:00:00 2001 From: Mihran Dovlatyan Date: Wed, 7 Feb 2018 12:19:43 +0100 Subject: [PATCH 06/10] return nil when linkData is null --- Spine/DeserializeOperation.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Spine/DeserializeOperation.swift b/Spine/DeserializeOperation.swift index 92e101b6..57347e62 100644 --- a/Spine/DeserializeOperation.swift +++ b/Spine/DeserializeOperation.swift @@ -262,7 +262,7 @@ class DeserializeOperation: Operation { if let linkData = serializedData["relationships"][key].dictionary { - guard linkData["data"] != nil else { + guard linkData["data"] != nil, linkData["data"]!.type != SwiftyJSON.Type.null else { return nil } From 092b1b9319d3be80c7708cd243ab7598811703cc Mon Sep 17 00:00:00 2001 From: Mihran Dovlatyan Date: Fri, 16 Mar 2018 15:50:30 +0100 Subject: [PATCH 07/10] small fix --- Cartfile.resolved | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cartfile.resolved b/Cartfile.resolved index 81ccebec..c909c740 100644 --- a/Cartfile.resolved +++ b/Cartfile.resolved @@ -1,3 +1,3 @@ -github "antitypical/Result" "3.0.0" +github "antitypical/Result" "3.2.4" github "SwiftyJSON/SwiftyJSON" "3.1.4" -github "Thomvis/BrightFutures" "v5.0.1" +github "Thomvis/BrightFutures" "5.2.0" From dba23f642547c3d3fa8ffc36c1c13b2906c4eb7b Mon Sep 17 00:00:00 2001 From: Mihran Dovlatyan Date: Mon, 14 May 2018 09:27:48 +0200 Subject: [PATCH 08/10] update meta --- Spine/Serializing.swift | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Spine/Serializing.swift b/Spine/Serializing.swift index c1a30ef9..2bc91d61 100644 --- a/Spine/Serializing.swift +++ b/Spine/Serializing.swift @@ -80,6 +80,11 @@ public class Serializer { let document = JSONAPIDocument(data: resources, included: nil, errors: nil, meta: nil, links: nil, jsonapi: nil) return try serializeDocument(document, options: options) } + + public func serializeResources(_ resources: [Resource], meta: Metadata?, options: SerializationOptions = [.IncludeID]) throws -> Data { + let document = JSONAPIDocument(data: resources, included: nil, errors: nil, meta: meta, links: nil, jsonapi: nil) + return try serializeDocument(document, options: options) + } /// Converts the given resource to link data, and serializes it into NSData. From d27f4395f65a3ae1081bd502b2d170ff19a7ea8a Mon Sep 17 00:00:00 2001 From: Mihran Dovlatyan Date: Mon, 14 May 2018 10:45:38 +0200 Subject: [PATCH 09/10] serialize meta --- Spine/SerializeOperation.swift | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Spine/SerializeOperation.swift b/Spine/SerializeOperation.swift index 0d002cdc..6fe144ad 100644 --- a/Spine/SerializeOperation.swift +++ b/Spine/SerializeOperation.swift @@ -61,6 +61,11 @@ class SerializeOperation: Operation { // Serialize type serializedData["type"] = resource.resourceType as AnyObject? + + // Serialize meta + if let meta = resource.meta { + serializedData["meta"] = meta + } // Serialize fields addAttributes(from: resource, to: &serializedData ) From 9e6d4906c77e453fb39e28827e42ab5660469eeb Mon Sep 17 00:00:00 2001 From: Mihran Dovlatyan Date: Mon, 14 May 2018 11:09:08 +0200 Subject: [PATCH 10/10] cleanup --- Spine/Serializing.swift | 6 ------ 1 file changed, 6 deletions(-) diff --git a/Spine/Serializing.swift b/Spine/Serializing.swift index 2bc91d61..49bda4b8 100644 --- a/Spine/Serializing.swift +++ b/Spine/Serializing.swift @@ -80,12 +80,6 @@ public class Serializer { let document = JSONAPIDocument(data: resources, included: nil, errors: nil, meta: nil, links: nil, jsonapi: nil) return try serializeDocument(document, options: options) } - - public func serializeResources(_ resources: [Resource], meta: Metadata?, options: SerializationOptions = [.IncludeID]) throws -> Data { - let document = JSONAPIDocument(data: resources, included: nil, errors: nil, meta: meta, links: nil, jsonapi: nil) - return try serializeDocument(document, options: options) - } - /// Converts the given resource to link data, and serializes it into NSData. /// `{"data": { "type": "people", "id": "12" }}`