Skip to content

Commit

Permalink
Removes embeded parameters that are not expected in include list (#22)
Browse files Browse the repository at this point in the history
* Removes embeded parameters that are not expected in include list
* Fixed tests
  • Loading branch information
z-turk3 authored Dec 23, 2024
1 parent ddfc83b commit 9b1936a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
10 changes: 8 additions & 2 deletions Halley/Core/Traverser/Traverser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,22 @@ private extension Traverser {
)
}

var responseModel = resource.parameters
// Remove parameters for relationships that are not included
// eg. remove embedded relationship that we do not include
resource._links?.relationships
.filter { rel in !relationshipsToFetch.contains(where: { $0.relationship == rel.key }) }
.forEach { responseModel.removeValue(forKey: $0.key) }

// Ensure .zip() doesn't end up with Empty Publisher which produces no elements
// and ends whole pipeline
guard requests.isEmpty == false else {
return .success(resource.parameters)
return .success(responseModel)
}

return requests
.zip()
.map { responses -> JSONResult in
var responseModel = resource.parameters
for response in responses {
switch response.result {
case .success(let value):
Expand Down
10 changes: 5 additions & 5 deletions Tests/Decoding/SingleResourceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,31 @@ import Halley
final class SingleResourceTests: XCTestCase {

func testDecodingAttributes() throws {
let fetcher = HalleyResourceFetcher(fromJson: "simple_single_resource")
let fetcher = HalleyResourceFetcher(fromJson: "simple_single_resource", for: Contact.self, includeType: .full)
let person = try awaitPublisher(fetcher.resource(ofType: Contact.self))
XCTAssertTrue(person.name == "Matthew Weier O'Phinney")
}

func testDecodingToOneRelationship() throws {
let fetcher = HalleyResourceFetcher(fromJson: "simple_single_resource")
let fetcher = HalleyResourceFetcher(fromJson: "simple_single_resource", for: Contact.self, includeType: .full)
let person = try awaitPublisher(fetcher.resource(ofType: Contact.self))
XCTAssertNotNil(person.website)
}

func testDecodingToManyRelationship() throws {
let fetcher = HalleyResourceFetcher(fromJson: "simple_single_resource")
let fetcher = HalleyResourceFetcher(fromJson: "simple_single_resource", for: Contact.self, includeType: .full)
let person = try awaitPublisher(fetcher.resource(ofType: Contact.self))
XCTAssertEqual(person.contacts?.count, 2)
}

func testDecodingSelfLink() throws {
let fetcher = HalleyResourceFetcher(fromJson: "simple_single_resource")
let fetcher = HalleyResourceFetcher(fromJson: "simple_single_resource", for: Contact.self, includeType: .full)
let person = try awaitPublisher(fetcher.resource(ofType: Contact.self))
XCTAssertEqual(person._links?.selfLink?.href, "http://example.org/api/user/matthew")
}

func testDecodingRelationshipSelfLink() throws {
let fetcher = HalleyResourceFetcher(fromJson: "simple_single_resource")
let fetcher = HalleyResourceFetcher(fromJson: "simple_single_resource", for: Contact.self, includeType: .full)
let person = try awaitPublisher(fetcher.resource(ofType: Contact.self))
XCTAssertEqual(person.website?._links?.selfLink?.href, "http://example.org/api/locations/mwop")
}
Expand Down

0 comments on commit 9b1936a

Please sign in to comment.