Fix: Add relationship linkage for cross-schema has_one/has_many #1478
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.
Summary
Fixes a critical bug where cross-schema relationships were not setting linkage data in JSON:API responses.
Problem
When using
has_oneorhas_manywith theschema:option, related resources appeared in theincludedsection butrelationships.<name>.datawasnull, preventing JSON:API clients from properly linking resources.Before:
{ "data": { "relationships": { "author": { "data": null } } }, "included": [ { "type": "users", "id": "123" } ] }After:
{ "data": { "relationships": { "author": { "data": { "type": "users", "id": "123" } } } }, "included": [ { "type": "users", "id": "123" } ] }Changes
Core Fix
Modified `lib/jsonapi/active_relation_resource_patch.rb`:
Tests
Added 12 comprehensive unit tests in `test/unit/resource/cross_schema_linkage_test.rb`:
has_one cross-schema (8 tests):
has_many cross-schema (4 tests):
Documentation
Test Plan
Impact
See `docs/CROSS_SCHEMA_FIX.md` for complete details.