From 99d9d96dbb674f8f2aa8c0f1254837cbc9533c22 Mon Sep 17 00:00:00 2001 From: Luke S Date: Mon, 2 Dec 2019 18:42:03 -0500 Subject: [PATCH] Permit missing data key for optional relationships This prevents a decoding error when `included[].relationships.*.data` is missing, using the same logic as if it was nil. --- Sources/JSONAPI/Resource/Relationship.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/JSONAPI/Resource/Relationship.swift b/Sources/JSONAPI/Resource/Relationship.swift index a11031e..67765b6 100644 --- a/Sources/JSONAPI/Resource/Relationship.swift +++ b/Sources/JSONAPI/Resource/Relationship.swift @@ -170,7 +170,7 @@ extension ToOneRelationship: Codable where Identifiable.Identifier: OptionalId { // succeeds and then attempt to coerce nil to a Identifier // type at which point we can store nil in `id`. let anyNil: Any? = nil - if try container.decodeNil(forKey: .data) { + if try !container.contains(.data) || container.decodeNil(forKey: .data) { guard let val = anyNil as? Identifiable.Identifier else { throw DecodingError.valueNotFound( Self.self,