Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Render error when unable to deserialize resource #96

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/generators/jsonapi/initializer/templates/initializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,20 @@
# # Set a default pagination scheme.
# config.jsonapi_pagination = ->(_) { {} }
#
# # Set the default action when the payload cannot be deserialized
# config.jsonapi_payload_malformed = -> {
# render jsonapi_errors: {
# title: 'Non-compliant Request Body',
# detail: 'The request was not formatted in compliance with the application/vnd.api+json spec',
# links: {
# about: 'http://jsonapi.org/format/'
# }
# }, status: :bad_request
# }
#
# # Uncomment to take no action when the payload cannot be deserialized
# config.jsonapi_payload_malformed = nil
#
# # Set a logger.
# config.logger = Logger.new(STDOUT)
#
Expand Down
13 changes: 12 additions & 1 deletion lib/jsonapi/rails/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,30 @@ module Configurable

DEFAULT_JSONAPI_PAGINATION = ->(_) { {} }

DEFAULT_JSONAPI_PAYLOAD_MALFORMED = -> {
render jsonapi_errors: {
title: 'Non-compliant Request Body',
detail: 'The request was not formatted in compliance with the application/vnd.api+json spec',
links: {
about: 'http://jsonapi.org/format/'
}
}, status: :bad_request
}

DEFAULT_LOGGER = Logger.new(STDERR)

DEFAULT_CONFIG = {
jsonapi_cache: DEFAULT_JSONAPI_CACHE,
jsonapi_class: DEFAULT_JSONAPI_CLASS,
jsonapi_errors_class: DEFAULT_JSONAPI_ERRORS_CLASS,
jsonapi_cache: DEFAULT_JSONAPI_CACHE,
jsonapi_expose: DEFAULT_JSONAPI_EXPOSE,
jsonapi_fields: DEFAULT_JSONAPI_FIELDS,
jsonapi_include: DEFAULT_JSONAPI_INCLUDE,
jsonapi_links: DEFAULT_JSONAPI_LINKS,
jsonapi_meta: DEFAULT_JSONAPI_META,
jsonapi_object: DEFAULT_JSONAPI_OBJECT,
jsonapi_pagination: DEFAULT_JSONAPI_PAGINATION,
jsonapi_payload_malformed: DEFAULT_JSONAPI_PAYLOAD_MALFORMED,
logger: DEFAULT_LOGGER
}.freeze

Expand Down
1 change: 1 addition & 0 deletions lib/jsonapi/rails/controller/deserialization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def deserializable_resource(key, options = {}, &block)
"Unable to deserialize #{key} because no JSON API payload was" \
" found. (#{controller.controller_name}##{params[:action]})"
end
controller.jsonapi_payload_malformed
next
end

Expand Down
6 changes: 6 additions & 0 deletions lib/jsonapi/rails/controller/hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ def jsonapi_meta
def jsonapi_pagination(resources)
instance_exec(resources, &JSONAPI::Rails.config[:jsonapi_pagination])
end

# Hook for rendering jsonapi_errors when no payload passed
def jsonapi_payload_malformed
return unless JSONAPI::Rails.config[:jsonapi_payload_malformed]
instance_exec(&JSONAPI::Rails.config[:jsonapi_payload_malformed])
end
end
end
end
Expand Down
43 changes: 43 additions & 0 deletions spec/deserialization_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,47 @@ def create
expect(controller.jsonapi_pointers).to eq(expected)
end
end

context 'when unable to deserialize resource from params' do
controller do
deserializable_resource :user

def create
render plain: :ok
end
end

context 'with the default config' do
it 'makes the deserialized resource available in params' do
post :create

expect(response.body).to eq(
{
:errors => [
{
:links => {
:about => "http://jsonapi.org/format/"
},
:title => "Non-compliant Request Body",
:detail => "The request was not formatted in compliance with the application/vnd.api+json spec"
}
],
:jsonapi => {
:version=>"1.0"
}
}.to_json
)
expect(response).to be_bad_request
end
end

context 'when the config[:jsonapi_payload_malformed] == nil' do
it 'makes the deserialization mapping available via #jsonapi_pointers' do
with_config(jsonapi_payload_malformed: nil) { post :create }

expect(response.body).to eq('ok')
expect(response).to be_success
end
end
end
end
4 changes: 3 additions & 1 deletion spec/dummy/config/initializers/new_framework_defaults.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
Rails.application.config.active_record.belongs_to_required_by_default = true

# Do not halt callback chains when a callback returns false. Previous versions had true.
ActiveSupport.halt_callback_chains_on_return_false = false
if Rails.version.to_f < 5.2
ActiveSupport.halt_callback_chains_on_return_false = false
end

# Configure SSL options to enable HSTS with subdomains. Previous versions had false.
Rails.application.config.ssl_options = { hsts: { subdomains: true } }