Skip to content

Commit 8825783

Browse files
authored
Merge pull request #121 from mattpolzin/generic-included-types
Add test that shows a custom generic included type of resource
2 parents 58ebdc9 + 93981ef commit 8825783

File tree

2 files changed

+60
-14
lines changed

2 files changed

+60
-14
lines changed

.github/workflows/tests.yml

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,19 @@ jobs:
1313
fail-fast: false
1414
matrix:
1515
image:
16-
- swift:5.2-xenial
17-
- swift:5.2-bionic
1816
- swift:5.2-focal
1917
- swift:5.2-centos8
20-
- swift:5.2-amazonlinux2
21-
- swift:5.3-xenial
22-
- swift:5.3-bionic
2318
- swift:5.3-focal
2419
- swift:5.3-centos8
25-
- swift:5.3-amazonlinux2
26-
- swift:5.4-xenial
27-
- swift:5.4-bionic
2820
- swift:5.4-focal
2921
- swift:5.4-centos8
30-
- swift:5.4-amazonlinux2
31-
- swift:5.5-xenial
32-
- swift:5.5-bionic
3322
- swift:5.5-focal
3423
- swift:5.5-centos8
35-
- swift:5.5-amazonlinux2
36-
- swift:5.6-bionic
3724
- swift:5.6-focal
38-
- swift:5.6-amazonlinux2
25+
# - swift:5.7-focal
26+
# - swift:5.8-focal
27+
# - swift:5.9-focal
28+
# - swift:5.10-focal
3929
container: ${{ matrix.image }}
4030
steps:
4131
- name: Checkout code

Tests/JSONAPITests/Includes/IncludeTests.swift

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ 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].contains { $0.type == "test_entity2"})
82+
XCTAssert(includes[TestEntityOther.self].contains { $0.type == "test_entity4"})
83+
}
84+
7585
func test_FourDifferentIncludes() {
7686
let includes = decoded(type: Includes<Include4<TestEntity, TestEntity2, TestEntity4, TestEntity6>>.self,
7787
data: four_different_type_includes)
@@ -678,4 +688,50 @@ extension IncludedTests {
678688
}
679689

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

0 commit comments

Comments
 (0)