From 396f8453d169b39873bb29a0ec339804ec60c1ab Mon Sep 17 00:00:00 2001 From: Mathew Polzin Date: Mon, 29 Jul 2019 21:00:44 -0700 Subject: [PATCH] swap out broken nil checks (not harming anything, but not working as evidenced by not getting hit by any test cases) --- Sources/JSONAPI/Document/ResourceBody.swift | 10 ++++++---- Sources/JSONAPI/Resource/Relationship.swift | 4 ---- .../JSONAPITests/ResourceBody/ResourceBodyTests.swift | 3 +++ 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/Sources/JSONAPI/Document/ResourceBody.swift b/Sources/JSONAPI/Document/ResourceBody.swift index eed341e..b8f170c 100644 --- a/Sources/JSONAPI/Document/ResourceBody.swift +++ b/Sources/JSONAPI/Document/ResourceBody.swift @@ -71,10 +71,12 @@ extension SingleResourceBody { public func encode(to encoder: Encoder) throws { var container = encoder.singleValueContainer() - if (value as Any?) == nil { - try container.encodeNil() - return - } + let anyNil: Any? = nil + let nilValue = anyNil as? Entity + guard value != nilValue else { + try container.encodeNil() + return + } try container.encode(value) } diff --git a/Sources/JSONAPI/Resource/Relationship.swift b/Sources/JSONAPI/Resource/Relationship.swift index f6a4bdc..43f5457 100644 --- a/Sources/JSONAPI/Resource/Relationship.swift +++ b/Sources/JSONAPI/Resource/Relationship.swift @@ -190,10 +190,6 @@ extension ToOneRelationship: Codable where Identifiable.Identifier: OptionalId { public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: ResourceLinkageCodingKeys.self) - if (id as Any?) == nil { - try container.encodeNil(forKey: .data) - } - if MetaType.self != NoMetadata.self { try container.encode(meta, forKey: .meta) } diff --git a/Tests/JSONAPITests/ResourceBody/ResourceBodyTests.swift b/Tests/JSONAPITests/ResourceBody/ResourceBodyTests.swift index a20db8c..9bc3f2c 100644 --- a/Tests/JSONAPITests/ResourceBody/ResourceBodyTests.swift +++ b/Tests/JSONAPITests/ResourceBody/ResourceBodyTests.swift @@ -100,6 +100,9 @@ class ResourceBodyTests: XCTestCase { XCTAssertEqual(combined.values.count, 3) XCTAssertEqual(combined.values, body1.values + body2.values) } +} + +extension ResourceBodyTests { enum ArticleType: ResourceObjectDescription { public static var jsonType: String { return "articles" }