Skip to content

fix: return 400 for invalid relationship resource linkage - #474

Merged
mattpolzin merged 1 commit into
beam-community:mainfrom
agustin2606:fix/scalar-relationship-linkage
Oct 22, 2026
Merged

mattpolzin merged 1 commit into
beam-community:mainfrom
agustin2606:fix/scalar-relationship-linkage

Conversation

@agustin2606

Copy link
Copy Markdown

Closes #473

FormatRequired accepted any non-object relationship data, so a scalar linkage ("oops", 123, true, "") passed validation and JSONAPI.Deserializer then raised a FunctionClauseError in DataToParams.transform_relationship/2. To-many arrays had the same gap: ["oops"], [%{"type" => "x"}] or [%{"id" => "1"}] crashed in the same place.

This PR:

  • Validated relationship linkage per the spec: null, a resource identifier object, or an array of them (possibly empty). Anything else now halts with a 400 instead of crashing.
  • Extracted the resource identifier checks into a private helper, so to-one linkage and every member of a to-many array go through the same id/type validation.
  • Added ErrorView.invalid_relationship_linkage_error_attrs/2 and an optional index argument to the existing id/type error helpers, so the source pointer names the offending member (/data/relationships/reviews/data/1/type). Existing callers are unaffected.
  • Added tests for the four scalar cases on POST and PATCH, and for malformed and well-formed to-many arrays.

Note: this fixes the documented EnsureSpec → Deserializer pipeline. Deserializer used on its own still assumes well-formed linkage; making DataToParams defensive too could be a follow-up if that's wanted.

FormatRequired let any non-object relationship `data` through, so scalar
linkage ("oops", 123, true, "") and to-many arrays with malformed members
reached DataToParams and raised a FunctionClauseError.

Validate linkage per the spec: null, a resource identifier object, or an
array of them. Anything else halts with a 400 whose source pointer names
the offending member, including its index for to-many relationships.

Closes beam-community#473
@agustin2606
agustin2606 requested a review from a team as a code owner October 21, 2026 12:42

@mattpolzin mattpolzin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks!

@mattpolzin
mattpolzin merged commit e81767a into beam-community:main Oct 22, 2026
10 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.

(FunctionClauseError) in DataToParams.transform_relationship/2` on scalar relationship linkage

2 participants