Skip to content

Don't crash obscurely on missing data payload for POST & PATCH requests #50

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

Open
wants to merge 2 commits 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
4 changes: 2 additions & 2 deletions lib/jsonapi_compliable/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def jsonapi_scope(scope, opts = {})
# @see Deserializer#initialize
# @return [Deserializer]
def deserialized_params
@deserialized_params ||= JsonapiCompliable::Deserializer.new(params, request.env)
@deserialized_params ||= JsonapiCompliable::Deserializer.new(params, request.method, request.env)
end

# Create the resource model and process all nested relationships via the
Expand Down Expand Up @@ -243,7 +243,7 @@ def default_jsonapi_render_options
private

def force_includes?
not deserialized_params.data.nil?
not deserialized_params.data.empty?
end

def perform_render_jsonapi(opts)
Expand Down
10 changes: 8 additions & 2 deletions lib/jsonapi_compliable/deserializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,22 @@
# { type: 'authors', method: :create, temp_id: 'abc123' }
class JsonapiCompliable::Deserializer
# @param payload [Hash] The incoming payload with symbolized keys
# @param method [String] The method that the request was sent with
# @param env [Hash] the Rack env (e.g. +request.env+).
def initialize(payload, env)
def initialize(payload, method, env)
@payload = payload
@payload = @payload[:_jsonapi] if @payload.has_key?(:_jsonapi)
@method = method
@env = env
end

# @return [Hash] the raw :data value of the payload
def data
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All looks good, except I was hoping we could do something like validate_payload! within the constructor, to keep the logic out of the accessor. The intent is to eventually do more complex validations, possibly using jsonapi-rb's parser.

@payload[:data]
if ["GET", "DELETE"].include?(@method)
@payload[:data] || {}
else
@payload[:data] or raise JsonapiCompliable::Errors::BadRequest.new("No data payload present")
end
end

# @return [String] the raw :id value of the payload
Expand Down
9 changes: 5 additions & 4 deletions lib/jsonapi_compliable/errors.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
module JsonapiCompliable
module Errors
class BadFilter < StandardError; end
class ValidationError < StandardError; end
class ValidationError < RuntimeError; end
class BadRequest < RuntimeError; end
class BadFilter < BadRequest; end

class UnsupportedPageSize < StandardError
class UnsupportedPageSize < BadRequest
def initialize(size, max)
@size, @max = size, max
end
Expand All @@ -13,7 +14,7 @@ def message
end
end

class StatNotFound < StandardError
class StatNotFound < BadRequest
def initialize(attribute, calculation)
@attribute = attribute
@calculation = calculation
Expand Down
36 changes: 35 additions & 1 deletion spec/deserializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,29 @@
}
end

let(:instance) { described_class.new(payload, {}) }
let(:instance) { described_class.new(payload, "GET", {}) }

describe '#data' do
subject { instance.data }

context 'when data is present' do
it 'returns the proper sub-object' do
expect(subject).to eq(payload[:data])
end
end

context 'when data is absent' do
let(:payload) do
{
foo: 'bar'
}
end

it 'returns an empty hash' do
expect(subject).to eq({})
end
end
end

describe '#attributes' do
subject { instance.attributes }
Expand All @@ -31,6 +53,18 @@
expect(subject[:id]).to eq('123')
end
end

context 'when data is absent' do
let(:payload) do
{
foo: 'bar'
}
end

it 'returns an empty hash' do
expect(subject).to eq({})
end
end
end

describe '#relationships' do
Expand Down