Merge pull request #1248 from beauby/jsonapi-parse

JSON API deserialization.
This commit is contained in:
Lucas Hosseini
2016-01-13 06:18:52 +01:00
7 changed files with 421 additions and 0 deletions

View File

@@ -0,0 +1,59 @@
require 'test_helper'
module ActionController
module Serialization
class JsonApi
class DeserializationTest < ActionController::TestCase
class DeserializationTestController < ActionController::Base
def render_parsed_payload
parsed_hash = ActiveModelSerializers::Deserialization.jsonapi_parse(params)
render json: parsed_hash
end
end
tests DeserializationTestController
def test_deserialization
hash = {
'data' => {
'type' => 'photos',
'id' => 'zorglub',
'attributes' => {
'title' => 'Ember Hamster',
'src' => 'http://example.com/images/productivity.png'
},
'relationships' => {
'author' => {
'data' => nil
},
'photographer' => {
'data' => { 'type' => 'people', 'id' => '9' }
},
'comments' => {
'data' => [
{ 'type' => 'comments', 'id' => '1' },
{ 'type' => 'comments', 'id' => '2' }
]
}
}
}
}
post :render_parsed_payload, hash
response = JSON.parse(@response.body)
expected = {
'id' => 'zorglub',
'title' => 'Ember Hamster',
'src' => 'http://example.com/images/productivity.png',
'author_id' => nil,
'photographer_id' => '9',
'comment_ids' => %w(1 2)
}
assert_equal(expected, response)
end
end
end
end
end

View File

@@ -0,0 +1,139 @@
require 'test_helper'
module ActiveModel
class Serializer
module Adapter
class JsonApi
module Deserialization
class ParseTest < Minitest::Test
def setup
@hash = {
'data' => {
'type' => 'photos',
'id' => 'zorglub',
'attributes' => {
'title' => 'Ember Hamster',
'src' => 'http://example.com/images/productivity.png'
},
'relationships' => {
'author' => {
'data' => nil
},
'photographer' => {
'data' => { 'type' => 'people', 'id' => '9' }
},
'comments' => {
'data' => [
{ 'type' => 'comments', 'id' => '1' },
{ 'type' => 'comments', 'id' => '2' }
]
}
}
}
}
@params = ActionController::Parameters.new(@hash)
@expected = {
id: 'zorglub',
title: 'Ember Hamster',
src: 'http://example.com/images/productivity.png',
author_id: nil,
photographer_id: '9',
comment_ids: %w(1 2)
}
@illformed_payloads = [nil,
{},
{
'data' => nil
}, {
'data' => { 'attributes' => [] }
}, {
'data' => { 'relationships' => [] }
}, {
'data' => {
'relationships' => { 'rel' => nil }
}
}, {
'data' => {
'relationships' => { 'rel' => {} }
}
}]
end
def test_hash
parsed_hash = ActiveModel::Serializer::Adapter::JsonApi::Deserialization.parse!(@hash)
assert_equal(@expected, parsed_hash)
end
def test_actioncontroller_parameters
assert_equal(false, @params.permitted?)
parsed_hash = ActiveModel::Serializer::Adapter::JsonApi::Deserialization.parse!(@params)
assert_equal(@expected, parsed_hash)
end
def test_illformed_payloads_safe
@illformed_payloads.each do |p|
parsed_hash = ActiveModel::Serializer::Adapter::JsonApi::Deserialization.parse(p)
assert_equal({}, parsed_hash)
end
end
def test_illformed_payloads_unsafe
@illformed_payloads.each do |p|
assert_raises(InvalidDocument) do
ActiveModel::Serializer::Adapter::JsonApi::Deserialization.parse!(p)
end
end
end
def test_filter_fields_only
parsed_hash = ActiveModel::Serializer::Adapter::JsonApi::Deserialization.parse!(@hash, only: [:id, :title, :author])
expected = {
id: 'zorglub',
title: 'Ember Hamster',
author_id: nil
}
assert_equal(expected, parsed_hash)
end
def test_filter_fields_except
parsed_hash = ActiveModel::Serializer::Adapter::JsonApi::Deserialization.parse!(@hash, except: [:id, :title, :author])
expected = {
src: 'http://example.com/images/productivity.png',
photographer_id: '9',
comment_ids: %w(1 2)
}
assert_equal(expected, parsed_hash)
end
def test_keys
parsed_hash = ActiveModel::Serializer::Adapter::JsonApi::Deserialization.parse!(@hash, keys: { author: :user, title: :post_title })
expected = {
id: 'zorglub',
post_title: 'Ember Hamster',
src: 'http://example.com/images/productivity.png',
user_id: nil,
photographer_id: '9',
comment_ids: %w(1 2)
}
assert_equal(expected, parsed_hash)
end
def test_polymorphic
parsed_hash = ActiveModel::Serializer::Adapter::JsonApi::Deserialization.parse!(@hash, polymorphic: [:photographer])
expected = {
id: 'zorglub',
title: 'Ember Hamster',
src: 'http://example.com/images/productivity.png',
author_id: nil,
photographer_id: '9',
photographer_type: 'people',
comment_ids: %w(1 2)
}
assert_equal(expected, parsed_hash)
end
end
end
end
end
end
end