Skip to content

fix: allow clearing a to-one relationship with an explicit null id - #7

Merged
caiotarifa merged 1 commit into
mainfrom
fix/clear-to-one-relationship
Aug 4, 2026
Merged

fix: allow clearing a to-one relationship with an explicit null id#7
caiotarifa merged 1 commit into
mainfrom
fix/clear-to-one-relationship

Conversation

@caiotarifa

Copy link
Copy Markdown
Owner

Closes #6

Problem

There was no way to clear a to-one relationship. JSON:API defines data: null for it, but the serializer could not produce that shape:

  • field: null became an attribute (attributes.field = null).
  • field: { type: 'documents', id: null } threw All included resources must have an ID.

Meanwhile field: [] already produced relationships.field.data = [] for the to-many case.

Change

A relationship object with an explicit id: null now serializes to relationships.<key>.data = null and is not collected into included. Because extractResource is shared, this works for nested resources too.

await api.update('article', {
  id: '1',
  author: { id: null }, // relationships.author.data = null
  tags: []              // relationships.tags.data = []
})

Behavior kept on purpose:

  • An object with no id key still throws All included resources must have an ID., so a forgotten key stays a loud error instead of silently clearing a relationship.
  • A bare field: null is still an attribute — the shape alone cannot tell an attribute from a relationship.

Tests

Three new cases in tests/serialize.test.ts: with type, without type, and on a nested resource. Full suite passes (64), plus typecheck, lint, and build.

Release

Version bumped to 2.2.0 with a CHANGELOG.md entry. 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 when type is absent. Left out of this PR, as the author suggested tracking it separately.

🤖 Generated with Claude Code

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>
@caiotarifa
caiotarifa merged commit bd946fe into main Aug 4, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot clear a to-one relationship (no way to send data: null)

1 participant