Skip to content

Commit 406a196

Browse files
takaokoujiclaude
andcommitted
Add backward compatibility for 0.9.x result.resource access
Add `resource` and `resources` methods to ResourceSetOperationResult and ResourcesSetOperationResult for backward compatibility with 0.9.x API. In 0.9.x, operation results had a `resource` accessor. In 0.10+, resources are accessed via `resource_set`. These new methods allow existing code that uses `result.resource` to continue working without modification. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent dd05bd9 commit 406a196

2 files changed

Lines changed: 136 additions & 0 deletions

File tree

lib/jsonapi/operation_result.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,32 @@ def to_hash(serializer)
5858
# :nocov:
5959
end
6060
end
61+
62+
# Returns the first resource from the resource_set for backward compatibility with 0.9.x
63+
# In 0.9.x, OperationResult had a `resource` accessor that returned the single resource.
64+
# This method provides the same interface for code that relied on `result.resource`.
65+
def resource
66+
return nil unless resource_set&.resource_klasses
67+
resource_set.resource_klasses.each_value do |identities|
68+
identities.each_value do |data|
69+
return data[:resource] if data[:resource]
70+
end
71+
end
72+
nil
73+
end
74+
75+
# Returns all resources from the resource_set for backward compatibility with 0.9.x
76+
# Useful when the result contains multiple resources.
77+
def resources
78+
return [] unless resource_set&.resource_klasses
79+
result = []
80+
resource_set.resource_klasses.each_value do |identities|
81+
identities.each_value do |data|
82+
result << data[:resource] if data[:resource]
83+
end
84+
end
85+
result
86+
end
6187
end
6288

6389
class ResourcesSetOperationResult < OperationResult
@@ -80,6 +106,29 @@ def to_hash(serializer)
80106
# :nocov:
81107
end
82108
end
109+
110+
# Returns the first resource from the resource_set for backward compatibility with 0.9.x
111+
def resource
112+
return nil unless resource_set&.resource_klasses
113+
resource_set.resource_klasses.each_value do |identities|
114+
identities.each_value do |data|
115+
return data[:resource] if data[:resource]
116+
end
117+
end
118+
nil
119+
end
120+
121+
# Returns all resources from the resource_set for backward compatibility with 0.9.x
122+
def resources
123+
return [] unless resource_set&.resource_klasses
124+
result = []
125+
resource_set.resource_klasses.each_value do |identities|
126+
identities.each_value do |data|
127+
result << data[:resource] if data[:resource]
128+
end
129+
end
130+
result
131+
end
83132
end
84133

85134
class RelatedResourcesSetOperationResult < ResourcesSetOperationResult
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
require File.expand_path('../../../test_helper', __FILE__)
2+
3+
# Tests for backward compatibility with 0.9.x API
4+
class BackwardCompatibilityTest < ActiveSupport::TestCase
5+
# Test find_by_key with old signature: find_by_key(id, context)
6+
# New signature: find_by_key(key, options = {})
7+
def test_find_by_key_with_old_signature
8+
# 0.9.x style: find_by_key(id, context)
9+
# In 0.9.x, context was passed directly as second argument
10+
# In 0.10+, context should be passed as options[:context]
11+
# For backward compatibility, if second arg is a Hash without :context key,
12+
# it should still work (context will be nil in resource)
13+
context = { current_user: 'test_user' }
14+
resource = PostResource.find_by_key(1, context)
15+
16+
assert_not_nil resource
17+
assert_equal 1, resource.id
18+
end
19+
20+
def test_find_by_key_with_new_signature
21+
# 0.10+ style: find_by_key(key, options = {})
22+
context = { current_user: 'test_user' }
23+
resource = PostResource.find_by_key(1, context: context)
24+
25+
assert_not_nil resource
26+
assert_equal 1, resource.id
27+
end
28+
29+
def test_find_by_key_without_context
30+
# find_by_key without any context
31+
resource = PostResource.find_by_key(1)
32+
33+
assert_not_nil resource
34+
assert_equal 1, resource.id
35+
end
36+
end
37+
38+
# Tests for ResourceSetOperationResult backward compatibility
39+
class ResourceSetOperationResultBackwardCompatibilityTest < ActiveSupport::TestCase
40+
def setup
41+
# Create a helper to build ResourceSet with resources
42+
end
43+
44+
def test_resource_method_returns_first_resource
45+
# Create a ResourceSet with a single resource using the proper API
46+
post = Post.find(1)
47+
resource = PostResource.new(post, nil)
48+
49+
# Use the ResourceSet with a resource (not nil)
50+
resource_set = JSONAPI::ResourceSet.new(resource)
51+
resource_set.mark_populated!
52+
53+
result = JSONAPI::ResourceSetOperationResult.new(:ok, resource_set)
54+
55+
# 0.9.x style: result.resource
56+
assert result.respond_to?(:resource), "ResourceSetOperationResult should respond to :resource for 0.9.x compatibility"
57+
assert_equal resource, result.resource
58+
end
59+
60+
def test_resource_method_returns_nil_for_empty_resource_set
61+
# Create an empty resource set by passing an empty array
62+
resource_set = JSONAPI::ResourceSet.new([])
63+
result = JSONAPI::ResourceSetOperationResult.new(:ok, resource_set)
64+
65+
assert_nil result.resource
66+
end
67+
68+
def test_resources_method_returns_all_resources
69+
# Create a ResourceSet with multiple resources
70+
post1 = Post.find(1)
71+
post2 = Post.find(2)
72+
resource1 = PostResource.new(post1, nil)
73+
resource2 = PostResource.new(post2, nil)
74+
75+
# Use the ResourceSet with an array of resources
76+
resource_set = JSONAPI::ResourceSet.new([resource1, resource2])
77+
resource_set.mark_populated!
78+
79+
result = JSONAPI::ResourceSetOperationResult.new(:ok, resource_set)
80+
81+
# 0.9.x style: result.resources (for collections)
82+
assert result.respond_to?(:resources), "ResourceSetOperationResult should respond to :resources for 0.9.x compatibility"
83+
assert_equal 2, result.resources.length
84+
assert_includes result.resources, resource1
85+
assert_includes result.resources, resource2
86+
end
87+
end

0 commit comments

Comments
 (0)