Skip to content

Commit 6288203

Browse files
committed
Revert "Add Rails >= 5.0.beta3 JSON API params parsing" (#1751)
1 parent 94db09b commit 6288203

File tree

6 files changed

+46
-213
lines changed

6 files changed

+46
-213
lines changed

CHANGELOG.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ Features:
99
Fixes:
1010
- [#1710](https://github.com/rails-api/active_model_serializers/pull/1710) Prevent association loading when `include_data` option
1111
is set to `false`. (@groyoh)
12-
- [#1747](https://github.com/rails-api/active_model_serializers/pull/1747) Improve jsonapi mime type registration for Rails 5 (@remear)
1312

1413
Misc:
1514
- [#1734](https://github.com/rails-api/active_model_serializers/pull/1734) Adds documentation for conditional attribute (@lambda2)

lib/active_model_serializers/register_jsonapi_renderer.rb

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,54 +22,43 @@
2222
# render jsonapi: model
2323
#
2424
# No wrapper format needed as it does not apply (i.e. no `wrap_parameters format: [jsonapi]`)
25+
2526
module ActiveModelSerializers::Jsonapi
2627
MEDIA_TYPE = 'application/vnd.api+json'.freeze
2728
HEADERS = {
2829
response: { 'CONTENT_TYPE'.freeze => MEDIA_TYPE },
2930
request: { 'ACCEPT'.freeze => MEDIA_TYPE }
3031
}.freeze
31-
32-
def self.install
33-
# actionpack/lib/action_dispatch/http/mime_types.rb
34-
Mime::Type.register MEDIA_TYPE, :jsonapi
35-
36-
if Rails::VERSION::MAJOR >= 5
37-
ActionDispatch::Request.parameter_parsers[:jsonapi] = parser
38-
else
39-
ActionDispatch::ParamsParser::DEFAULT_PARSERS[Mime[:jsonapi]] = parser
40-
end
41-
42-
# ref https://github.com/rails/rails/pull/21496
43-
ActionController::Renderers.add :jsonapi do |json, options|
44-
json = serialize_jsonapi(json, options).to_json(options) unless json.is_a?(String)
45-
self.content_type ||= Mime[:jsonapi]
46-
self.response_body = json
47-
end
48-
end
49-
50-
# Proposal: should actually deserialize the JSON API params
51-
# to the hash format expected by `ActiveModel::Serializers::JSON`
52-
# actionpack/lib/action_dispatch/http/parameters.rb
53-
def self.parser
54-
lambda do |body|
55-
data = JSON.parse(body)
56-
data = { :_json => data } unless data.is_a?(Hash)
57-
data.with_indifferent_access
58-
end
59-
end
60-
6132
module ControllerSupport
6233
def serialize_jsonapi(json, options)
6334
options[:adapter] = :json_api
64-
options.fetch(:serialization_context) do
65-
options[:serialization_context] = ActiveModelSerializers::SerializationContext.new(request)
66-
end
35+
options.fetch(:serialization_context) { options[:serialization_context] = ActiveModelSerializers::SerializationContext.new(request) }
6736
get_serializer(json, options)
6837
end
6938
end
7039
end
7140

72-
ActiveModelSerializers::Jsonapi.install
41+
# actionpack/lib/action_dispatch/http/mime_types.rb
42+
Mime::Type.register ActiveModelSerializers::Jsonapi::MEDIA_TYPE, :jsonapi
43+
44+
parsers = Rails::VERSION::MAJOR >= 5 ? ActionDispatch::Http::Parameters : ActionDispatch::ParamsParser
45+
media_type = Mime::Type.lookup(ActiveModelSerializers::Jsonapi::MEDIA_TYPE)
46+
47+
# Proposal: should actually deserialize the JSON API params
48+
# to the hash format expected by `ActiveModel::Serializers::JSON`
49+
# actionpack/lib/action_dispatch/http/parameters.rb
50+
parsers::DEFAULT_PARSERS[media_type] = lambda do |body|
51+
data = JSON.parse(body)
52+
data = { :_json => data } unless data.is_a?(Hash)
53+
data.with_indifferent_access
54+
end
55+
56+
# ref https://github.com/rails/rails/pull/21496
57+
ActionController::Renderers.add :jsonapi do |json, options|
58+
json = serialize_jsonapi(json, options).to_json(options) unless json.is_a?(String)
59+
self.content_type ||= media_type
60+
self.response_body = json
61+
end
7362

7463
ActiveSupport.on_load(:action_controller) do
7564
include ActiveModelSerializers::Jsonapi::ControllerSupport

test/action_controller/json_api/linked_test.rb

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
module ActionController
44
module Serialization
55
class JsonApi
6-
class LinkedTest < ActionDispatch::IntegrationTest
6+
class LinkedTest < ActionController::TestCase
77
class LinkedTestController < ActionController::Base
8+
require 'active_model_serializers/register_jsonapi_renderer'
89
def setup_post
910
ActionController::Base.cache_store.clear
1011
@role1 = Role.new(id: 1, name: 'admin')
@@ -38,76 +39,70 @@ def setup_post
3839

3940
def render_resource_without_include
4041
setup_post
41-
render json: @post
42+
render jsonapi: @post
4243
end
4344

4445
def render_resource_with_include
4546
setup_post
46-
render json: @post, adapter: :json_api, include: [:author]
47+
render jsonapi: @post, include: [:author]
4748
end
4849

4950
def render_resource_with_include_of_custom_key_by_original
5051
setup_post
51-
render json: @post, adapter: :json_api, include: [:reviews], serializer: PostWithCustomKeysSerializer
52+
render jsonapi: @post, include: [:reviews], serializer: PostWithCustomKeysSerializer
5253
end
5354

5455
def render_resource_with_nested_include
5556
setup_post
56-
render json: @post, adapter: :json_api, include: [comments: [:author]]
57+
render jsonapi: @post, include: [comments: [:author]]
5758
end
5859

5960
def render_resource_with_nested_has_many_include_wildcard
6061
setup_post
61-
render json: @post, adapter: :json_api, include: 'author.*'
62+
render jsonapi: @post, include: 'author.*'
6263
end
6364

6465
def render_resource_with_missing_nested_has_many_include
6566
setup_post
6667
@post.author = @author2 # author2 has no roles.
67-
render json: @post, adapter: :json_api, include: [author: [:roles]]
68+
render jsonapi: @post, include: [author: [:roles]]
6869
end
6970

7071
def render_collection_with_missing_nested_has_many_include
7172
setup_post
7273
@post.author = @author2
73-
render json: [@post, @post2], adapter: :json_api, include: [author: [:roles]]
74+
render jsonapi: [@post, @post2], include: [author: [:roles]]
7475
end
7576

7677
def render_collection_without_include
7778
setup_post
78-
render json: [@post], adapter: :json_api
79+
render jsonapi: [@post]
7980
end
8081

8182
def render_collection_with_include
8283
setup_post
83-
render json: [@post], adapter: :json_api, include: 'author, comments'
84+
render jsonapi: [@post], include: 'author, comments'
8485
end
8586
end
8687

87-
setup do
88-
@routes = Rails.application.routes.draw do
89-
ActiveSupport::Deprecation.silence do
90-
match ':action', :to => LinkedTestController, via: [:get, :post]
91-
end
92-
end
93-
end
88+
tests LinkedTestController
9489

9590
def test_render_resource_without_include
96-
get '/render_resource_without_include'
91+
get :render_resource_without_include
9792
response = JSON.parse(@response.body)
9893
refute response.key? 'included'
9994
end
10095

10196
def test_render_resource_with_include
102-
get '/render_resource_with_include'
97+
get :render_resource_with_include
10398
response = JSON.parse(@response.body)
10499
assert response.key? 'included'
105100
assert_equal 1, response['included'].size
106101
assert_equal 'Steve K.', response['included'].first['attributes']['name']
107102
end
108103

109104
def test_render_resource_with_nested_has_many_include
110-
get '/render_resource_with_nested_has_many_include_wildcard'
105+
get :render_resource_with_nested_has_many_include_wildcard
111106
response = JSON.parse(@response.body)
112107
expected_linked = [
113108
{
@@ -149,7 +144,7 @@ def test_render_resource_with_nested_has_many_include
149144
end
150145

151146
def test_render_resource_with_include_of_custom_key_by_original
152-
get '/render_resource_with_include_of_custom_key_by_original'
147+
get :render_resource_with_include_of_custom_key_by_original
153148
response = JSON.parse(@response.body)
154149
assert response.key? 'included'
155150

@@ -161,33 +156,33 @@ def test_render_resource_with_include_of_custom_key_by_original
161156
end
162157

163158
def test_render_resource_with_nested_include
164-
get '/render_resource_with_nested_include'
159+
get :render_resource_with_nested_include
165160
response = JSON.parse(@response.body)
166161
assert response.key? 'included'
167162
assert_equal 3, response['included'].size
168163
end
169164

170165
def test_render_collection_without_include
171-
get '/render_collection_without_include'
166+
get :render_collection_without_include
172167
response = JSON.parse(@response.body)
173168
refute response.key? 'included'
174169
end
175170

176171
def test_render_collection_with_include
177-
get '/render_collection_with_include'
172+
get :render_collection_with_include
178173
response = JSON.parse(@response.body)
179174
assert response.key? 'included'
180175
end
181176

182177
def test_render_resource_with_nested_attributes_even_when_missing_associations
183-
get '/render_resource_with_missing_nested_has_many_include'
178+
get :render_resource_with_missing_nested_has_many_include
184179
response = JSON.parse(@response.body)
185180
assert response.key? 'included'
186181
refute has_type?(response['included'], 'roles')
187182
end
188183

189184
def test_render_collection_with_missing_nested_has_many_include
190-
get '/render_collection_with_missing_nested_has_many_include'
185+
get :render_collection_with_missing_nested_has_many_include
191186
response = JSON.parse(@response.body)
192187
assert response.key? 'included'
193188
assert has_type?(response['included'], 'roles')

test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb

Lines changed: 0 additions & 143 deletions
This file was deleted.

test/support/isolated_unit.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141

4242
# These files do not require any others and are needed
4343
# to run the tests
44-
require 'active_support/testing/autorun'
4544
require 'active_support/testing/isolation'
4645

4746
module TestHelpers

0 commit comments

Comments
 (0)