Skip to content

Commit f7a4cef

Browse files
authored
Merge branch 'master' into sj/fix-multi-word-custom-endpoint
2 parents 9cf3d0f + afeb8f8 commit f7a4cef

36 files changed

+127
-40
lines changed

.github/workflows/ruby.yml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ permissions:
1818

1919
jobs:
2020
test:
21-
runs-on: ubuntu-latest
21+
runs-on: ubuntu-22.04
2222
strategy:
2323
fail-fast: false
2424
matrix:
@@ -44,12 +44,9 @@ jobs:
4444
- ruby: "2.7"
4545
rails: "4.2"
4646
steps:
47-
- uses: actions/checkout@v3
47+
- uses: actions/checkout@v4
4848
- name: Set up Ruby
49-
# To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
50-
# change this to (see https://github.com/ruby/setup-ruby#versioning):
51-
# uses: ruby/setup-ruby@v1
52-
uses: ruby/setup-ruby@55283cc23133118229fd3f97f9336ee23a179fcf # v1.146.0
49+
uses: ruby/setup-ruby@v1
5350
with:
5451
ruby-version: ${{ matrix.ruby }}
5552
bundler-cache: true # runs 'bundle install' and caches installed gems automatically

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
## Unreleased
44

5-
## 1.22.0
5+
66
- [#400](https://github.com/JsonApiClient/json_api_client/pull/400) - Fix for multi-word custom endpoint and route format
77

8+
## 1.22.0
9+
- [#403](https://github.com/JsonApiClient/json_api_client/pull/403) - Feature: Use the association options to lookup relationship class
10+
- [#406](https://github.com/JsonApiClient/json_api_client/pull/406) - Deep-merge nested `additional_params`
11+
812
## 1.21.1
913
- [#404](https://github.com/JsonApiClient/json_api_client/pull/404) - Expose NotFound json errors
1014
- [#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, [] ),

lib/json_api_client/utils.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ def self.compute_type(klass, type_name)
77
# the type_name is an absolute reference.
88
return type_name.constantize if type_name.match(/^::/)
99

10+
# Check the klass association definitions
11+
association_klass_match = klass.associations.find { |a| a.attr_name.to_s.singularize == type_name.underscore }
12+
association_klass = association_klass_match.options[:class] if association_klass_match
13+
return association_klass if association_klass
14+
1015
# Build a list of candidates to search for
1116
candidates = []
1217
klass.name.scan(/::|$/) { candidates.unshift "#{$`}::#{type_name}" }

lib/json_api_client/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module JsonApiClient
2-
VERSION = "1.21.1"
2+
VERSION = "1.22.0"
33
end

test/unit/association_test.rb

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,21 @@ class Employee < TestResource
8989
has_one :chief, klass: 'Employee'
9090
end
9191

92-
class AssociationTest < MiniTest::Test
92+
module CrossNamespaceTwo
93+
class Nail < TestResource
94+
property :size
95+
end
96+
end
97+
98+
module CrossNamespaceOne
99+
class Hammer < TestResource
100+
property :brand
101+
102+
has_many :nails, class: CrossNamespaceTwo::Nail
103+
end
104+
end
93105

106+
class AssociationTest < Minitest::Test
94107
def test_default_properties_no_changes
95108
stub_request(:post, 'http://example.com/accounts').
96109
with(headers: { content_type: 'application/vnd.api+json', accept: 'application/vnd.api+json' }, body: {
@@ -1070,4 +1083,62 @@ def test_does_not_load_include_from_dataset
10701083
assert_nil(records.first.chief)
10711084
end
10721085

1086+
def test_cross_namespace_resource_references
1087+
stub_request(:get, 'http://example.com/hammers?include=nails')
1088+
.to_return(
1089+
headers: {
1090+
content_type: 'application/vnd.api+json'
1091+
}, body: {
1092+
data: [
1093+
{
1094+
id: '1',
1095+
type: 'hammers',
1096+
attributes: {
1097+
brand: 'Hardware Store'
1098+
},
1099+
relationships: {
1100+
nails: { data: [{id: '2', type: 'nails'}] }
1101+
}
1102+
},
1103+
{
1104+
id: '2',
1105+
type: 'hammers',
1106+
attributes: {
1107+
brand: 'Hardware Store'
1108+
},
1109+
relationships: {
1110+
nails: { data: [{id: '3', type: 'nails'}] }
1111+
}
1112+
}
1113+
],
1114+
included: [
1115+
{
1116+
id: '2',
1117+
type: 'nails',
1118+
attributes: {
1119+
size: 10
1120+
}
1121+
},
1122+
{
1123+
id: '3',
1124+
type: 'nails',
1125+
attributes: {
1126+
size: 8
1127+
}
1128+
}
1129+
]
1130+
}.to_json)
1131+
1132+
records = CrossNamespaceOne::Hammer.includes(:nails).to_a
1133+
1134+
assert_equal(2, records.size)
1135+
assert_equal('1', records.first.id)
1136+
assert_equal('2', records.second.id)
1137+
assert_equal('2', records.first.nails.first.id)
1138+
assert_equal('3', records.second.nails.first.id)
1139+
1140+
assert_equal(1, records.first.nails.size)
1141+
assert_equal(1, records.second.nails.size)
1142+
end
1143+
10731144
end

test/unit/benchmark_dynamic_attributes_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'test_helper'
22
require 'benchmark'
33

4-
class BenchmarkDynamicAttributesTest < MiniTest::Test
4+
class BenchmarkDynamicAttributesTest < Minitest::Test
55
def test_can_parse_global_meta_data
66
stub_request(:get, "http://example.com/articles/1")
77
.to_return(headers: {content_type: "application/vnd.api+json"}, body: {

test/unit/coercion_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'test_helper'
22

3-
class CoercionTest < MiniTest::Test
3+
class CoercionTest < Minitest::Test
44
TIME_STRING = '2015-04-28 10:45:35 -0700'
55

66
class CoercionTypes < TestResource

test/unit/compound_document_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'test_helper'
22

3-
class CompoundDocumentTest < MiniTest::Test
3+
class CompoundDocumentTest < Minitest::Test
44

55
TEST_DATA = %{
66
{

test/unit/compound_non_included_document_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'test_helper'
22

3-
class CompoundNonIncludedDocumentTest < MiniTest::Test
3+
class CompoundNonIncludedDocumentTest < Minitest::Test
44

55
TEST_DATA = %{
66
{

test/unit/connection_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class CustomAdapterResource < TestResource
3636
}
3737
end
3838

39-
class ConnectionTest < MiniTest::Test
39+
class ConnectionTest < Minitest::Test
4040

4141
def test_basic
4242
assert_equal(NullConnection, CustomConnectionResource.connection_class)

test/unit/creation_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'test_helper'
22

3-
class CreationTest < MiniTest::Test
3+
class CreationTest < Minitest::Test
44

55
class CallbackTest < TestResource
66
include JsonApiClient::Helpers::Callbacks

test/unit/custom_endpoint_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class MythicBeasts < TestResource
2525

2626
end
2727

28-
class CustomEndpointTest < MiniTest::Test
28+
class CustomEndpointTest < Minitest::Test
2929

3030
def test_collection_get
3131
stub_request(:get, "http://example.com/countries/autocomplete?starts_with=bel")

test/unit/custom_header_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'test_helper'
22

3-
class CustomHeaderTest < MiniTest::Test
3+
class CustomHeaderTest < Minitest::Test
44

55
class CustomHeaderResource < TestResource
66
end

test/unit/custom_paginator_params_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'test_helper'
22

3-
class CustomPaginatorTest < MiniTest::Test
3+
class CustomPaginatorTest < Minitest::Test
44

55
class CustomPaginator < JsonApiClient::Paginating::Paginator
66
self.page_param = 'pagina'

test/unit/custom_paginator_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'test_helper'
22

3-
class CustomPaginatorTest < MiniTest::Test
3+
class CustomPaginatorTest < Minitest::Test
44

55
class CustomPaginator < JsonApiClient::Paginating::Paginator
66
def total_entries

test/unit/destroying_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'test_helper'
22

3-
class DestroyingTest < MiniTest::Test
3+
class DestroyingTest < Minitest::Test
44

55
class CallbackTest < TestResource
66
include JsonApiClient::Helpers::Callbacks

test/unit/editing_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
class Author < TestResource
44
end
55

6-
class EditingTest < MiniTest::Test
6+
class EditingTest < Minitest::Test
77

88
def test_attribute_changed
99
stub_request(:get, "http://example.com/authors/1")

test/unit/error_collector_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'test_helper'
22

3-
class ErrorCollectorTest < MiniTest::Test
3+
class ErrorCollectorTest < Minitest::Test
44

55
def test_can_handle_no_errors
66
stub_request(:post, "http://example.com/articles")

test/unit/errors_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'test_helper'
22

3-
class ErrorsTest < MiniTest::Test
3+
class ErrorsTest < Minitest::Test
44

55
def test_connection_errors
66
stub_request(:get, "http://example.com/users")

test/unit/finding_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'test_helper'
22

33
# This tests this Resource.find method
4-
class FindingTest < MiniTest::Test
4+
class FindingTest < Minitest::Test
55

66
def test_find_by_id
77
stub_request(:get, "http://example.com/articles/1")

test/unit/implementation_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'test_helper'
22

3-
class ImplementationTest < MiniTest::Test
3+
class ImplementationTest < Minitest::Test
44

55
def test_defaults_on_missing_fields
66
stub_request(:get, "http://example.com/articles")

test/unit/integer_id_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Director < BaseResource
2424

2525
NUMERIC_ASSERTION = Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.4') ? Fixnum : Integer
2626

27-
class IntegerIdTestAssociationTest < MiniTest::Test
27+
class IntegerIdTestAssociationTest < Minitest::Test
2828

2929
def test_included_document_test_id_from_method_as_integer
3030
stub_request(:get, 'http://example.com/movies/1?include=actor')

test/unit/meta_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'test_helper'
22

3-
class MetaTest < MiniTest::Test
3+
class MetaTest < Minitest::Test
44

55
def test_can_parse_global_meta_data
66
stub_request(:get, "http://example.com/articles/1")

test/unit/nested_param_paginator_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'test_helper'
22

3-
class NestedParamPaginatorTest < MiniTest::Test
3+
class NestedParamPaginatorTest < Minitest::Test
44

55
class Book < JsonApiClient::Resource
66
self.site = "http://example.com/"

test/unit/nested_param_paginator_top_level_links_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# Copied from TopLevelLinksTest and modified to have consistent
44
# pagination params using the new NestedParamPaginator
5-
class NestedParamPaginatorTopLevelLinksTest < MiniTest::Test
5+
class NestedParamPaginatorTopLevelLinksTest < Minitest::Test
66

77
def setup
88
@nested_param_paginator = JsonApiClient::Paginating::NestedParamPaginator

test/unit/parser_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'test_helper'
22

3-
class ParserTest < MiniTest::Test
3+
class ParserTest < Minitest::Test
44

55
def test_can_parse_single_record
66
stub_request(:get, "http://example.com/articles/1")

test/unit/persistence_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'test_helper'
22

3-
class PersistenceTest < MiniTest::Test
3+
class PersistenceTest < Minitest::Test
44

55
def test_standard_primary_key
66
user = User.new

test/unit/query_builder_test.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'test_helper'
22

3-
class QueryBuilderTest < MiniTest::Test
3+
class QueryBuilderTest < Minitest::Test
44

55
def test_can_filter
66
stub_request(:get, "http://example.com/articles")
@@ -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'}})

test/unit/resource_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'test_helper'
22

3-
class ResourceTest < MiniTest::Test
3+
class ResourceTest < Minitest::Test
44

55
def test_basic
66
assert_equal :id, Article.primary_key

test/unit/schemable_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class MultipleSchema < TestResource
2727
properties :name, :short_name, :long_name, type: :string
2828
end
2929

30-
class SchemableTest < MiniTest::Test
30+
class SchemableTest < Minitest::Test
3131

3232
def test_default_attributes
3333
resource = SchemaResource.new

test/unit/serializing_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'test_helper'
22

3-
class SerializingTest < MiniTest::Test
3+
class SerializingTest < Minitest::Test
44

55
class LimitedField < TestResource
66
self.read_only_attributes += ['foo']

test/unit/server_side_error_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'test_helper'
22

3-
class ServerSideErrorTest < MiniTest::Test
3+
class ServerSideErrorTest < Minitest::Test
44

55
def test_can_handle_validations
66
stub_request(:post, "http://example.com/users")

test/unit/status_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'test_helper'
22

3-
class StatusTest < MiniTest::Test
3+
class StatusTest < Minitest::Test
44

55
def test_server_responding_with_status_meta
66
stub_request(:get, "http://example.com/users/1")

0 commit comments

Comments
 (0)