Skip to content

Commit b9cb23d

Browse files
committed
chore: allow mixing meta while merging Body.Data
Ignore irrelevant generic types when merging Document.Body.Data. ### Reasoning I have a paginated request and response data has no meta and links, because it’s irrelevant for me.
1 parent c5d1e71 commit b9cb23d

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

Sources/JSONAPI/Document/Document.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ extension Document {
261261
}
262262

263263
extension Document.Body.Data where PrimaryResourceBody: ResourceBodyAppendable {
264-
public func merging(_ other: Document.Body.Data,
264+
public func merging<OtherDescription, OtherError>(_ other: Document<PrimaryResourceBody, MetaType, LinksType, IncludeType, OtherDescription, OtherError>.Body.Data,
265265
combiningMetaWith metaMerge: (MetaType, MetaType) -> MetaType,
266266
combiningLinksWith linksMerge: (LinksType, LinksType) -> LinksType) -> Document.Body.Data {
267267
return Document.Body.Data(primary: primary.appending(other.primary),
@@ -272,10 +272,11 @@ extension Document.Body.Data where PrimaryResourceBody: ResourceBodyAppendable {
272272
}
273273

274274
extension Document.Body.Data where PrimaryResourceBody: ResourceBodyAppendable, MetaType == NoMetadata, LinksType == NoLinks {
275-
public func merging(_ other: Document.Body.Data) -> Document.Body.Data {
276-
return merging(other,
277-
combiningMetaWith: { _, _ in .none },
278-
combiningLinksWith: { _, _ in .none })
275+
public func merging<OtherMeta, OtherLinks, OtherDescription, OtherError>(_ other: Document<PrimaryResourceBody, OtherMeta, OtherLinks, IncludeType, OtherDescription, OtherError>.Body.Data) -> Document.Body.Data {
276+
return Document.Body.Data(primary: primary.appending(other.primary),
277+
includes: includes.appending(other.includes),
278+
meta: meta,
279+
links: links)
279280
}
280281
}
281282

Tests/JSONAPITests/Document/DocumentTests.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1528,6 +1528,23 @@ extension DocumentTests {
15281528
XCTAssertEqual(combined.primary.values, bodyData1.primary.values + bodyData2.primary.values)
15291529
}
15301530

1531+
public func test_MergeBodyDataMixedMetaLinksErrorAndAPI(){
1532+
let entity1 = Article(attributes: .none, relationships: .init(author: "2"), meta: .none, links: .none)
1533+
let entity2 = Article(attributes: .none, relationships: .init(author: "3"), meta: .none, links: .none)
1534+
1535+
let bodyData1 = Document<ManyResourceBody<Article>, NoMetadata, NoLinks, NoIncludes, NoAPIDescription, UnknownJSONAPIError>.Body.Data(primary: .init(resourceObjects: [entity1]),
1536+
includes: .none,
1537+
meta: .none,
1538+
links: .none)
1539+
let bodyData2 = Document<ManyResourceBody<Article>, TestPageMetadata, TestLinks, NoIncludes, TestAPIDescription, GenericJSONAPIError<String>>.Body.Data(primary: .init(resourceObjects: [entity2]),
1540+
includes: .none,
1541+
meta: .init(total: 5, limit: 2, offset: 2),
1542+
links: .init(link: .init(url: "one"), link2: .init(url: .init(), meta: .init(hello: "world"))))
1543+
let combined = bodyData1.merging(bodyData2)
1544+
1545+
XCTAssertEqual(combined.primary.values, bodyData1.primary.values + bodyData2.primary.values)
1546+
}
1547+
15311548
public func test_MergeBodyDataWithMergeFunctions() {
15321549
let article1 = Article(attributes: .none, relationships: .init(author: "2"), meta: .none, links: .none)
15331550
let author1 = Author(id: "2", attributes: .none, relationships: .none, meta: .none, links: .none)

0 commit comments

Comments
 (0)