Skip to content

Commit 9f47275

Browse files
authored
Merge pull request JsonApiClient#406 from tom-lord/deep_merge_additional_params
Deep-merge nested additional_params (i.e. chained use of `with_params` with nested arguments)
2 parents 462e59e + 5251838 commit 9f47275

3 files changed

Lines changed: 13 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- [#406](https://github.com/JsonApiClient/json_api_client/pull/406) - Deep-merge nested `additional_params`
6+
57
## 1.21.1
68
- [#404](https://github.com/JsonApiClient/json_api_client/pull/404) - Expose NotFound json errors
79
- [#378](https://github.com/JsonApiClient/json_api_client/pull/378) - Add the ability to create a subclass of JsonApiClient::Resource to have a modified id method

lib/json_api_client/query/builder.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def _new_scope( opts = {} )
136136
primary_key: opts.fetch( :primary_key, @primary_key ),
137137
pagination_params: @pagination_params.merge( opts.fetch( :pagination_params, {} ) ),
138138
path_params: @path_params.merge( opts.fetch( :path_params, {} ) ),
139-
additional_params: @additional_params.merge( opts.fetch( :additional_params, {} ) ),
139+
additional_params: @additional_params.deep_merge( opts.fetch( :additional_params, {} ) ),
140140
filters: @filters.merge( opts.fetch( :filters, {} ) ),
141141
includes: @includes + opts.fetch( :includes, [] ),
142142
orders: @orders + opts.fetch( :orders, [] ),

test/unit/query_builder_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,16 @@ def test_can_specify_additional_params
122122
Article.with_params(sort: "foo").to_a
123123
end
124124

125+
def test_can_merge_nested_additional_params
126+
stub_request(:get, "http://example.com/articles")
127+
.with(query: {top: {foos: 1, bars: 2}})
128+
.to_return(headers: {content_type: "application/vnd.api+json"}, body: {
129+
data: []
130+
}.to_json)
131+
132+
Article.with_params(top: {foos: 1}).with_params(top: {bars: 2}).to_a
133+
end
134+
125135
def test_can_select_fields
126136
stub_request(:get, "http://example.com/articles")
127137
.with(query: {fields: {articles: 'title,body'}})

0 commit comments

Comments
 (0)