From 10500824e784e1eb44d2411447b30ffe3619ecb6 Mon Sep 17 00:00:00 2001 From: Daniel Schweighoefer Date: Fri, 26 Feb 2016 18:26:51 +0100 Subject: [PATCH 1/2] fix resource identifier objects According to http://jsonapi.org/format/#document-resource-identifier-objects rescource identifier objects can contain an additional meta attribute. This fix adds supports for the full specification. --- lib/jsonapi/request_parser.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/jsonapi/request_parser.rb b/lib/jsonapi/request_parser.rb index da60aedd0..7368628f3 100644 --- a/lib/jsonapi/request_parser.rb +++ b/lib/jsonapi/request_parser.rb @@ -387,14 +387,15 @@ def parse_to_one_links_object(raw) } end - if !(raw.is_a?(Hash) || raw.is_a?(ActionController::Parameters)) || - raw.keys.length != 2 || !(raw.key?('type') && raw.key?('id')) + if !raw.is_a?(Hash) || raw.is_a?(ActionController::Parameters)) || + (raw.length != 2 && !raw.key?('meta')) || !(raw.key?('type') && raw.key?('id')) fail JSONAPI::Exceptions::InvalidLinksObject.new end { type: unformat_key(raw['type']).to_s, - id: raw['id'] + id: raw['id'], + meta: raw['meta'] } end From 7ce6c3e8bbde4ed37b86e942f630b83b06ae1010 Mon Sep 17 00:00:00 2001 From: Daniel Schweighoefer Date: Wed, 20 Apr 2016 18:48:02 +0200 Subject: [PATCH 2/2] set meta only if present, introduce helper method --- lib/jsonapi/request_parser.rb | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/lib/jsonapi/request_parser.rb b/lib/jsonapi/request_parser.rb index 7368628f3..9e84b68da 100644 --- a/lib/jsonapi/request_parser.rb +++ b/lib/jsonapi/request_parser.rb @@ -381,22 +381,20 @@ def verify_type(type) def parse_to_one_links_object(raw) if raw.nil? - return { - type: nil, - id: nil - } + return link_object_hash(nil, nil) end - if !raw.is_a?(Hash) || raw.is_a?(ActionController::Parameters)) || - (raw.length != 2 && !raw.key?('meta')) || !(raw.key?('type') && raw.key?('id')) + if !(raw.is_a?(Hash) || raw.is_a?(ActionController::Parameters)) || (raw.length != 2 && !raw.key?('meta')) || !(raw.key?('type') && raw.key?('id')) fail JSONAPI::Exceptions::InvalidLinksObject.new end - { - type: unformat_key(raw['type']).to_s, - id: raw['id'], - meta: raw['meta'] - } + link_object_hash(unformat_key(raw['type']).to_s, raw['id'], raw['meta']) + end + + def link_object_hash(type, id, meta=nil) + link_object = {type: type, id: id} + link_object[:meta] = meta unless meta.nil? + link_object end def parse_to_many_links_object(raw)