Skip to content

Commit 77ff77a

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

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
@@ -72,6 +72,15 @@ class IncludedTests: XCTestCase {
7272
data: three_different_type_includes)
7373
}
7474

75+
func test_OneKnownAndTwoGenericIncludes() {
76+
let includes = decoded(type: Includes<Include2<TestEntity, TestEntityOther>>.self,
77+
data: three_different_type_includes)
78+
79+
XCTAssertEqual(includes[TestEntity.self].count, 1)
80+
XCTAssertEqual(includes[TestEntityOther.self].count, 2)
81+
XCTAssert(includes[TestEntityOther.self].map { $0.type }.contains(["test_entity2", "test_entity4"]))
82+
}
83+
7584
func test_FourDifferentIncludes() {
7685
let includes = decoded(type: Includes<Include4<TestEntity, TestEntity2, TestEntity4, TestEntity6>>.self,
7786
data: four_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)