fix: allow clearing a to-one relationship with an explicit null id - #7
Merged
Conversation
A relationship object with `id: null` now serializes to `relationships.<key>.data = null`, the shape JSON:API defines for removing a to-one relationship. This mirrors the to-many case, where `[]` already produced `data: []`. An object with no `id` key keeps throwing, so a forgotten key stays a loud error instead of silently clearing a relationship. A bare `field: null` remains an attribute. Also bumps the version to 2.2.0 and documents clearing relationships in the README and docs site. Closes #6 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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.
Closes #6
Problem
There was no way to clear a to-one relationship. JSON:API defines
data: nullfor it, but the serializer could not produce that shape:field: nullbecame an attribute (attributes.field = null).field: { type: 'documents', id: null }threwAll included resources must have an ID.Meanwhile
field: []already producedrelationships.field.data = []for the to-many case.Change
A relationship object with an explicit
id: nullnow serializes torelationships.<key>.data = nulland is not collected intoincluded. BecauseextractResourceis shared, this works for nested resources too.Behavior kept on purpose:
idkey still throwsAll included resources must have an ID., so a forgotten key stays a loud error instead of silently clearing a relationship.field: nullis still an attribute — the shape alone cannot tell an attribute from a relationship.Tests
Three new cases in
tests/serialize.test.ts: withtype, withouttype, and on a nested resource. Full suite passes (64), plustypecheck,lint, andbuild.Release
Version bumped to 2.2.0 with a
CHANGELOG.mdentry. Clearing relationships is documented in the README and on the docs site.Not included
The issue also notes that a pre-built JSON:API document passed to
request()is dropped whentypeis absent. Left out of this PR, as the author suggested tracking it separately.🤖 Generated with Claude Code