|
| 1 | +# Cross-Schema Relationship Linkage Fix |
| 2 | + |
| 3 | +## Problem |
| 4 | + |
| 5 | +When using `has_one` or `has_many` relationships with the `schema:` option for cross-schema relationships, the relationship linkage data was not being set correctly in the JSON:API response. |
| 6 | + |
| 7 | +**Symptom:** |
| 8 | +```json |
| 9 | +{ |
| 10 | + "data": { |
| 11 | + "relationships": { |
| 12 | + "author": { |
| 13 | + "data": null // ❌ Should be { "type": "users", "id": "123" } |
| 14 | + } |
| 15 | + } |
| 16 | + }, |
| 17 | + "included": [ |
| 18 | + { |
| 19 | + "type": "users", // ✅ Related resource is included |
| 20 | + "id": "123", |
| 21 | + "attributes": { ... } |
| 22 | + } |
| 23 | + ] |
| 24 | +} |
| 25 | +``` |
| 26 | + |
| 27 | +The related resource appears in the `included` section, but `relationships.author.data` is `null`, preventing clients from properly linking the resources. |
| 28 | + |
| 29 | +## Root Cause |
| 30 | + |
| 31 | +The `handle_cross_schema_to_one` and `handle_cross_schema_to_many` methods in `active_relation_resource_patch.rb` were: |
| 32 | + |
| 33 | +1. ✅ Correctly loading related resources from cross-schema tables |
| 34 | +2. ✅ Correctly adding them to `included` section |
| 35 | +3. ❌ **NOT** setting the relationship linkage data in the source resource |
| 36 | + |
| 37 | +This prevented JSON:API clients from establishing the relationship between the primary resource and the included resource. |
| 38 | + |
| 39 | +## Solution |
| 40 | + |
| 41 | +Modified `lib/jsonapi/active_relation_resource_patch.rb` to: |
| 42 | + |
| 43 | +### 1. Handle Array sources (not just Hash) |
| 44 | + |
| 45 | +The `handle_cross_schema_included` method now converts Array sources to a Hash of fragments: |
| 46 | + |
| 47 | +```ruby |
| 48 | +source_fragments_hash = {} |
| 49 | +source_ids = if source.is_a?(Hash) |
| 50 | + source_fragments_hash = source |
| 51 | + source.keys.map(&:id) |
| 52 | +elsif source.is_a?(Array) |
| 53 | + source.each do |item| |
| 54 | + if item.respond_to?(:identity) |
| 55 | + source_fragments_hash[item.identity] = item |
| 56 | + elsif item.is_a?(JSONAPI::ResourceIdentity) |
| 57 | + source_fragments_hash[item] = JSONAPI::ResourceFragment.new(item) |
| 58 | + end |
| 59 | + end |
| 60 | + # ... |
| 61 | +end |
| 62 | +``` |
| 63 | + |
| 64 | +### 2. Add linkage to source fragments |
| 65 | + |
| 66 | +In both `handle_cross_schema_to_one` and `handle_cross_schema_to_many`, after creating the related resource fragment, we now add the linkage to the source fragment: |
| 67 | + |
| 68 | +```ruby |
| 69 | +# Create fragment for related resource |
| 70 | +fragments[rid] = JSONAPI::ResourceFragment.new(rid, resource: resource) |
| 71 | + |
| 72 | +# Add linkage to source fragment |
| 73 | +source_rid = JSONAPI::ResourceIdentity.new(self, source_resource.id) |
| 74 | +if options[:source_fragments] && options[:source_fragments][source_rid] |
| 75 | + options[:source_fragments][source_rid].add_related_identity(relationship.name, rid) |
| 76 | +end |
| 77 | +``` |
| 78 | + |
| 79 | +This ensures that the `relationships.<name>.data` field is populated correctly in the serialized output. |
| 80 | + |
| 81 | +## Testing |
| 82 | + |
| 83 | +### Unit Tests |
| 84 | + |
| 85 | +Created comprehensive unit tests in `test/unit/resource/cross_schema_linkage_test.rb`: |
| 86 | + |
| 87 | +- `test_has_one_cross_schema_creates_linkage_data` - Verifies linkage data is set for has_one |
| 88 | +- `test_has_one_cross_schema_with_null_foreign_key` - Handles null foreign keys gracefully |
| 89 | +- `test_cross_schema_relationship_with_array_source` - Tests Array source handling |
| 90 | +- `test_cross_schema_relationship_with_hash_source` - Tests Hash source handling |
| 91 | +- `test_multiple_candidates_with_same_recruiter` - Tests deduplication |
| 92 | +- `test_cross_schema_included_in_full_serialization` - End-to-end serialization test |
| 93 | +- `test_cross_schema_relationships_hash_registration` - Verifies configuration |
| 94 | +- `test_non_cross_schema_relationships_still_work` - Regression test |
| 95 | + |
| 96 | +### Test Fixtures |
| 97 | + |
| 98 | +Created test fixtures: |
| 99 | +- `test_employees.yml` - User data from "another schema" |
| 100 | +- `test_candidates.yml` - Primary resources with foreign keys |
| 101 | +- `test_locations.yml` - Normal same-schema relationships |
| 102 | +- `test_departments.yml` - For has_many testing |
| 103 | + |
| 104 | +## Usage |
| 105 | + |
| 106 | +Define cross-schema relationships using the `schema:` option: |
| 107 | + |
| 108 | +### has_one example: |
| 109 | + |
| 110 | +```ruby |
| 111 | +class CandidateResource < JSONAPI::ActiveRelationResource |
| 112 | + attributes :full_name, :email |
| 113 | + |
| 114 | + has_one :author, |
| 115 | + class_name: 'User', |
| 116 | + schema: 'auth_schema', |
| 117 | + exclude_links: :default, |
| 118 | + always_include_linkage_data: true |
| 119 | +end |
| 120 | +``` |
| 121 | + |
| 122 | +### has_many example: |
| 123 | + |
| 124 | +```ruby |
| 125 | +class DepartmentResource < JSONAPI::ActiveRelationResource |
| 126 | + attributes :name |
| 127 | + |
| 128 | + has_many :members, |
| 129 | + class_name: 'User', |
| 130 | + schema: 'auth_schema', |
| 131 | + exclude_links: :default |
| 132 | +end |
| 133 | +``` |
| 134 | + |
| 135 | +## Files Modified |
| 136 | + |
| 137 | +- `lib/jsonapi/active_relation_resource_patch.rb` - Core fix for linkage |
| 138 | +- `test/unit/resource/cross_schema_linkage_test.rb` - Comprehensive unit tests |
| 139 | +- `test/unit/resource/cross_schema_test.rb` - Original cross-schema tests |
| 140 | +- `test/fixtures/active_record.rb` - Added test tables |
| 141 | +- `test/fixtures/test_*.yml` - Test data fixtures |
| 142 | + |
| 143 | +## Running Tests |
| 144 | + |
| 145 | +```bash |
| 146 | +cd jsonapi-resources |
| 147 | +bundle install |
| 148 | +bundle exec rake test TEST=test/unit/resource/cross_schema_linkage_test.rb |
| 149 | +``` |
| 150 | + |
| 151 | +## Migration Notes |
| 152 | + |
| 153 | +Existing applications using cross-schema relationships will automatically benefit from this fix. No changes to application code are required - the linkage data will now be correctly populated in responses. |
0 commit comments