Skip to content

Commit 8e20b5a

Browse files
committed
Merge branch 'master' into rails5
2 parents 6ebe984 + 3ffbed8 commit 8e20b5a

6 files changed

Lines changed: 51 additions & 4 deletions

File tree

lib/jsonapi/acts_as_resource_controller.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,18 @@ def render_errors(errors)
148148

149149
def render_results(operation_results)
150150
response_doc = create_response_document(operation_results)
151-
render status: response_doc.status, json: response_doc.contents, content_type: JSONAPI::MEDIA_TYPE
151+
152+
render_options = {
153+
status: response_doc.status,
154+
json: response_doc.contents,
155+
content_type: JSONAPI::MEDIA_TYPE
156+
}
157+
158+
render_options[:location] = response_doc.contents[:data]["links"][:self] if (
159+
response_doc.status == :created && response_doc.contents[:data].class != Array
160+
)
161+
162+
render(render_options)
152163
end
153164

154165
def create_response_document(operation_results)
@@ -175,7 +186,7 @@ def handle_exceptions(e)
175186
when JSONAPI::Exceptions::Error
176187
render_errors(e.errors)
177188
else
178-
if JSONAPI.configuration.exception_class_whitelist.any? { |k| e.class.ancestors.include?(k) }
189+
if JSONAPI.configuration.exception_class_whitelisted?(e)
179190
fail e
180191
else
181192
internal_server_error = JSONAPI::Exceptions::InternalServerError.new(e)

lib/jsonapi/configuration.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ def operations_processor=(operations_processor)
9595
@operations_processor = JSONAPI::OperationsProcessor.operations_processor_for(@operations_processor_name)
9696
end
9797

98+
def exception_class_whitelisted?(e)
99+
@exception_class_whitelist.flatten.any? { |k| e.class.ancestors.include?(k) }
100+
end
101+
98102
attr_writer :allow_include, :allow_sort, :allow_filter
99103

100104
attr_writer :default_paginator

lib/jsonapi/operations_processor.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def with_default_handling(&block)
9292
raise e
9393

9494
rescue => e
95-
if JSONAPI.configuration.exception_class_whitelist.any? { |k| e.class.ancestors.include?(k) }
95+
if JSONAPI.configuration.exception_class_whitelisted?(e)
9696
raise e
9797
else
9898
@request.server_error_callbacks.each { |callback| safe_run_callback(callback, e) }

lib/jsonapi/resource.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,9 @@ def is_filter_relationship?(filter)
638638

639639
def verify_filter(filter, raw, context = nil)
640640
filter_values = []
641-
filter_values += CSV.parse_line(raw) unless raw.nil? || raw.empty?
641+
if raw.present?
642+
filter_values += raw.is_a?(String) ? CSV.parse_line(raw) : [raw]
643+
end
642644

643645
strategy = _allowed_filters.fetch(filter, Hash.new)[:verify]
644646

test/controllers/controller_test.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,13 @@ def test_index_filter_by_title
103103
assert_equal 1, json_response['data'].size
104104
end
105105

106+
def test_index_filter_with_hash_values
107+
get :index, {filter: {search: {title: 'New post'}}}
108+
assert_response :success
109+
assert json_response['data'].is_a?(Array)
110+
assert_equal 1, json_response['data'].size
111+
end
112+
106113
def test_index_filter_by_ids
107114
get :index, params: {filter: {ids: '1,2'}}
108115
assert_response :success
@@ -412,6 +419,7 @@ def test_create_simple
412419
assert json_response['data'].is_a?(Hash)
413420
assert_equal 'JR is Great', json_response['data']['attributes']['title']
414421
assert_equal 'JSONAPIResources is the greatest thing since unsliced bread.', json_response['data']['attributes']['body']
422+
assert_equal json_response['data']['links']['self'], response.location
415423
end
416424

417425
def test_create_link_to_missing_object
@@ -433,6 +441,7 @@ def test_create_link_to_missing_object
433441
assert_response :unprocessable_entity
434442
# TODO: check if this validation is working
435443
assert_match /author - can't be blank/, response.body
444+
assert_equal nil, response.location
436445
end
437446

438447
def test_create_extra_param
@@ -454,6 +463,7 @@ def test_create_extra_param
454463

455464
assert_response :bad_request
456465
assert_match /asdfg is not allowed/, response.body
466+
assert_equal nil,response.location
457467
end
458468

459469
def test_create_extra_param_allow_extra_params
@@ -486,6 +496,7 @@ def test_create_extra_param_allow_extra_params
486496
assert_equal "Param not allowed", json_response['meta']["warnings"][0]["title"]
487497
assert_equal "asdfg is not allowed.", json_response['meta']["warnings"][0]["detail"]
488498
assert_equal '105', json_response['meta']["warnings"][0]["code"]
499+
assert_equal json_response['data']['links']['self'], response.location
489500
ensure
490501
JSONAPI.configuration.raise_if_parameters_not_allowed = true
491502
end
@@ -515,6 +526,7 @@ def test_create_with_invalid_data
515526
assert_equal "/data/attributes/title", json_response['errors'][1]['source']['pointer']
516527
assert_equal "is too long (maximum is 35 characters)", json_response['errors'][1]['title']
517528
assert_equal "title - is too long (maximum is 35 characters)", json_response['errors'][1]['detail']
529+
assert_equal nil, response.location
518530
end
519531

520532
def test_create_multiple
@@ -551,6 +563,7 @@ def test_create_multiple
551563
assert_nil json_response['data'][0]['relationships']['author']['data']
552564
assert_match /JR is Great/, response.body
553565
assert_match /Ember is Great/, response.body
566+
assert_equal nil, response.location
554567
end
555568

556569
def test_create_multiple_wrong_case
@@ -583,6 +596,7 @@ def test_create_multiple_wrong_case
583596

584597
assert_response :bad_request
585598
assert_match /Title/, json_response['errors'][0]['detail']
599+
assert_equal nil, response.location
586600
end
587601

588602
def test_create_simple_missing_posts
@@ -603,6 +617,7 @@ def test_create_simple_missing_posts
603617

604618
assert_response :bad_request
605619
assert_match /The required parameter, data, is missing./, json_response['errors'][0]['detail']
620+
assert_equal nil, response.location
606621
end
607622

608623
def test_create_simple_wrong_type
@@ -623,6 +638,7 @@ def test_create_simple_wrong_type
623638

624639
assert_response :bad_request
625640
assert_match /posts_spelled_wrong is not a valid resource./, json_response['errors'][0]['detail']
641+
assert_equal nil, response.location
626642
end
627643

628644
def test_create_simple_missing_type
@@ -642,6 +658,7 @@ def test_create_simple_missing_type
642658

643659
assert_response :bad_request
644660
assert_match /The required parameter, type, is missing./, json_response['errors'][0]['detail']
661+
assert_equal nil, response.location
645662
end
646663

647664
def test_create_simple_unpermitted_attributes
@@ -662,6 +679,7 @@ def test_create_simple_unpermitted_attributes
662679

663680
assert_response :bad_request
664681
assert_match /subject/, json_response['errors'][0]['detail']
682+
assert_equal nil, response.location
665683
end
666684

667685
def test_create_simple_unpermitted_attributes_allow_extra_params
@@ -696,6 +714,7 @@ def test_create_simple_unpermitted_attributes_allow_extra_params
696714
assert_equal "Param not allowed", json_response['meta']["warnings"][0]["title"]
697715
assert_equal "subject is not allowed.", json_response['meta']["warnings"][0]["detail"]
698716
assert_equal '105', json_response['meta']["warnings"][0]["code"]
717+
assert_equal json_response['data']['links']['self'], response.location
699718
ensure
700719
JSONAPI.configuration.raise_if_parameters_not_allowed = true
701720
end
@@ -723,6 +742,7 @@ def test_create_with_links_to_many_type_ids
723742
assert_equal '3', json_response['data']['relationships']['author']['data']['id']
724743
assert_equal 'JR is Great', json_response['data']['attributes']['title']
725744
assert_equal 'JSONAPIResources is the greatest thing since unsliced bread.', json_response['data']['attributes']['body']
745+
assert_equal json_response['data']['links']['self'], response.location
726746
end
727747

728748
def test_create_with_links_to_many_array
@@ -748,6 +768,7 @@ def test_create_with_links_to_many_array
748768
assert_equal '3', json_response['data']['relationships']['author']['data']['id']
749769
assert_equal 'JR is Great', json_response['data']['attributes']['title']
750770
assert_equal 'JSONAPIResources is the greatest thing since unsliced bread.', json_response['data']['attributes']['body']
771+
assert_equal json_response['data']['links']['self'], response.location
751772
end
752773

753774
def test_create_with_links_include_and_fields
@@ -774,6 +795,7 @@ def test_create_with_links_include_and_fields
774795
assert_equal '3', json_response['data']['relationships']['author']['data']['id']
775796
assert_equal 'JR is Great!', json_response['data']['attributes']['title']
776797
assert_not_nil json_response['included'].size
798+
assert_equal json_response['data']['links']['self'], response.location
777799
end
778800

779801
def test_update_with_links

test/fixtures/active_record.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,14 @@ def title=(title)
956956
records.where('id IN (?)', value)
957957
}
958958

959+
filter :search,
960+
verify: ->(values, context) {
961+
values.all?{|v| v.is_a?(Hash) } && values
962+
},
963+
apply: -> (records, values, _options) {
964+
records.where(title: values.first['title'])
965+
}
966+
959967
def self.updatable_fields(context)
960968
super(context) - [:author, :subject]
961969
end

0 commit comments

Comments
 (0)