You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
jsonapi-rails breaks the shortcut form of render which dispatches on the single argument's value (render :action_name, render 'template/name', render '/file/name').
I have stumbled upon this issue when tried out the 'ActiveRecord Errors' branch.
This can be reproduced on the current HEADs of both master (dc373eb) and ar-validation-errors (498c2b5).
Steps to reproduce
(Optional) Create new gemset to ensure clean environment:
bin/rails generate model Post title:text
echo'2.times { |i| Post.create!(title: "Post ##{i + 1}") }'> db/seeds.rb
bin/rake db:create db:migrate db:seed
bin/rails generate jsonapi:serializable Post
Setup sample actions
cat <<RUBY> config/routes.rbRails.application.routes.draw do get '/api', to:'application#api' get '/html', to:'application#html'endRUBY
cat <<RUBY> app/controllers/application_controller.rbclassApplicationController < ActionController::Base# sample action to make sure jsonapi-rails worksdefapi render jsonapi:Post.allend# this action failsdefhtml render :welcomeendendRUBY
Start Rails server and hit '/api' to make sure jsonapi-rails works.
Hit '/html'
Expected behavior
Rails fails with an ActionView::MissingTemplate error.
Some debugging reveals that the error comes from this line (introduced in 20a6756) where #merge is being called on the :welcome Symbol.
I'm not very familiar with the Rails internals, but it seems like the signature of JSONAPI::Rails::ActionController#render (which takes a single hash argument) is not compatible with the ActionPack's render (both AbstractController::Rendering and ActionController::Renderer can take variable number of arguments).
I see several workarounds:
Change the signature of JSONAPI::Rails::ActionController#render and have it normalize its arguments somehow. But this will likely duplicate the work ActionPack does under the hood to process the shortcut syntax of render.
Drop JSONAPI::Rails::ActionController#render and override AbstractController::Rendering#_normalize_options instead. I don't like it because _normalize_options is kinda undocumented and I have not seen a project that actually uses it.
Move providing :_reverse_mapping inside the renderer proc, since the proc is evaluated in the controller context and has access to request. Another benefit is that it will modify options passed to the JSONAPI renderers only.
I have implemented the second and the third options and they both work on the sample application.
The text was updated successfully, but these errors were encountered:
jsonapi-rails
breaks the shortcut form ofrender
which dispatches on the single argument's value (render :action_name
,render 'template/name'
,render '/file/name'
).I have stumbled upon this issue when tried out the 'ActiveRecord Errors' branch.
This can be reproduced on the current HEADs of both
master
(dc373eb) andar-validation-errors
(498c2b5).Steps to reproduce
Start Rails server and hit '/api' to make sure
jsonapi-rails
works.Hit '/html'
Expected behavior
Rails fails with an
ActionView::MissingTemplate
error.Actual behavior
Rails fails with the following error:
Workarounds
Some debugging reveals that the error comes from this line (introduced in 20a6756) where
#merge
is being called on the:welcome
Symbol.I'm not very familiar with the Rails internals, but it seems like the signature of
JSONAPI::Rails::ActionController#render
(which takes a single hash argument) is not compatible with the ActionPack'srender
(bothAbstractController::Rendering
andActionController::Renderer
can take variable number of arguments).I see several workarounds:
Change the signature of
JSONAPI::Rails::ActionController#render
and have it normalize its arguments somehow. But this will likely duplicate the work ActionPack does under the hood to process the shortcut syntax ofrender
.Drop
JSONAPI::Rails::ActionController#render
and overrideAbstractController::Rendering#_normalize_options
instead. I don't like it because_normalize_options
is kinda undocumented and I have not seen a project that actually uses it.Move providing
:_reverse_mapping
inside the renderer proc, since the proc is evaluated in the controller context and has access torequest
. Another benefit is that it will modify options passed to the JSONAPI renderers only.I have implemented the second and the third options and they both work on the sample application.
The text was updated successfully, but these errors were encountered: