diff --git a/Sources/JSONAPI/Resource/Entity.swift b/Sources/JSONAPI/Resource/Entity.swift index 7620fb8..6fe7a03 100644 --- a/Sources/JSONAPI/Resource/Entity.swift +++ b/Sources/JSONAPI/Resource/Entity.swift @@ -80,7 +80,7 @@ public protocol IdentifiableEntityType: EntityType, Relatable where EntityRawIdT /// encoded to or decoded from a JSON API /// "Resource Object." /// See https://jsonapi.org/format/#document-resource-objects -public struct Entity: EntityType { +public struct Entity: EntityType { /// The `Entity`'s Id. This can be of type `Unidentified` if /// the entity is being created clientside and the /// server is being asked to create a unique Id. Otherwise, @@ -92,11 +92,19 @@ public struct Entity + typealias TestType = BasicEntity enum DoubleToString: Transformer { public static func transform(_ from: Double) -> String { diff --git a/Tests/JSONAPITests/Computed Properties/ComputedPropertiesTests.swift b/Tests/JSONAPITests/Computed Properties/ComputedPropertiesTests.swift index a72b282..6cefcb1 100644 --- a/Tests/JSONAPITests/Computed Properties/ComputedPropertiesTests.swift +++ b/Tests/JSONAPITests/Computed Properties/ComputedPropertiesTests.swift @@ -57,5 +57,5 @@ extension ComputedPropertiesTests { } } - public typealias TestType = Entity + public typealias TestType = BasicEntity } diff --git a/Tests/JSONAPITests/Document/DocumentTests.swift b/Tests/JSONAPITests/Document/DocumentTests.swift index 8066e21..cad7a4a 100644 --- a/Tests/JSONAPITests/Document/DocumentTests.swift +++ b/Tests/JSONAPITests/Document/DocumentTests.swift @@ -970,7 +970,7 @@ extension DocumentTests { typealias Relationships = NoRelationships } - typealias Author = Entity + typealias Author = BasicEntity enum ArticleType: EntityDescription { static var type: String { return "articles" } @@ -982,7 +982,7 @@ extension DocumentTests { } } - typealias Article = Entity + typealias Article = BasicEntity struct TestPageMetadata: JSONAPI.Meta { let total: Int diff --git a/Tests/JSONAPITests/Entity/EntityTests.swift b/Tests/JSONAPITests/Entity/EntityTests.swift index e40038b..8ad511a 100644 --- a/Tests/JSONAPITests/Entity/EntityTests.swift +++ b/Tests/JSONAPITests/Entity/EntityTests.swift @@ -13,14 +13,14 @@ class EntityTests: XCTestCase { func test_relationship_access() { let entity1 = TestEntity1() - let entity2 = TestEntity2(other: entity1.pointer) + let entity2 = TestEntity2(relationships: .init(other: entity1.pointer)) XCTAssertEqual(entity2.relationships.other, entity1.pointer) } func test_relationship_operator_access() { let entity1 = TestEntity1() - let entity2 = TestEntity2(other: entity1.pointer) + let entity2 = TestEntity2(relationships: .init(other: entity1.pointer)) XCTAssertEqual(entity2 ~> \.other, entity1.id) } @@ -29,14 +29,14 @@ class EntityTests: XCTestCase { let entity1 = TestEntity1() let entity2 = TestEntity1() let entity4 = TestEntity1() - let entity3 = TestEntity3(others: .init(relationships: [entity1.pointer, entity2.pointer, entity4.pointer])) + let entity3 = TestEntity3(relationships: .init(others: .init(relationships: [entity1.pointer, entity2.pointer, entity4.pointer]))) XCTAssertEqual(entity3 ~> \.others, [entity1.id, entity2.id, entity4.id]) } func test_relationshipIds() { let entity1 = TestEntity1() - let entity2 = TestEntity2(other: entity1.pointer) + let entity2 = TestEntity2(relationships: .init(other: entity1.pointer)) XCTAssertEqual(entity2.relationships.other.id, entity1.id) } @@ -346,7 +346,7 @@ extension EntityTests { typealias Relationships = NoRelationships } - typealias TestEntity1 = Entity + typealias TestEntity1 = BasicEntity enum TestEntityType2: EntityDescription { static var type: String { return "second_test_entities"} @@ -358,7 +358,7 @@ extension EntityTests { } } - typealias TestEntity2 = Entity + typealias TestEntity2 = BasicEntity enum TestEntityType3: EntityDescription { static var type: String { return "third_test_entities"} @@ -370,7 +370,7 @@ extension EntityTests { } } - typealias TestEntity3 = Entity + typealias TestEntity3 = BasicEntity enum TestEntityType4: EntityDescription { static var type: String { return "fourth_test_entities"} @@ -386,7 +386,7 @@ extension EntityTests { } } - typealias TestEntity4 = Entity + typealias TestEntity4 = BasicEntity enum TestEntityType5: EntityDescription { static var type: String { return "fifth_test_entities"} @@ -398,7 +398,7 @@ extension EntityTests { } } - typealias TestEntity5 = Entity + typealias TestEntity5 = BasicEntity enum TestEntityType6: EntityDescription { static var type: String { return "sixth_test_entities" } @@ -412,7 +412,7 @@ extension EntityTests { } } - typealias TestEntity6 = Entity + typealias TestEntity6 = BasicEntity enum TestEntityType7: EntityDescription { static var type: String { return "seventh_test_entities" } @@ -425,7 +425,7 @@ extension EntityTests { } } - typealias TestEntity7 = Entity + typealias TestEntity7 = BasicEntity enum TestEntityType8: EntityDescription { static var type: String { return "eighth_test_entities" } @@ -443,7 +443,7 @@ extension EntityTests { } } - typealias TestEntity8 = Entity + typealias TestEntity8 = BasicEntity enum TestEntityType9: EntityDescription { public static var type: String { return "ninth_test_entities" } @@ -465,7 +465,7 @@ extension EntityTests { } } - typealias TestEntity9 = Entity + typealias TestEntity9 = BasicEntity enum TestEntityType10: EntityDescription { public static var type: String { return "tenth_test_entities" } @@ -478,7 +478,7 @@ extension EntityTests { } } - typealias TestEntity10 = Entity + typealias TestEntity10 = BasicEntity enum TestEntityType11: EntityDescription { public static var type: String { return "eleventh_test_entities" } @@ -490,7 +490,7 @@ extension EntityTests { typealias Relationships = NoRelationships } - typealias TestEntity11 = Entity + typealias TestEntity11 = BasicEntity enum UnidentifiedTestEntityType: EntityDescription { public static var type: String { return "unidentified_test_entities" } @@ -502,7 +502,7 @@ extension EntityTests { typealias Relationships = NoRelationships } - typealias UnidentifiedTestEntity = NewEntity + typealias UnidentifiedTestEntity = NewEntity enum IntToString: Transformer { public static func transform(_ from: Int) -> String { @@ -541,15 +541,3 @@ extension EntityTests { } } } - -extension Entity where Description == EntityTests.TestEntityType2, EntityRawIdType: CreatableRawIdType { - init(other: ToOneRelationship) { - self.init(relationships: .init(other: other)) - } -} - -extension Entity where Description == EntityTests.TestEntityType3, EntityRawIdType: CreatableRawIdType { - init(others: ToManyRelationship) { - self.init(relationships: .init(others: others)) - } -} diff --git a/Tests/JSONAPITests/Includes/IncludeTests.swift b/Tests/JSONAPITests/Includes/IncludeTests.swift index 112c7d7..971df36 100644 --- a/Tests/JSONAPITests/Includes/IncludeTests.swift +++ b/Tests/JSONAPITests/Includes/IncludeTests.swift @@ -137,7 +137,7 @@ extension IncludedTests { } } - typealias TestEntity = Entity + typealias TestEntity = BasicEntity enum TestEntityType2: EntityDescription { @@ -153,7 +153,7 @@ extension IncludedTests { } } - typealias TestEntity2 = Entity + typealias TestEntity2 = BasicEntity enum TestEntityType3: EntityDescription { @@ -167,7 +167,7 @@ extension IncludedTests { } } - typealias TestEntity3 = Entity + typealias TestEntity3 = BasicEntity enum TestEntityType4: EntityDescription { @@ -178,7 +178,7 @@ extension IncludedTests { public static var type: String { return "test_entity4" } } - typealias TestEntity4 = Entity + typealias TestEntity4 = BasicEntity enum TestEntityType5: EntityDescription { @@ -189,7 +189,7 @@ extension IncludedTests { public static var type: String { return "test_entity5" } } - typealias TestEntity5 = Entity + typealias TestEntity5 = BasicEntity enum TestEntityType6: EntityDescription { @@ -202,5 +202,5 @@ extension IncludedTests { } } - typealias TestEntity6 = Entity + typealias TestEntity6 = BasicEntity } diff --git a/Tests/JSONAPITests/JSONAPITestLib/EntityCheckTests.swift b/Tests/JSONAPITests/JSONAPITestLib/EntityCheckTests.swift index 192db8f..cfda469 100644 --- a/Tests/JSONAPITests/JSONAPITestLib/EntityCheckTests.swift +++ b/Tests/JSONAPITests/JSONAPITestLib/EntityCheckTests.swift @@ -47,7 +47,7 @@ extension EntityCheckTests { public typealias Relationships = NoRelationships } - public typealias OkEntity = Entity + public typealias OkEntity = BasicEntity enum OtherOkDescription: EntityDescription { public static var type: String { return "hmm" } @@ -56,7 +56,7 @@ extension EntityCheckTests { public typealias Relationships = NoRelationships } - public typealias OtherOkEntity = Entity + public typealias OtherOkEntity = BasicEntity enum EnumAttributesDescription: EntityDescription { public static var type: String { return "hello" } @@ -75,7 +75,7 @@ extension EntityCheckTests { public typealias Relationships = NoRelationships } - public typealias EnumAttributesEntity = Entity + public typealias EnumAttributesEntity = BasicEntity enum EnumRelationshipsDescription: EntityDescription { public static var type: String { return "hello" } @@ -94,7 +94,7 @@ extension EntityCheckTests { } } - public typealias EnumRelationshipsEntity = Entity + public typealias EnumRelationshipsEntity = BasicEntity enum BadAttributeDescription: EntityDescription { public static var type: String { return "hello" } @@ -107,7 +107,7 @@ extension EntityCheckTests { public typealias Relationships = NoRelationships } - public typealias BadAttributeEntity = Entity + public typealias BadAttributeEntity = BasicEntity enum BadRelationshipDescription: EntityDescription { public static var type: String { return "hello" } @@ -120,7 +120,7 @@ extension EntityCheckTests { } } - public typealias BadRelationshipEntity = Entity + public typealias BadRelationshipEntity = BasicEntity enum OptionalArrayAttributeDescription: EntityDescription { public static var type: String { return "hello" } @@ -133,5 +133,5 @@ extension EntityCheckTests { public typealias Relationships = NoRelationships } - public typealias OptionalArrayAttributeEntity = Entity + public typealias OptionalArrayAttributeEntity = BasicEntity } diff --git a/Tests/JSONAPITests/JSONAPITestLib/Id+LiteralTests.swift b/Tests/JSONAPITests/JSONAPITestLib/Id+LiteralTests.swift index 0cefdae..d45fcc5 100644 --- a/Tests/JSONAPITests/JSONAPITestLib/Id+LiteralTests.swift +++ b/Tests/JSONAPITests/JSONAPITestLib/Id+LiteralTests.swift @@ -31,5 +31,5 @@ extension Id_LiteralTests { public typealias Relationships = NoRelationships } - typealias TestEntity = Entity + typealias TestEntity = BasicEntity } diff --git a/Tests/JSONAPITests/JSONAPITestLib/Relationship+LiteralTests.swift b/Tests/JSONAPITests/JSONAPITestLib/Relationship+LiteralTests.swift index 42ec030..87ee0f0 100644 --- a/Tests/JSONAPITests/JSONAPITestLib/Relationship+LiteralTests.swift +++ b/Tests/JSONAPITests/JSONAPITestLib/Relationship+LiteralTests.swift @@ -34,5 +34,5 @@ extension Relationship_LiteralTests { public typealias Relationships = NoRelationships } - typealias TestEntity = Entity + typealias TestEntity = BasicEntity } diff --git a/Tests/JSONAPITests/Links/LinksTests.swift b/Tests/JSONAPITests/Links/LinksTests.swift index 713eeaf..15f5dd3 100644 --- a/Tests/JSONAPITests/Links/LinksTests.swift +++ b/Tests/JSONAPITests/Links/LinksTests.swift @@ -13,6 +13,7 @@ class LinksTests: XCTestCase { let links = decoded(type: LinksTests.Links.self, data: link_without_meta) XCTAssertEqual(links.link.url, "https://website.com/path/file") + XCTAssertEqual(links.optionalLink?.url, "https://theweb.com/not") XCTAssertEqual(links.link.meta, NoMetadata()) } @@ -20,6 +21,18 @@ class LinksTests: XCTestCase { test_DecodeEncodeEquality(type: LinksTests.Links.self, data: link_without_meta) } + func test_linkWithNoMetaWithoutOptionalLink() { + let links = decoded(type: LinksTests.Links.self, data: link_without_meta_without_optional_link) + + XCTAssertEqual(links.link.url, "https://website.com/path/file") + XCTAssertNil(links.optionalLink) + XCTAssertEqual(links.link.meta, NoMetadata()) + } + + func test_linkWithNoMetaWithoutOptionalLink_encode() { + test_DecodeEncodeEquality(type: LinksTests.Links.self, data: link_without_meta_without_optional_link) + } + func test_linkWithNullMetadata() { let link = decoded(type: Link.self, data: link_with_null_meta) @@ -60,6 +73,7 @@ extension LinksTests { struct Links: JSONAPI.Links { let link: Link + let optionalLink: Link? } struct Metadata: JSONAPI.Meta { diff --git a/Tests/JSONAPITests/Links/stub/LinkStubs.swift b/Tests/JSONAPITests/Links/stub/LinkStubs.swift index 6c0563f..ce94f91 100644 --- a/Tests/JSONAPITests/Links/stub/LinkStubs.swift +++ b/Tests/JSONAPITests/Links/stub/LinkStubs.swift @@ -6,6 +6,13 @@ // let link_without_meta = """ +{ + "link": "https://website.com/path/file", + "optionalLink": "https://theweb.com/not" +} +""".data(using: .utf8)! + +let link_without_meta_without_optional_link = """ { "link": "https://website.com/path/file" } diff --git a/Tests/JSONAPITests/Poly/PolyProxyTests.swift b/Tests/JSONAPITests/Poly/PolyProxyTests.swift index 6863cff..01e5947 100644 --- a/Tests/JSONAPITests/Poly/PolyProxyTests.swift +++ b/Tests/JSONAPITests/Poly/PolyProxyTests.swift @@ -83,8 +83,8 @@ public extension PolyProxyTests { public typealias Relationships = NoRelationships } - public typealias UserA = Entity - public typealias UserB = Entity + public typealias UserA = BasicEntity + public typealias UserB = BasicEntity public typealias User = Poly2 } diff --git a/Tests/JSONAPITests/Poly/PolyTests.swift b/Tests/JSONAPITests/Poly/PolyTests.swift index 10f901e..614afbb 100644 --- a/Tests/JSONAPITests/Poly/PolyTests.swift +++ b/Tests/JSONAPITests/Poly/PolyTests.swift @@ -275,7 +275,7 @@ extension PolyTests { } } - typealias TestEntity = Entity + typealias TestEntity = BasicEntity enum TestEntityType2: EntityDescription { @@ -291,7 +291,7 @@ extension PolyTests { } } - typealias TestEntity2 = Entity + typealias TestEntity2 = BasicEntity enum TestEntityType3: EntityDescription { @@ -305,7 +305,7 @@ extension PolyTests { } } - typealias TestEntity3 = Entity + typealias TestEntity3 = BasicEntity enum TestEntityType4: EntityDescription { @@ -316,7 +316,7 @@ extension PolyTests { public static var type: String { return "test_entity4" } } - typealias TestEntity4 = Entity + typealias TestEntity4 = BasicEntity enum TestEntityType5: EntityDescription { @@ -327,7 +327,7 @@ extension PolyTests { public static var type: String { return "test_entity5" } } - typealias TestEntity5 = Entity + typealias TestEntity5 = BasicEntity enum TestEntityType6: EntityDescription { @@ -340,5 +340,5 @@ extension PolyTests { } } - typealias TestEntity6 = Entity + typealias TestEntity6 = BasicEntity } diff --git a/Tests/JSONAPITests/Relationships/RelationshipTests.swift b/Tests/JSONAPITests/Relationships/RelationshipTests.swift index 35fe44d..40ba9e6 100644 --- a/Tests/JSONAPITests/Relationships/RelationshipTests.swift +++ b/Tests/JSONAPITests/Relationships/RelationshipTests.swift @@ -81,5 +81,5 @@ extension RelationshipTests { public static var type: String { return "test_entity1" } } - typealias TestEntity1 = Entity + typealias TestEntity1 = BasicEntity } diff --git a/Tests/JSONAPITests/ResourceBody/ResourceBodyTests.swift b/Tests/JSONAPITests/ResourceBody/ResourceBodyTests.swift index 124c5d4..7566b03 100644 --- a/Tests/JSONAPITests/ResourceBody/ResourceBodyTests.swift +++ b/Tests/JSONAPITests/ResourceBody/ResourceBodyTests.swift @@ -61,5 +61,5 @@ class ResourceBodyTests: XCTestCase { } } - typealias Article = Entity + typealias Article = BasicEntity } diff --git a/Tests/JSONAPITests/Test Helpers/Entity+Id.swift b/Tests/JSONAPITests/Test Helpers/Entity+Id.swift index edf5794..3ad2dde 100644 --- a/Tests/JSONAPITests/Test Helpers/Entity+Id.swift +++ b/Tests/JSONAPITests/Test Helpers/Entity+Id.swift @@ -7,6 +7,8 @@ import JSONAPI -public typealias Entity = JSONAPI.Entity +public typealias Entity = JSONAPI.Entity -public typealias NewEntity = JSONAPI.Entity +public typealias BasicEntity = Entity + +public typealias NewEntity = JSONAPI.Entity