Skip to content

Commit 8bda6a7

Browse files
committed
upgrade to jsonapi beta5, improve tests to be valid resource requests
1 parent 8940c49 commit 8bda6a7

File tree

4 files changed

+48
-47
lines changed

4 files changed

+48
-47
lines changed

Gemfile

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ eval_gemfile local_gemfile if File.readable?(local_gemfile)
77
# Specify your gem's dependencies in active_model_serializers.gemspec
88
gemspec
99

10-
# TODO: remove when @beauby re-publishes this gem
11-
gem 'jsonapi', github: 'beauby/jsonapi'
12-
1310
version = ENV['RAILS_VERSION'] || '4.2'
1411

1512
if version == 'master'

active_model_serializers.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Gem::Specification.new do |spec|
4242
# 'minitest'
4343
# 'thread_safe'
4444

45-
spec.add_runtime_dependency 'jsonapi', '~> 0.1.1.beta2'
45+
spec.add_runtime_dependency 'jsonapi', '~> 0.1.1.beta5'
4646
spec.add_runtime_dependency 'case_transform', '>= 0.2'
4747

4848
spec.add_development_dependency 'activerecord', rails_versions

lib/active_model_serializers/adapter/json_api/deserialization.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,8 @@ def parse!(document, options = {})
8181
# Same as parse!, but returns an empty hash instead of raising InvalidDocument
8282
# on invalid payloads.
8383
def parse(document, options = {})
84-
document = JSONAPI.parse(document, options)
85-
document = document.to_hash
86-
84+
JSONAPI.parse_response!(document)
85+
document = document.to_h
8786
primary_data = document['data']
8887

8988
# null data is allowed, as per the JSON API Schema
@@ -102,7 +101,8 @@ def parse(document, options = {})
102101

103102
hash
104103

105-
rescue JSONAPI::Parser::InvalidDocument
104+
rescue JSONAPI::Parser::InvalidDocument => e
105+
puts e.message
106106
return {} unless block_given?
107107
yield
108108
end

test/action_controller/json_api/deserialization_test.rb

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ class JsonApi
66
class DeserializationTest < ActionController::TestCase
77
class DeserializationTestController < ActionController::Base
88
def render_parsed_payload
9-
parsed_hash = ActiveModelSerializers::Deserialization.jsonapi_parse(params)
9+
parsed_hash = ActiveModelSerializers::Deserialization.jsonapi_parse(params['resource'])
1010
render json: parsed_hash
1111
end
1212

1313
def render_polymorphic_parsed_payload
1414
parsed_hash = ActiveModelSerializers::Deserialization.jsonapi_parse(
15-
params,
15+
params['resource'],
1616
polymorphic: [:restriction_for, :restricted_to]
1717
)
1818
render json: parsed_hash
@@ -23,17 +23,19 @@ def render_polymorphic_parsed_payload
2323

2424
def test_deserialization_of_relationship_only_object
2525
hash = {
26-
'data' => {
27-
'type' => 'restraints',
28-
'relationships' => {
29-
'restriction_for' => {
30-
'data' => {
31-
'type' => 'discounts',
32-
'id' => '67'
26+
'resource' => {
27+
'data' => {
28+
'type' => 'restraints',
29+
'relationships' => {
30+
'restriction_for' => {
31+
'data' => {
32+
'type' => 'discounts',
33+
'id' => '67'
34+
}
35+
},
36+
'restricted_to' => {
37+
'data' => nil
3338
}
34-
},
35-
'restricted_to' => {
36-
'data' => nil
3739
}
3840
}
3941
},
@@ -55,34 +57,36 @@ def test_deserialization_of_relationship_only_object
5557

5658
def test_deserialization
5759
hash = {
58-
'data' => {
59-
'type' => 'photos',
60-
'id' => 'zorglub',
61-
'attributes' => {
62-
'title' => 'Ember Hamster',
63-
'src' => 'http://example.com/images/productivity.png',
64-
'image-width' => '200',
65-
'imageHeight' => '200',
66-
'ImageSize' => '1024'
67-
},
68-
'relationships' => {
69-
'author' => {
70-
'data' => nil
71-
},
72-
'photographer' => {
73-
'data' => { 'type' => 'people', 'id' => '9' }
60+
'resource' => {
61+
'data' => {
62+
'type' => 'photos',
63+
'id' => 'zorglub',
64+
'attributes' => {
65+
'title' => 'Ember Hamster',
66+
'src' => 'http://example.com/images/productivity.png',
67+
'image-width' => '200',
68+
'imageHeight' => '200',
69+
'ImageSize' => '1024'
7470
},
75-
'comments' => {
76-
'data' => [
77-
{ 'type' => 'comments', 'id' => '1' },
78-
{ 'type' => 'comments', 'id' => '2' }
79-
]
80-
},
81-
'related-images' => {
82-
'data' => [
83-
{ 'type' => 'image', 'id' => '7' },
84-
{ 'type' => 'image', 'id' => '8' }
85-
]
71+
'relationships' => {
72+
'author' => {
73+
'data' => nil
74+
},
75+
'photographer' => {
76+
'data' => { 'type' => 'people', 'id' => '9' }
77+
},
78+
'comments' => {
79+
'data' => [
80+
{ 'type' => 'comments', 'id' => '1' },
81+
{ 'type' => 'comments', 'id' => '2' }
82+
]
83+
},
84+
'related-images' => {
85+
'data' => [
86+
{ 'type' => 'image', 'id' => '7' },
87+
{ 'type' => 'image', 'id' => '8' }
88+
]
89+
}
8690
}
8791
}
8892
}

0 commit comments

Comments
 (0)