Fix: Add relationship linkage for cross-schema has_one/has_many#1478
Closed
robert-hromej wants to merge 5 commits intocerebris:masterfrom
Closed
Fix: Add relationship linkage for cross-schema has_one/has_many#1478robert-hromej wants to merge 5 commits intocerebris:masterfrom
robert-hromej wants to merge 5 commits intocerebris:masterfrom
Conversation
Fixes a bug where cross-schema relationships were not setting linkage data in JSON:API responses. Related resources appeared in 'included' section but relationships.<name>.data was null, preventing clients from properly linking resources. Changes: - Modified handle_cross_schema_included to convert Array sources to Hash - Added linkage setting in handle_cross_schema_to_one via add_related_identity - Fixed handle_cross_schema_to_many SQL query and linkage - Added source_fragments passing through enhanced_options Tests: - Added 12 comprehensive unit tests covering has_one and has_many - Tests cover Array/Hash sources, null handling, deduplication - Created test fixtures and models for cross-schema scenarios Documentation: - docs/CROSS_SCHEMA_FIX.md - complete problem analysis and solution - CHANGELOG_CROSS_SCHEMA.md - detailed changelog This fix is fully backward compatible.
76ddeaf to
d4b5c96
Compare
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.
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.