Skip to content

Commit 9b6392a

Browse files
committed
chore: allow absent data in ToManyRelationship
1 parent c5d1e71 commit 9b6392a

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

Sources/JSONAPI/Resource/Relationship.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,11 @@ extension ToManyRelationship: Codable {
401401
links = try container.decode(LinksType.self, forKey: .links)
402402
}
403403

404+
guard container.contains(.data) else {
405+
idsWithMeta = []
406+
return
407+
}
408+
404409
var identifiers: UnkeyedDecodingContainer
405410
do {
406411
identifiers = try container.nestedUnkeyedContainer(forKey: .data)

Tests/JSONAPITests/Relationships/RelationshipTests.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,14 @@ extension RelationshipTests {
235235
test_DecodeEncodeEquality(type: ToManyWithMetaAndLinks.self,
236236
data: to_many_relationship_with_meta_and_links)
237237
}
238+
239+
func test_ToManyRelationshipWithMetaNoData() {
240+
let relationship = decoded(type: ToManyWithMeta.self,
241+
data: to_many_relationship_with_meta_no_data)
242+
243+
XCTAssertEqual(relationship.ids, [])
244+
XCTAssertEqual(relationship.meta.a, "hello")
245+
}
238246
}
239247

240248
// MARK: Nullable

Tests/JSONAPITests/Relationships/stubs/RelationshipStubs.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,11 @@ let to_many_relationship_type_mismatch = """
241241
]
242242
}
243243
""".data(using: .utf8)!
244+
245+
let to_many_relationship_with_meta_no_data = """
246+
{
247+
"meta": {
248+
"a": "hello"
249+
}
250+
}
251+
""".data(using: .utf8)!

0 commit comments

Comments
 (0)