Skip to content

Commit 15c8a8a

Browse files
qelphyboxchingor13
authored andcommitted
Fixed nested resources keys escaping (#236)
* Added vagrantfile with provision * Fixed nested resource_id escape * Added test for resource escaping * Updated README.md * Fixed setting prefix path for belongs_to association * Revert "Updated README.md" This reverts commit 77958b8. * Revert "Added vagrantfile with provision" This reverts commit 4f9f029.
1 parent c0b0c4f commit 15c8a8a

File tree

7 files changed

+27
-9
lines changed

7 files changed

+27
-9
lines changed

lib/json_api_client/associations/base_association.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ def from_result_set(result_set)
2323
end
2424
end
2525
end
26-
end
26+
end

lib/json_api_client/associations/belongs_to.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,20 @@ def belongs_to(attr_name, options = {})
1111
end
1212

1313
class Association < BaseAssociation
14+
include Helpers::URI
1415
def param
1516
:"#{attr_name}_id"
1617
end
1718

1819
def to_prefix_path
1920
"#{attr_name.to_s.pluralize}/%{#{param}}"
2021
end
22+
23+
def set_prefix_path(attrs)
24+
attrs[param] = encode_part(attrs[param]) if attrs.key?(param)
25+
to_prefix_path % attrs
26+
end
2127
end
2228
end
2329
end
24-
end
30+
end

lib/json_api_client/helpers.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ module Helpers
33
autoload :Callbacks, 'json_api_client/helpers/callbacks'
44
autoload :Dirty, 'json_api_client/helpers/dirty'
55
autoload :DynamicAttributes, 'json_api_client/helpers/dynamic_attributes'
6+
autoload :URI, 'json_api_client/helpers/uri'
67
end
7-
end
8+
end

lib/json_api_client/helpers/uri.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module JsonApiClient
2+
module Helpers
3+
module URI
4+
def encode_part(part)
5+
Addressable::URI.encode_component(part, Addressable::URI::CharacterClasses::UNRESERVED)
6+
end
7+
end
8+
end
9+
end

lib/json_api_client/query/requestor.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module JsonApiClient
22
module Query
33
class Requestor
44
extend Forwardable
5+
include Helpers::URI
56

67
def initialize(klass)
78
@klass = klass
@@ -49,16 +50,12 @@ def custom(method_name, options, params)
4950

5051
def resource_path(parameters)
5152
if resource_id = parameters[klass.primary_key]
52-
File.join(klass.path(parameters), encoded(resource_id))
53+
File.join(klass.path(parameters), encode_part(resource_id))
5354
else
5455
klass.path(parameters)
5556
end
5657
end
5758

58-
def encoded(part)
59-
Addressable::URI.encode_component(part, Addressable::URI::CharacterClasses::UNRESERVED)
60-
end
61-
6259
def request(type, path, params)
6360
klass.parser.parse(klass, connection.run(type, path, params, klass.custom_headers))
6461
end

lib/json_api_client/resource.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def path(params = nil)
119119
parts = [resource_path]
120120
if params && _prefix_path.present?
121121
path_params = params.delete(:path) || params
122-
parts.unshift(_prefix_path % path_params.symbolize_keys)
122+
parts.unshift(_set_prefix_path(path_params.symbolize_keys))
123123
else
124124
parts.unshift(_prefix_path)
125125
end
@@ -266,6 +266,10 @@ def _prefix_path
266266
_belongs_to_associations.map(&:to_prefix_path).join("/")
267267
end
268268

269+
def _set_prefix_path(attrs)
270+
_belongs_to_associations.map { |a| a.set_prefix_path(attrs) }.join("/")
271+
end
272+
269273
def _new_scope
270274
query_builder.new(self)
271275
end

test/unit/association_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,7 @@ def test_belongs_to_path
496496
end
497497
assert_equal("foos/%{foo_id}/specifieds", Specified.path)
498498
assert_equal("foos/1/specifieds", Specified.path({foo_id: 1}))
499+
assert_equal("foos/%D0%99%D0%A6%D0%A3%D0%9A%D0%95%D0%9D/specifieds", Specified.path({foo_id: 'ЙЦУКЕН'}))
499500
end
500501

501502
def test_find_belongs_to

0 commit comments

Comments
 (0)