Skip to content
Closed
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
6 changes: 4 additions & 2 deletions lib/jsonapi/deserialization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ def jsonapi_inflector
def jsonapi_deserialize(document, options = {})
if document.respond_to?(:permit!)
# Handle Rails params...
primary_data = document.dup.require(:data).permit!.as_json
primary_data =
document.dup.require(:data).permit!.to_h.deep_stringify_keys
elsif document.is_a?(Hash)
primary_data = (document.as_json['data'] || {}).deep_dup
primary_data =
(document.to_h.deep_stringify_keys['data'] || {}).deep_dup
else
return {}
end
Expand Down
20 changes: 17 additions & 3 deletions spec/deserialization_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

RSpec.describe JSONAPI::Deserialization do
let(:jsonapi_deserialize) { UsersController.new.method(:jsonapi_deserialize) }
let(:file_object) { ::File.new('./Gemfile') }
let(:document) do
{
data: {
id: 1,
type: 'note',
attributes: {
title: 'Title 1',
date: '2015-12-20'
date: '2015-12-20',
file: file_object
},
relationships: {
author: {
Expand Down Expand Up @@ -40,7 +42,7 @@

describe '#jsonapi_deserialize' do
it do
expect(jsonapi_deserialize.call(document)).to eq(
expect(jsonapi_deserialize.call(document)).to include(
'id' => 1,
'date' => '2015-12-20',
'title' => 'Title 1',
Expand All @@ -50,6 +52,18 @@
)
end

context 'with file attribute' do
it do
expect(jsonapi_deserialize.call(document)['file'])
.to be_an_instance_of(File)
end

it do
expect(jsonapi_deserialize.call(document)['file'])
.not_to be_an_instance_of(String)
end
end

context 'with `only`' do
it do
expect(jsonapi_deserialize.call(document, only: :notes)).to eq(
Expand All @@ -61,7 +75,7 @@
context 'with `except`' do
it do
expect(
jsonapi_deserialize.call(document, except: [:date, :title])
jsonapi_deserialize.call(document, except: [:date, :title, :file])
).to eq(
'id' => 1,
'author_id' => 2,
Expand Down