Skip to content

Commit 7712920

Browse files
committed
Add test that shows a custom generic included type of resource
1 parent 58ebdc9 commit 7712920

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

Tests/JSONAPITests/Includes/IncludeTests.swift

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@ class IncludedTests: XCTestCase {
5858
data: two_different_type_includes)
5959
}
6060

61+
func test_OneKnownAndOneGenericInclude() {
62+
let includes = decoded(type: Includes<Include2<TestEntity, TestEntityOther>>.self,
63+
data: two_different_type_includes)
64+
65+
XCTAssertEqual(includes[TestEntity.self].count, 1)
66+
XCTAssertEqual(includes[TestEntityOther.self].count, 1)
67+
XCTAssertEqual(includes[TestEntityOther.self][0].type, "test_entity2")
68+
}
69+
6170
func test_ThreeDifferentIncludes() {
6271
let includes = decoded(type: Includes<Include3<TestEntity, TestEntity2, TestEntity4>>.self,
6372
data: three_different_type_includes)
@@ -678,4 +687,50 @@ extension IncludedTests {
678687
}
679688

680689
typealias TestEntity15 = BasicEntity<TestEntityType15>
690+
691+
enum TestEntityTypeOther: ResourceObjectProxyDescription {
692+
public static var jsonType: String { return "_generic" }
693+
694+
typealias Attributes = NoAttributes
695+
typealias Relationships = NoRelationships
696+
}
697+
698+
struct TestEntityOther: ResourceObjectProxy, Codable {
699+
typealias Description = TestEntityTypeOther
700+
typealias EntityRawIdType = String
701+
702+
public let type : String
703+
public let id : JSONAPI.Id<String, Self>
704+
705+
public let attributes = NoAttributes()
706+
public let relationships = NoRelationships()
707+
708+
enum CodingKeys: CodingKey {
709+
case id
710+
case type
711+
}
712+
713+
init(from decoder: Decoder) throws {
714+
let container = try decoder.container(keyedBy: CodingKeys.self)
715+
716+
let type: String
717+
do {
718+
type = try container.decode(String.self, forKey: .type)
719+
} catch let error as DecodingError {
720+
throw ResourceObjectDecodingError(error, jsonAPIType: Self.jsonType)
721+
?? error
722+
}
723+
724+
id = .init(rawValue: try container.decode(String.self, forKey: .id))
725+
self.type = type
726+
}
727+
728+
func encode(to encoder: Encoder) throws {
729+
var container = encoder.container(keyedBy: CodingKeys.self)
730+
731+
try container.encode(type, forKey: .type)
732+
733+
try container.encode(id, forKey: .id)
734+
}
735+
}
681736
}

0 commit comments

Comments
 (0)