Skip to content

Improve error handling in JSONAPI deserializer #139

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

Merged
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
2 changes: 1 addition & 1 deletion lib/jsonapi_compliable/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ def _persist
end

def force_includes?
not deserialized_params.data.nil?
not (deserialized_params.data.nil? || deserialized_params.data.empty?)
end

def perform_render_jsonapi(opts)
Expand Down
13 changes: 11 additions & 2 deletions lib/jsonapi_compliable/deserializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,23 @@ class JsonapiCompliable::Deserializer
# @param payload [Hash] The incoming payload with symbolized keys
# @param env [Hash] the Rack env (e.g. +request.env+).
def initialize(payload, env)
@payload = payload
@payload = payload || {}
@payload = @payload[:_jsonapi] if @payload.has_key?(:_jsonapi)
@env = env
validate_content_type
end

# checks Content-Type header and prints a warning if it doesn't seem correct
def validate_content_type
content_type = @env['CONTENT_TYPE'] || ""
if !(content_type.include?("application/json") || content_type.include?("application/vnd.api+json"))
print("WARNING - JSONAPI Compliable :: Content-Type header appears to be set to an invalid value: #{content_type}\n")
end
end

# @return [Hash] the raw :data value of the payload
def data
@payload[:data]
@payload[:data] || {}
end

# @return [String] the raw :id value of the payload
Expand Down