Skip to content

Commit

Permalink
allow optional metadata to be omitted as well as nulled out.
Browse files Browse the repository at this point in the history
  • Loading branch information
mattpolzin committed Apr 30, 2024
1 parent 3856a88 commit 83cf5bb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
3 changes: 0 additions & 3 deletions Sources/JSONAPI/Resource/Relationship.swift
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,6 @@ extension MetaRelationship: Codable {
}
}

fileprivate protocol _Optional {}
extension Optional: _Optional {}

extension ToOneRelationship: Codable where Identifiable.ID: OptionalId {
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: ResourceLinkageCodingKeys.self)
Expand Down
12 changes: 11 additions & 1 deletion Sources/JSONAPI/Resource/Resource Object/ResourceObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,17 @@ public extension ResourceObject {
)
}

meta = try (NoMetadata() as? MetaType) ?? container.decode(MetaType.self, forKey: .meta)
do {
meta = try (NoMetadata() as? MetaType) ?? container.decode(MetaType.self, forKey: .meta)
} catch let decodingError as DecodingError {
let anyNil: Any? = nil

guard case .keyNotFound = decodingError,
let omittedMeta = anyNil as? MetaType else {
throw decodingError
}
meta = omittedMeta
}

links = try (NoLinks() as? LinksType) ?? container.decode(LinksType.self, forKey: .links)
}
Expand Down
22 changes: 20 additions & 2 deletions Tests/JSONAPITests/ResourceObject/ResourceObjectTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ extension ResourceObjectTests {
data: entity_no_relationships_no_attributes)
}

func test_EntityNoMetaDecodesAsOptionalMeta() {
let _ = decoded(type: TestEntityOptionalMeta.self,
data: entity_no_relationships_no_attributes)
}

func test_EntityNoRelationshipsSomeAttributes() {
let entity = decoded(type: TestEntity5.self,
data: entity_no_relationships_some_attributes)
Expand Down Expand Up @@ -861,13 +866,13 @@ extension ResourceObjectTests {

public struct Relationships: JSONAPI.Relationships {
public init() {
optionalMeta = nil
optionalMeta = nil
optionalOne = nil
optionalNullableOne = nil
optionalMany = nil
}

let optionalMeta: MetaRelationship<TestEntityMeta, NoLinks>?
let optionalMeta: MetaRelationship<TestEntityMeta, NoLinks>?

let optionalOne: ToOneRelationship<TestEntity1, NoIdMetadata, NoMetadata, NoLinks>?

Expand All @@ -879,6 +884,19 @@ extension ResourceObjectTests {

typealias TestEntity12 = BasicEntity<TestEntityType12>

enum TestEntityOptionalMetaType: JSONAPI.ResourceObjectDescription {
public static var jsonType: String { return "test_entities" }

typealias Attributes = NoAttributes
typealias Relationships = NoRelationships
}

struct UnimportantMeta: JSONAPI.Meta {
let property1: String
}

typealias TestEntityOptionalMeta = JSONAPI.ResourceObject<TestEntityOptionalMetaType, UnimportantMeta?, NoLinks, String>

enum UnidentifiedTestEntityType: ResourceObjectDescription {
public static var jsonType: String { return "unidentified_test_entities" }

Expand Down

0 comments on commit 83cf5bb

Please sign in to comment.