Skip to content

Commit 3ffbed8

Browse files
committed
Merge pull request #640 from vrybas/add_location_header_if_created_single_resource
[Fixes #289] Add 'Location' header if single resource created
2 parents 4356b85 + 933750c commit 3ffbed8

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

lib/jsonapi/acts_as_resource_controller.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,17 @@ def render_errors(errors)
152152

153153
def render_results(operation_results)
154154
response_doc = create_response_document(operation_results)
155-
render status: response_doc.status, json: response_doc.contents
155+
156+
render_options = {
157+
status: response_doc.status,
158+
json: response_doc.contents
159+
}
160+
161+
render_options[:location] = response_doc.contents[:data]["links"][:self] if (
162+
response_doc.status == :created && response_doc.contents[:data].class != Array
163+
)
164+
165+
render(render_options)
156166
end
157167

158168
def create_response_document(operation_results)

test/controllers/controller_test.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,7 @@ def test_create_simple
419419
assert json_response['data'].is_a?(Hash)
420420
assert_equal 'JR is Great', json_response['data']['attributes']['title']
421421
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
422423
end
423424

424425
def test_create_link_to_missing_object
@@ -440,6 +441,7 @@ def test_create_link_to_missing_object
440441
assert_response :unprocessable_entity
441442
# TODO: check if this validation is working
442443
assert_match /author - can't be blank/, response.body
444+
assert_equal nil, response.location
443445
end
444446

445447
def test_create_extra_param
@@ -461,6 +463,7 @@ def test_create_extra_param
461463

462464
assert_response :bad_request
463465
assert_match /asdfg is not allowed/, response.body
466+
assert_equal nil,response.location
464467
end
465468

466469
def test_create_extra_param_allow_extra_params
@@ -493,6 +496,7 @@ def test_create_extra_param_allow_extra_params
493496
assert_equal "Param not allowed", json_response['meta']["warnings"][0]["title"]
494497
assert_equal "asdfg is not allowed.", json_response['meta']["warnings"][0]["detail"]
495498
assert_equal '105', json_response['meta']["warnings"][0]["code"]
499+
assert_equal json_response['data']['links']['self'], response.location
496500
ensure
497501
JSONAPI.configuration.raise_if_parameters_not_allowed = true
498502
end
@@ -522,6 +526,7 @@ def test_create_with_invalid_data
522526
assert_equal "/data/attributes/title", json_response['errors'][1]['source']['pointer']
523527
assert_equal "is too long (maximum is 35 characters)", json_response['errors'][1]['title']
524528
assert_equal "title - is too long (maximum is 35 characters)", json_response['errors'][1]['detail']
529+
assert_equal nil, response.location
525530
end
526531

527532
def test_create_multiple
@@ -558,6 +563,7 @@ def test_create_multiple
558563
assert_nil json_response['data'][0]['relationships']['author']['data']
559564
assert_match /JR is Great/, response.body
560565
assert_match /Ember is Great/, response.body
566+
assert_equal nil, response.location
561567
end
562568

563569
def test_create_multiple_wrong_case
@@ -590,6 +596,7 @@ def test_create_multiple_wrong_case
590596

591597
assert_response :bad_request
592598
assert_match /Title/, json_response['errors'][0]['detail']
599+
assert_equal nil, response.location
593600
end
594601

595602
def test_create_simple_missing_posts
@@ -610,6 +617,7 @@ def test_create_simple_missing_posts
610617

611618
assert_response :bad_request
612619
assert_match /The required parameter, data, is missing./, json_response['errors'][0]['detail']
620+
assert_equal nil, response.location
613621
end
614622

615623
def test_create_simple_wrong_type
@@ -630,6 +638,7 @@ def test_create_simple_wrong_type
630638

631639
assert_response :bad_request
632640
assert_match /posts_spelled_wrong is not a valid resource./, json_response['errors'][0]['detail']
641+
assert_equal nil, response.location
633642
end
634643

635644
def test_create_simple_missing_type
@@ -649,6 +658,7 @@ def test_create_simple_missing_type
649658

650659
assert_response :bad_request
651660
assert_match /The required parameter, type, is missing./, json_response['errors'][0]['detail']
661+
assert_equal nil, response.location
652662
end
653663

654664
def test_create_simple_unpermitted_attributes
@@ -669,6 +679,7 @@ def test_create_simple_unpermitted_attributes
669679

670680
assert_response :bad_request
671681
assert_match /subject/, json_response['errors'][0]['detail']
682+
assert_equal nil, response.location
672683
end
673684

674685
def test_create_simple_unpermitted_attributes_allow_extra_params
@@ -703,6 +714,7 @@ def test_create_simple_unpermitted_attributes_allow_extra_params
703714
assert_equal "Param not allowed", json_response['meta']["warnings"][0]["title"]
704715
assert_equal "subject is not allowed.", json_response['meta']["warnings"][0]["detail"]
705716
assert_equal '105', json_response['meta']["warnings"][0]["code"]
717+
assert_equal json_response['data']['links']['self'], response.location
706718
ensure
707719
JSONAPI.configuration.raise_if_parameters_not_allowed = true
708720
end
@@ -730,6 +742,7 @@ def test_create_with_links_to_many_type_ids
730742
assert_equal '3', json_response['data']['relationships']['author']['data']['id']
731743
assert_equal 'JR is Great', json_response['data']['attributes']['title']
732744
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
733746
end
734747

735748
def test_create_with_links_to_many_array
@@ -755,6 +768,7 @@ def test_create_with_links_to_many_array
755768
assert_equal '3', json_response['data']['relationships']['author']['data']['id']
756769
assert_equal 'JR is Great', json_response['data']['attributes']['title']
757770
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
758772
end
759773

760774
def test_create_with_links_include_and_fields
@@ -781,6 +795,7 @@ def test_create_with_links_include_and_fields
781795
assert_equal '3', json_response['data']['relationships']['author']['data']['id']
782796
assert_equal 'JR is Great!', json_response['data']['attributes']['title']
783797
assert_not_nil json_response['included'].size
798+
assert_equal json_response['data']['links']['self'], response.location
784799
end
785800

786801
def test_update_with_links

0 commit comments

Comments
 (0)