Skip to content

Commit fa76ad2

Browse files
committed
Replace #as_json with #to_h because of wrong object value parsing
1 parent 3a685a7 commit fa76ad2

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

lib/jsonapi/deserialization.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@ def jsonapi_inflector
3535
def jsonapi_deserialize(document, options = {})
3636
if document.respond_to?(:permit!)
3737
# Handle Rails params...
38-
primary_data = document.dup.require(:data).permit!.as_json
38+
primary_data =
39+
document.dup.require(:data).permit!.to_h.with_indifferent_access
3940
elsif document.is_a?(Hash)
40-
primary_data = (document.as_json['data'] || {}).deep_dup
41+
primary_data =
42+
(document.to_h.with_indifferent_access['data'] || {}).deep_dup
4143
else
4244
return {}
4345
end

spec/deserialization_spec.rb

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22

33
RSpec.describe JSONAPI::Deserialization do
44
let(:jsonapi_deserialize) { UsersController.new.method(:jsonapi_deserialize) }
5+
let(:file_object) { ::File.new('./Gemfile') }
56
let(:document) do
67
{
78
data: {
89
id: 1,
910
type: 'note',
1011
attributes: {
1112
title: 'Title 1',
12-
date: '2015-12-20'
13+
date: '2015-12-20',
14+
file: file_object
1315
},
1416
relationships: {
1517
author: {
@@ -40,7 +42,7 @@
4042

4143
describe '#jsonapi_deserialize' do
4244
it do
43-
expect(jsonapi_deserialize.call(document)).to eq(
45+
expect(jsonapi_deserialize.call(document)).to include(
4446
'id' => 1,
4547
'date' => '2015-12-20',
4648
'title' => 'Title 1',
@@ -50,6 +52,18 @@
5052
)
5153
end
5254

55+
context 'with file attribute' do
56+
it do
57+
expect(jsonapi_deserialize.call(document)['file']).to
58+
be_an_instance_of(File)
59+
end
60+
61+
it do
62+
expect(jsonapi_deserialize.call(document)['file']).not_to
63+
be_an_instance_of(String)
64+
end
65+
end
66+
5367
context 'with `only`' do
5468
it do
5569
expect(jsonapi_deserialize.call(document, only: :notes)).to eq(
@@ -61,7 +75,7 @@
6175
context 'with `except`' do
6276
it do
6377
expect(
64-
jsonapi_deserialize.call(document, except: [:date, :title])
78+
jsonapi_deserialize.call(document, except: [:date, :title, :file])
6579
).to eq(
6680
'id' => 1,
6781
'author_id' => 2,

0 commit comments

Comments
 (0)