Skip to content

Commit

Permalink
Merge pull request #406 from tom-lord/deep_merge_additional_params
Browse files Browse the repository at this point in the history
Deep-merge nested additional_params (i.e. chained use of `with_params` with nested arguments)
  • Loading branch information
gaorlov authored Nov 22, 2023
2 parents 462e59e + 5251838 commit 9f47275
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- [#406](https://github.com/JsonApiClient/json_api_client/pull/406) - Deep-merge nested `additional_params`

## 1.21.1
- [#404](https://github.com/JsonApiClient/json_api_client/pull/404) - Expose NotFound json errors
- [#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
Expand Down
2 changes: 1 addition & 1 deletion lib/json_api_client/query/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def _new_scope( opts = {} )
primary_key: opts.fetch( :primary_key, @primary_key ),
pagination_params: @pagination_params.merge( opts.fetch( :pagination_params, {} ) ),
path_params: @path_params.merge( opts.fetch( :path_params, {} ) ),
additional_params: @additional_params.merge( opts.fetch( :additional_params, {} ) ),
additional_params: @additional_params.deep_merge( opts.fetch( :additional_params, {} ) ),
filters: @filters.merge( opts.fetch( :filters, {} ) ),
includes: @includes + opts.fetch( :includes, [] ),
orders: @orders + opts.fetch( :orders, [] ),
Expand Down
10 changes: 10 additions & 0 deletions test/unit/query_builder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,16 @@ def test_can_specify_additional_params
Article.with_params(sort: "foo").to_a
end

def test_can_merge_nested_additional_params
stub_request(:get, "http://example.com/articles")
.with(query: {top: {foos: 1, bars: 2}})
.to_return(headers: {content_type: "application/vnd.api+json"}, body: {
data: []
}.to_json)

Article.with_params(top: {foos: 1}).with_params(top: {bars: 2}).to_a
end

def test_can_select_fields
stub_request(:get, "http://example.com/articles")
.with(query: {fields: {articles: 'title,body'}})
Expand Down

0 comments on commit 9f47275

Please sign in to comment.