From b9cb23d6235917a50a1386e06d39e67c0deffbda Mon Sep 17 00:00:00 2001 From: Vitalii Budnik Date: Wed, 20 Sep 2023 18:53:51 +0300 Subject: [PATCH 1/2] chore: allow mixing meta while merging Body.Data MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- Sources/JSONAPI/Document/Document.swift | 11 ++++++----- Tests/JSONAPITests/Document/DocumentTests.swift | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/Sources/JSONAPI/Document/Document.swift b/Sources/JSONAPI/Document/Document.swift index da0a37f..b10a97f 100644 --- a/Sources/JSONAPI/Document/Document.swift +++ b/Sources/JSONAPI/Document/Document.swift @@ -261,7 +261,7 @@ extension Document { } extension Document.Body.Data where PrimaryResourceBody: ResourceBodyAppendable { - public func merging(_ other: Document.Body.Data, + public func merging(_ other: Document.Body.Data, combiningMetaWith metaMerge: (MetaType, MetaType) -> MetaType, combiningLinksWith linksMerge: (LinksType, LinksType) -> LinksType) -> Document.Body.Data { return Document.Body.Data(primary: primary.appending(other.primary), @@ -272,10 +272,11 @@ extension Document.Body.Data where PrimaryResourceBody: ResourceBodyAppendable { } extension Document.Body.Data where PrimaryResourceBody: ResourceBodyAppendable, MetaType == NoMetadata, LinksType == NoLinks { - public func merging(_ other: Document.Body.Data) -> Document.Body.Data { - return merging(other, - combiningMetaWith: { _, _ in .none }, - combiningLinksWith: { _, _ in .none }) + public func merging(_ other: Document.Body.Data) -> Document.Body.Data { + return Document.Body.Data(primary: primary.appending(other.primary), + includes: includes.appending(other.includes), + meta: meta, + links: links) } } diff --git a/Tests/JSONAPITests/Document/DocumentTests.swift b/Tests/JSONAPITests/Document/DocumentTests.swift index d6058ec..1acc297 100644 --- a/Tests/JSONAPITests/Document/DocumentTests.swift +++ b/Tests/JSONAPITests/Document/DocumentTests.swift @@ -1528,6 +1528,23 @@ extension DocumentTests { XCTAssertEqual(combined.primary.values, bodyData1.primary.values + bodyData2.primary.values) } + public func test_MergeBodyDataMixedMetaLinksErrorAndAPI(){ + let entity1 = Article(attributes: .none, relationships: .init(author: "2"), meta: .none, links: .none) + let entity2 = Article(attributes: .none, relationships: .init(author: "3"), meta: .none, links: .none) + + let bodyData1 = Document, NoMetadata, NoLinks, NoIncludes, NoAPIDescription, UnknownJSONAPIError>.Body.Data(primary: .init(resourceObjects: [entity1]), + includes: .none, + meta: .none, + links: .none) + let bodyData2 = Document, TestPageMetadata, TestLinks, NoIncludes, TestAPIDescription, GenericJSONAPIError>.Body.Data(primary: .init(resourceObjects: [entity2]), + includes: .none, + meta: .init(total: 5, limit: 2, offset: 2), + links: .init(link: .init(url: "one"), link2: .init(url: .init(), meta: .init(hello: "world")))) + let combined = bodyData1.merging(bodyData2) + + XCTAssertEqual(combined.primary.values, bodyData1.primary.values + bodyData2.primary.values) + } + public func test_MergeBodyDataWithMergeFunctions() { let article1 = Article(attributes: .none, relationships: .init(author: "2"), meta: .none, links: .none) let author1 = Author(id: "2", attributes: .none, relationships: .none, meta: .none, links: .none) From 7f326df678d9b5f6d421488c4bd37da9d2d42c8b Mon Sep 17 00:00:00 2001 From: Mathew Polzin Date: Fri, 1 Mar 2024 13:36:00 -0600 Subject: [PATCH 2/2] keep the metadata and links for the merged-in document body if the merged-into document body has no metadata or links. --- Sources/JSONAPI/Document/Document.swift | 10 +++++----- Tests/JSONAPITests/Document/DocumentTests.swift | 2 ++ 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Sources/JSONAPI/Document/Document.swift b/Sources/JSONAPI/Document/Document.swift index b10a97f..3673493 100644 --- a/Sources/JSONAPI/Document/Document.swift +++ b/Sources/JSONAPI/Document/Document.swift @@ -272,11 +272,11 @@ extension Document.Body.Data where PrimaryResourceBody: ResourceBodyAppendable { } extension Document.Body.Data where PrimaryResourceBody: ResourceBodyAppendable, MetaType == NoMetadata, LinksType == NoLinks { - public func merging(_ other: Document.Body.Data) -> Document.Body.Data { - return Document.Body.Data(primary: primary.appending(other.primary), - includes: includes.appending(other.includes), - meta: meta, - links: links) + public func merging(_ other: Document.Body.Data) -> Document.Body.Data { + return .init(primary: primary.appending(other.primary), + includes: includes.appending(other.includes), + meta: other.meta, + links: other.links) } } diff --git a/Tests/JSONAPITests/Document/DocumentTests.swift b/Tests/JSONAPITests/Document/DocumentTests.swift index 1acc297..559a257 100644 --- a/Tests/JSONAPITests/Document/DocumentTests.swift +++ b/Tests/JSONAPITests/Document/DocumentTests.swift @@ -1543,6 +1543,8 @@ extension DocumentTests { let combined = bodyData1.merging(bodyData2) XCTAssertEqual(combined.primary.values, bodyData1.primary.values + bodyData2.primary.values) + XCTAssertEqual(combined.meta, bodyData2.meta) + XCTAssertEqual(combined.links, bodyData2.links) } public func test_MergeBodyDataWithMergeFunctions() {