Support absent attributes/relationships in resource decoding#20
Open
alanf wants to merge 1 commit into
Open
Conversation
The JSON:API spec allows a resource object's attributes and relationships members to be entirely absent, but Resource.init(from:) previously threw keyNotFound whenever either key was missing. Add an opt-in EmptyRepresentable protocol so a ResourceDefinition can declare tolerance for a missing key, and teach @ResourceWrapper to auto-synthesize that conformance when every attribute/relationship property is Optional. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
alanf
marked this pull request as ready for review
July 23, 2026 17:21
NicoMulet
reviewed
Jul 24, 2026
NicoMulet
left a comment
Contributor
There was a problem hiding this comment.
LGTM! Thanks for adding this important behavior to the library
| self.attributes = attributes | ||
| } else if !container.contains(.attributes), let emptyType = Attributes.self as? any EmptyRepresentable.Type | ||
| { | ||
| self.attributes = emptyType.empty as! Attributes |
Contributor
There was a problem hiding this comment.
Can we add this cast to the if condition, so that we avoid of making of force cast
| } else if !container.contains(.relationships), | ||
| let emptyType = Relationships.self as? any EmptyRepresentable.Type | ||
| { | ||
| self.relationships = emptyType.empty as! Relationships |
Contributor
There was a problem hiding this comment.
Same comment for this cast
Comment on lines
+346
to
+348
| } catch DecodingError.keyNotFound { | ||
| // then | ||
| } |
Contributor
There was a problem hiding this comment.
We need an extra catch block in order to also fail the test if the error is not the one we were expecting
Comment on lines
+158
to
+160
| } catch DecodingError.keyNotFound { | ||
| // then | ||
| } |
Contributor
There was a problem hiding this comment.
Same for this do-catch block
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Support absent
attributes/relationshipsin resource decodingPer the JSON:API spec, a resource
object's
attributesandrelationshipsmembers are optional — onlytype/idare required.Resourcepreviously threwkeyNotFoundif either was missing. This adds opt-in support fordecoding through that gap.
Changes
EmptyRepresentableprotocol: conformAttributes/Relationshipsto it, andResourcefalls back to
.emptywhen the corresponding key is absent, instead of throwing. (Malformed-but-presentdata still throws as before.)
@ResourceWrapperauto-synthesizes this conformance whenever every attribute/relationshipproperty is
Optional— the common case needs no extra annotation. Applied independently toattributes vs. relationships;
BodyDefinitionis excluded.Tradeoffs
"defaults-to-
[]" semantics added here).Swift.Optional<T>(only
T?/Optional<T>) — a pre-existing limitation in a shared macro helper, not aregression, and not worth touching for this rare spelling.