fun_with_json_api 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Rakefile +4 -1
- data/config/locales/fun_with_json_api.en.yml +29 -2
- data/lib/fun_with_json_api.rb +30 -2
- data/lib/fun_with_json_api/action_controller_extensions/serialization.rb +18 -0
- data/lib/fun_with_json_api/attribute.rb +3 -3
- data/lib/fun_with_json_api/attributes/relationship.rb +37 -23
- data/lib/fun_with_json_api/attributes/relationship_collection.rb +55 -38
- data/lib/fun_with_json_api/attributes/string_attribute.rb +12 -1
- data/lib/fun_with_json_api/attributes/uuid_v4_attribute.rb +27 -0
- data/lib/fun_with_json_api/controller_methods.rb +1 -1
- data/lib/fun_with_json_api/deserializer.rb +61 -8
- data/lib/fun_with_json_api/deserializer_class_methods.rb +37 -7
- data/lib/fun_with_json_api/exceptions/illegal_client_generated_identifier.rb +17 -0
- data/lib/fun_with_json_api/exceptions/invalid_client_generated_identifier.rb +17 -0
- data/lib/fun_with_json_api/exceptions/invalid_document_identifier.rb +17 -0
- data/lib/fun_with_json_api/exceptions/invalid_document_type.rb +20 -0
- data/lib/fun_with_json_api/exceptions/invalid_relationship.rb +5 -3
- data/lib/fun_with_json_api/exceptions/invalid_relationship_type.rb +17 -0
- data/lib/fun_with_json_api/exceptions/missing_resource.rb +15 -0
- data/lib/fun_with_json_api/exceptions/unknown_attribute.rb +15 -0
- data/lib/fun_with_json_api/exceptions/unknown_relationship.rb +15 -0
- data/lib/fun_with_json_api/find_collection_from_document.rb +124 -0
- data/lib/fun_with_json_api/find_resource_from_document.rb +112 -0
- data/lib/fun_with_json_api/pre_deserializer.rb +1 -0
- data/lib/fun_with_json_api/railtie.rb +30 -1
- data/lib/fun_with_json_api/schema_validator.rb +47 -0
- data/lib/fun_with_json_api/schema_validators/check_attributes.rb +52 -0
- data/lib/fun_with_json_api/schema_validators/check_document_id_matches_resource.rb +96 -0
- data/lib/fun_with_json_api/schema_validators/check_document_type_matches_resource.rb +40 -0
- data/lib/fun_with_json_api/schema_validators/check_relationships.rb +127 -0
- data/lib/fun_with_json_api/version.rb +1 -1
- data/spec/dummy/log/test.log +172695 -0
- data/spec/fixtures/active_record.rb +6 -0
- data/spec/fun_with_json_api/controller_methods_spec.rb +8 -3
- data/spec/fun_with_json_api/deserializer_class_methods_spec.rb +14 -6
- data/spec/fun_with_json_api/deserializer_spec.rb +155 -40
- data/spec/fun_with_json_api/exception_spec.rb +9 -9
- data/spec/fun_with_json_api/find_collection_from_document_spec.rb +203 -0
- data/spec/fun_with_json_api/find_resource_from_document_spec.rb +100 -0
- data/spec/fun_with_json_api/pre_deserializer_spec.rb +26 -26
- data/spec/fun_with_json_api/railtie_spec.rb +88 -0
- data/spec/fun_with_json_api/schema_validator_spec.rb +94 -0
- data/spec/fun_with_json_api/schema_validators/check_attributes_spec.rb +52 -0
- data/spec/fun_with_json_api/schema_validators/check_document_id_matches_resource_spec.rb +115 -0
- data/spec/fun_with_json_api/schema_validators/check_document_type_matches_resource_spec.rb +30 -0
- data/spec/fun_with_json_api/schema_validators/check_relationships_spec.rb +150 -0
- data/spec/fun_with_json_api_spec.rb +148 -4
- metadata +49 -4
- data/spec/example_spec.rb +0 -64
@@ -8,7 +8,7 @@ describe FunWithJsonApi::Exception do
|
|
8
8
|
describe '#message' do
|
9
9
|
subject { instance.message }
|
10
10
|
|
11
|
-
it '
|
11
|
+
it 'returns the developer-only exception message' do
|
12
12
|
expect(subject).to eq message
|
13
13
|
end
|
14
14
|
end
|
@@ -16,14 +16,14 @@ describe FunWithJsonApi::Exception do
|
|
16
16
|
describe '#payload' do
|
17
17
|
subject { instance.payload }
|
18
18
|
|
19
|
-
it '
|
19
|
+
it 'wraps the payload in a array' do
|
20
20
|
expect(subject).to eq [payload]
|
21
21
|
end
|
22
22
|
|
23
|
-
context 'with an array of payloads' do
|
23
|
+
context 'with an array of exceptions payloads' do
|
24
24
|
let(:payload) { Array.new(3) { FunWithJsonApi::ExceptionPayload.new } }
|
25
25
|
|
26
|
-
it '
|
26
|
+
it 'returns all exception payloads' do
|
27
27
|
expect(subject).to eq payload
|
28
28
|
end
|
29
29
|
end
|
@@ -32,14 +32,14 @@ describe FunWithJsonApi::Exception do
|
|
32
32
|
describe '#http_status' do
|
33
33
|
subject { instance.http_status }
|
34
34
|
|
35
|
-
it '
|
35
|
+
it 'defaults to returning 400' do
|
36
36
|
expect(subject).to eq 400
|
37
37
|
end
|
38
38
|
|
39
39
|
context 'with a payload with a status value' do
|
40
40
|
let(:payload) { FunWithJsonApi::ExceptionPayload.new.tap { |p| p.status = '422' } }
|
41
41
|
|
42
|
-
it '
|
42
|
+
it 'returns the payload status value' do
|
43
43
|
expect(subject).to eq 422
|
44
44
|
end
|
45
45
|
end
|
@@ -49,7 +49,7 @@ describe FunWithJsonApi::Exception do
|
|
49
49
|
Array.new(3) { FunWithJsonApi::ExceptionPayload.new.tap { |p| p.status = '403' } }
|
50
50
|
end
|
51
51
|
|
52
|
-
it '
|
52
|
+
it 'returns the common payload status value' do
|
53
53
|
expect(subject).to eq 403
|
54
54
|
end
|
55
55
|
end
|
@@ -59,7 +59,7 @@ describe FunWithJsonApi::Exception do
|
|
59
59
|
Array.new(3) { |i| FunWithJsonApi::ExceptionPayload.new.tap { |p| p.status = "40#{i}" } }
|
60
60
|
end
|
61
61
|
|
62
|
-
it '
|
62
|
+
it 'falls back to returning 400' do
|
63
63
|
expect(subject).to eq 400
|
64
64
|
end
|
65
65
|
end
|
@@ -69,7 +69,7 @@ describe FunWithJsonApi::Exception do
|
|
69
69
|
Array.new(3) { |i| FunWithJsonApi::ExceptionPayload.new.tap { |p| p.status = "50#{i}" } }
|
70
70
|
end
|
71
71
|
|
72
|
-
it '
|
72
|
+
it 'falls back to returning 500' do
|
73
73
|
expect(subject).to eq 500
|
74
74
|
end
|
75
75
|
end
|
@@ -0,0 +1,203 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe FunWithJsonApi::FindCollectionFromDocument do
|
4
|
+
describe '.find' do
|
5
|
+
let(:deserializer) { instance_double('FunWithJsonApi::Deserializer') }
|
6
|
+
subject { described_class.find(document, deserializer) }
|
7
|
+
|
8
|
+
context 'with a deserializer' do
|
9
|
+
before { allow(deserializer).to receive(:type).and_return('person') }
|
10
|
+
|
11
|
+
context 'with a document containing a resource that matches the expected type' do
|
12
|
+
let(:document) { { data: [{ id: '42', type: 'person' }] } }
|
13
|
+
|
14
|
+
context 'with a resource matching the document' do
|
15
|
+
let!(:resource) { double('resource') }
|
16
|
+
before do
|
17
|
+
allow(deserializer).to receive(:load_collection_from_id_values)
|
18
|
+
.with(['42'])
|
19
|
+
.and_return([resource])
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'returns the resource in a array' do
|
23
|
+
expect(subject).to eq [resource]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
context 'with a single resource that cannot be found' do
|
28
|
+
let!(:resource) { double('resource') }
|
29
|
+
before do
|
30
|
+
allow(deserializer).to receive(:load_collection_from_id_values)
|
31
|
+
.with(['42'])
|
32
|
+
.and_return([])
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'raises a MissingResource error' do
|
36
|
+
allow(deserializer).to receive(:id_param).and_return(:id)
|
37
|
+
allow(deserializer).to receive(:resource_class).and_return(
|
38
|
+
class_double('ActiveRecord::Base')
|
39
|
+
)
|
40
|
+
allow(deserializer).to receive(:format_collection_ids).with([]).and_return([])
|
41
|
+
|
42
|
+
expect { subject }.to raise_error(FunWithJsonApi::Exceptions::MissingResource) do |e|
|
43
|
+
expect(e.payload.size).to eq 1
|
44
|
+
|
45
|
+
payload = e.payload.first
|
46
|
+
expect(payload.status).to eq '404'
|
47
|
+
expect(payload.code).to eq 'missing_resource'
|
48
|
+
expect(payload.title).to eq 'Unable to find the requested resource'
|
49
|
+
expect(payload.detail).to eq "Unable to find 'person' with matching id: '42'"
|
50
|
+
expect(payload.pointer).to eq '/data/0/id'
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
context 'with a document containing multiple resource that matches the expected type' do
|
57
|
+
let(:document) do
|
58
|
+
{
|
59
|
+
data: [
|
60
|
+
{ id: '42', type: 'person' },
|
61
|
+
{ id: '43', type: 'person' },
|
62
|
+
{ id: '44', type: 'person' }
|
63
|
+
]
|
64
|
+
}
|
65
|
+
end
|
66
|
+
|
67
|
+
context 'when all resources can be found' do
|
68
|
+
let!(:resource_a) { double('resource', code: '42') }
|
69
|
+
let!(:resource_b) { double('resource', code: '43') }
|
70
|
+
let!(:resource_c) { double('resource', code: '44') }
|
71
|
+
before do
|
72
|
+
allow(deserializer).to receive(:load_collection_from_id_values)
|
73
|
+
.with(%w(42 43 44))
|
74
|
+
.and_return([resource_a, resource_b, resource_c])
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'returns all resources in a array' do
|
78
|
+
expect(subject).to eq [resource_a, resource_b, resource_c]
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
context 'when not all resources could be found' do
|
83
|
+
let!(:resource_a) { double('resource', code: '42') }
|
84
|
+
let!(:resource_b) { double('resource', code: '43') }
|
85
|
+
let!(:resource_c) { double('resource', code: '44') }
|
86
|
+
before do
|
87
|
+
allow(deserializer).to receive(:load_collection_from_id_values)
|
88
|
+
.with(%w(42 43 44))
|
89
|
+
.and_return([resource_b])
|
90
|
+
end
|
91
|
+
|
92
|
+
it 'raises a MissingResource error with a payload for each error' do
|
93
|
+
allow(deserializer).to receive(:id_param).and_return(:code)
|
94
|
+
allow(deserializer).to receive(:resource_class).and_return(
|
95
|
+
class_double('ActiveRecord::Base')
|
96
|
+
)
|
97
|
+
allow(deserializer).to receive(:format_collection_ids).with(
|
98
|
+
[resource_b]
|
99
|
+
).and_return([resource_b.code])
|
100
|
+
|
101
|
+
expect { subject }.to raise_error(FunWithJsonApi::Exceptions::MissingResource) do |e|
|
102
|
+
expect(e.payload.size).to eq 2
|
103
|
+
|
104
|
+
payload_a = e.payload.first
|
105
|
+
expect(payload_a.status).to eq '404'
|
106
|
+
expect(payload_a.code).to eq 'missing_resource'
|
107
|
+
expect(payload_a.title).to eq 'Unable to find the requested resource'
|
108
|
+
expect(payload_a.detail).to eq "Unable to find 'person' with matching id: '42'"
|
109
|
+
expect(payload_a.pointer).to eq '/data/0/id'
|
110
|
+
|
111
|
+
payload_b = e.payload.last
|
112
|
+
expect(payload_b.status).to eq '404'
|
113
|
+
expect(payload_b.code).to eq 'missing_resource'
|
114
|
+
expect(payload_b.title).to eq 'Unable to find the requested resource'
|
115
|
+
expect(payload_b.detail).to eq "Unable to find 'person' with matching id: '44'"
|
116
|
+
expect(payload_b.pointer).to eq '/data/2/id'
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
context 'with a document containing a resource that does not match the expected type' do
|
123
|
+
let(:document) { { data: [{ id: '42', type: 'blargh' }] } }
|
124
|
+
|
125
|
+
it 'raises a InvalidDocumentType error' do
|
126
|
+
expect { subject }.to raise_error(FunWithJsonApi::Exceptions::InvalidDocumentType) do |e|
|
127
|
+
expect(e.payload.size).to eq 1
|
128
|
+
|
129
|
+
payload = e.payload.first
|
130
|
+
expect(payload.code).to eq 'invalid_document_type'
|
131
|
+
expect(payload.pointer).to eq '/data/0/type'
|
132
|
+
expect(payload.title).to eq 'Request json_api data type does not match endpoint'
|
133
|
+
expect(payload.detail).to eq "Expected 'blargh' to be a 'person' resource"
|
134
|
+
expect(payload.status).to eq '409'
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
context 'with a document containing multiple resources that does not match' do
|
140
|
+
let(:document) do
|
141
|
+
{
|
142
|
+
data: [
|
143
|
+
{ id: '42', type: 'person' },
|
144
|
+
{ id: '43', type: 'foobar' },
|
145
|
+
{ id: '44', type: 'blargh' }
|
146
|
+
]
|
147
|
+
}
|
148
|
+
end
|
149
|
+
|
150
|
+
it 'raises a InvalidDocumentType error with a payload for each error' do
|
151
|
+
expect { subject }.to raise_error(FunWithJsonApi::Exceptions::InvalidDocumentType) do |e|
|
152
|
+
expect(e.payload.size).to eq 2
|
153
|
+
|
154
|
+
payload_a = e.payload.first
|
155
|
+
expect(payload_a.code).to eq 'invalid_document_type'
|
156
|
+
expect(payload_a.pointer).to eq '/data/1/type'
|
157
|
+
expect(payload_a.title).to eq 'Request json_api data type does not match endpoint'
|
158
|
+
expect(payload_a.detail).to eq "Expected 'foobar' to be a 'person' resource"
|
159
|
+
expect(payload_a.status).to eq '409'
|
160
|
+
|
161
|
+
payload_b = e.payload.last
|
162
|
+
expect(payload_b.code).to eq 'invalid_document_type'
|
163
|
+
expect(payload_b.pointer).to eq '/data/2/type'
|
164
|
+
expect(payload_b.title).to eq 'Request json_api data type does not match endpoint'
|
165
|
+
expect(payload_b.detail).to eq "Expected 'blargh' to be a 'person' resource"
|
166
|
+
expect(payload_b.status).to eq '409'
|
167
|
+
end
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
context 'with a document containing an empty array' do
|
172
|
+
let(:document) { { data: [] } }
|
173
|
+
|
174
|
+
it 'returns an empty array' do
|
175
|
+
expect(subject).to eq([])
|
176
|
+
end
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
context 'with a malformed document' do
|
181
|
+
it 'raises a InvalidDocument error' do
|
182
|
+
[
|
183
|
+
{ id: 'foo' },
|
184
|
+
{ data: 'string' },
|
185
|
+
{ data: { id: '42', type: 'person' } }
|
186
|
+
].each do |invalid_document|
|
187
|
+
expect do
|
188
|
+
described_class.find(invalid_document, deserializer)
|
189
|
+
end.to raise_error(FunWithJsonApi::Exceptions::InvalidDocument) do |e|
|
190
|
+
expect(e.payload.size).to eq 1
|
191
|
+
|
192
|
+
payload = e.payload.first
|
193
|
+
expect(payload.code).to eq 'invalid_document'
|
194
|
+
expect(payload.pointer).to eq '/data'
|
195
|
+
expect(payload.title).to eq 'Request json_api document is invalid'
|
196
|
+
expect(payload.detail).to eq 'Expected data to be an Array of resources'
|
197
|
+
expect(payload.status).to eq '400'
|
198
|
+
end
|
199
|
+
end
|
200
|
+
end
|
201
|
+
end
|
202
|
+
end
|
203
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe FunWithJsonApi::FindResourceFromDocument do
|
4
|
+
describe '.find' do
|
5
|
+
let(:deserializer) { instance_double('FunWithJsonApi::Deserializer') }
|
6
|
+
subject { described_class.find(document, deserializer) }
|
7
|
+
|
8
|
+
context 'with a document containing a resource' do
|
9
|
+
let(:document) { { data: { id: '42', type: 'person' } } }
|
10
|
+
|
11
|
+
context 'with a deserializer that matches the document' do
|
12
|
+
before { allow(deserializer).to receive(:type).and_return('person') }
|
13
|
+
|
14
|
+
context 'with a resource matching the document' do
|
15
|
+
let!(:resource) { double('resource') }
|
16
|
+
before do
|
17
|
+
allow(deserializer).to receive(:load_resource_from_id_value)
|
18
|
+
.with('42')
|
19
|
+
.and_return(resource)
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'returns the resource' do
|
23
|
+
expect(subject).to eq resource
|
24
|
+
end
|
25
|
+
end
|
26
|
+
context 'when a resource cannot be found' do
|
27
|
+
let!(:resource) { double('resource') }
|
28
|
+
before do
|
29
|
+
allow(deserializer).to receive(:load_resource_from_id_value)
|
30
|
+
.with('42')
|
31
|
+
.and_return(nil)
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'raises a MissingResource error' do
|
35
|
+
allow(deserializer).to receive(:id_param).and_return(:id)
|
36
|
+
|
37
|
+
expect { subject }.to raise_error(FunWithJsonApi::Exceptions::MissingResource) do |e|
|
38
|
+
expect(e.payload.size).to eq 1
|
39
|
+
|
40
|
+
payload = e.payload.first
|
41
|
+
expect(payload.status).to eq '404'
|
42
|
+
expect(payload.code).to eq 'missing_resource'
|
43
|
+
expect(payload.title).to eq 'Unable to find the requested resource'
|
44
|
+
expect(payload.detail).to eq "Unable to find 'person' with matching id: '42'"
|
45
|
+
expect(payload.pointer).to eq '/data/id'
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
context 'when the deserializer type does not match the document' do
|
52
|
+
before { allow(deserializer).to receive(:type).and_return('blargh') }
|
53
|
+
|
54
|
+
it 'raises a InvalidDocumentType error' do
|
55
|
+
expect { subject }.to raise_error(FunWithJsonApi::Exceptions::InvalidDocumentType) do |e|
|
56
|
+
expect(e.payload.size).to eq 1
|
57
|
+
|
58
|
+
payload = e.payload.first
|
59
|
+
expect(payload.code).to eq 'invalid_document_type'
|
60
|
+
expect(payload.pointer).to eq '/data/type'
|
61
|
+
expect(payload.title).to eq 'Request json_api data type does not match endpoint'
|
62
|
+
expect(payload.detail).to eq "Expected data type to be a 'blargh' resource"
|
63
|
+
expect(payload.status).to eq '409'
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
context 'with a document containing a null data atttribute' do
|
70
|
+
let(:document) { { data: nil } }
|
71
|
+
|
72
|
+
it 'returns nil' do
|
73
|
+
expect(subject).to eq(nil)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
context 'with a malformed document' do
|
78
|
+
it 'raises a InvalidDocument error' do
|
79
|
+
[
|
80
|
+
{ id: 'foo' },
|
81
|
+
{ data: 'string' },
|
82
|
+
{ data: [{ id: 'foo', type: 'bar' }] }
|
83
|
+
].each do |invalid_document|
|
84
|
+
expect do
|
85
|
+
described_class.find(invalid_document, deserializer)
|
86
|
+
end.to raise_error(FunWithJsonApi::Exceptions::InvalidDocument) do |e|
|
87
|
+
expect(e.payload.size).to eq 1
|
88
|
+
|
89
|
+
payload = e.payload.first
|
90
|
+
expect(payload.code).to eq 'invalid_document'
|
91
|
+
expect(payload.pointer).to eq '/data'
|
92
|
+
expect(payload.title).to eq 'Request json_api document is invalid'
|
93
|
+
expect(payload.detail).to eq 'Expected data to be a Hash or null'
|
94
|
+
expect(payload.status).to eq '400'
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
@@ -3,7 +3,7 @@ require 'spec_helper'
|
|
3
3
|
describe FunWithJsonApi::PreDeserializer do
|
4
4
|
describe '.parse' do
|
5
5
|
describe 'document attributes parsing' do
|
6
|
-
it '
|
6
|
+
it 'converts known attribute values into a hash' do
|
7
7
|
deserializer = Class.new(FunWithJsonApi::Deserializer) do
|
8
8
|
attribute :foo
|
9
9
|
end.create
|
@@ -16,11 +16,11 @@ describe FunWithJsonApi::PreDeserializer do
|
|
16
16
|
}
|
17
17
|
}.deep_stringify_keys!
|
18
18
|
|
19
|
-
expect(
|
19
|
+
expect(described_class.parse(document, deserializer)).to eq(
|
20
20
|
foo: 'bar'
|
21
21
|
)
|
22
22
|
end
|
23
|
-
it '
|
23
|
+
it 'handles renamed attributes' do
|
24
24
|
deserializer = Class.new(FunWithJsonApi::Deserializer) do
|
25
25
|
attribute :foo, as: :blargh
|
26
26
|
end.create
|
@@ -33,11 +33,11 @@ describe FunWithJsonApi::PreDeserializer do
|
|
33
33
|
}
|
34
34
|
}.deep_stringify_keys!
|
35
35
|
|
36
|
-
expect(
|
36
|
+
expect(described_class.parse(document, deserializer)).to eq(
|
37
37
|
blargh: 'bar'
|
38
38
|
)
|
39
39
|
end
|
40
|
-
it '
|
40
|
+
it 'only returns known attributes' do
|
41
41
|
deserializer = Class.new(FunWithJsonApi::Deserializer) do
|
42
42
|
attribute :foo
|
43
43
|
end.create
|
@@ -51,14 +51,14 @@ describe FunWithJsonApi::PreDeserializer do
|
|
51
51
|
}
|
52
52
|
}.deep_stringify_keys!
|
53
53
|
|
54
|
-
expect(
|
54
|
+
expect(described_class.parse(document, deserializer)).to eq(
|
55
55
|
foo: 'bar'
|
56
56
|
)
|
57
57
|
end
|
58
58
|
end
|
59
59
|
|
60
60
|
describe 'single relationship parsing' do
|
61
|
-
it '
|
61
|
+
it 'outputs single relationship values with a _id suffix' do
|
62
62
|
foo_deserializer_class = Class.new(FunWithJsonApi::Deserializer)
|
63
63
|
deserializer = Class.new(FunWithJsonApi::Deserializer) do
|
64
64
|
belongs_to :foo, foo_deserializer_class
|
@@ -74,11 +74,11 @@ describe FunWithJsonApi::PreDeserializer do
|
|
74
74
|
}
|
75
75
|
}.deep_stringify_keys!
|
76
76
|
|
77
|
-
expect(
|
77
|
+
expect(described_class.parse(document, deserializer)).to eq(
|
78
78
|
foo_id: '42'
|
79
79
|
)
|
80
80
|
end
|
81
|
-
it '
|
81
|
+
it 'handles renamed relationships' do
|
82
82
|
foo_deserializer_class = Class.new(FunWithJsonApi::Deserializer)
|
83
83
|
deserializer = Class.new(FunWithJsonApi::Deserializer) do
|
84
84
|
belongs_to :foo, foo_deserializer_class, as: :blargh
|
@@ -94,11 +94,11 @@ describe FunWithJsonApi::PreDeserializer do
|
|
94
94
|
}
|
95
95
|
}.deep_stringify_keys!
|
96
96
|
|
97
|
-
expect(
|
97
|
+
expect(described_class.parse(document, deserializer)).to eq(
|
98
98
|
blargh_id: '42'
|
99
99
|
)
|
100
100
|
end
|
101
|
-
it '
|
101
|
+
it 'only returns known relationships' do
|
102
102
|
foo_deserializer_class = Class.new(FunWithJsonApi::Deserializer)
|
103
103
|
deserializer = Class.new(FunWithJsonApi::Deserializer) do
|
104
104
|
belongs_to :foo, foo_deserializer_class
|
@@ -117,14 +117,14 @@ describe FunWithJsonApi::PreDeserializer do
|
|
117
117
|
}
|
118
118
|
}.deep_stringify_keys!
|
119
119
|
|
120
|
-
expect(
|
120
|
+
expect(described_class.parse(document, deserializer)).to eq(
|
121
121
|
foo_id: '42'
|
122
122
|
)
|
123
123
|
end
|
124
124
|
end
|
125
125
|
|
126
126
|
describe 'relationship collection parsing' do
|
127
|
-
it '
|
127
|
+
it 'outputs singular relationship values with a _ids suffix' do
|
128
128
|
foo_deserializer_class = Class.new(FunWithJsonApi::Deserializer)
|
129
129
|
deserializer = Class.new(FunWithJsonApi::Deserializer) do
|
130
130
|
has_many :foos, foo_deserializer_class
|
@@ -140,11 +140,11 @@ describe FunWithJsonApi::PreDeserializer do
|
|
140
140
|
}
|
141
141
|
}.deep_stringify_keys!
|
142
142
|
|
143
|
-
expect(
|
143
|
+
expect(described_class.parse(document, deserializer)).to eq(
|
144
144
|
foo_ids: %w(42 24)
|
145
145
|
)
|
146
146
|
end
|
147
|
-
it '
|
147
|
+
it 'handles renamed relationships' do
|
148
148
|
foo_deserializer_class = Class.new(FunWithJsonApi::Deserializer)
|
149
149
|
deserializer = Class.new(FunWithJsonApi::Deserializer) do
|
150
150
|
has_many :foos, foo_deserializer_class, as: :blargh
|
@@ -160,11 +160,11 @@ describe FunWithJsonApi::PreDeserializer do
|
|
160
160
|
}
|
161
161
|
}.deep_stringify_keys!
|
162
162
|
|
163
|
-
expect(
|
163
|
+
expect(described_class.parse(document, deserializer)).to eq(
|
164
164
|
blargh_ids: %w(42 11)
|
165
165
|
)
|
166
166
|
end
|
167
|
-
it '
|
167
|
+
it 'only returns known relationship collections' do
|
168
168
|
foo_deserializer_class = Class.new(FunWithJsonApi::Deserializer)
|
169
169
|
deserializer = Class.new(FunWithJsonApi::Deserializer) do
|
170
170
|
has_many :foos, foo_deserializer_class
|
@@ -183,14 +183,14 @@ describe FunWithJsonApi::PreDeserializer do
|
|
183
183
|
}
|
184
184
|
}.deep_stringify_keys!
|
185
185
|
|
186
|
-
expect(
|
186
|
+
expect(described_class.parse(document, deserializer)).to eq(
|
187
187
|
foo_ids: ['42']
|
188
188
|
)
|
189
189
|
end
|
190
190
|
end
|
191
191
|
|
192
192
|
describe 'parser exceptions' do
|
193
|
-
it '
|
193
|
+
it 'handles an invalid /data value' do
|
194
194
|
deserializer = Class.new(FunWithJsonApi::Deserializer).create
|
195
195
|
[
|
196
196
|
nil,
|
@@ -200,7 +200,7 @@ describe FunWithJsonApi::PreDeserializer do
|
|
200
200
|
}
|
201
201
|
].each do |document|
|
202
202
|
expect do
|
203
|
-
|
203
|
+
described_class.parse(document, deserializer)
|
204
204
|
end.to raise_error(FunWithJsonApi::Exceptions::InvalidDocument) do |e|
|
205
205
|
expect(e.payload.size).to eq 1
|
206
206
|
|
@@ -213,7 +213,7 @@ describe FunWithJsonApi::PreDeserializer do
|
|
213
213
|
end
|
214
214
|
end
|
215
215
|
end
|
216
|
-
it '
|
216
|
+
it 'handles an invalid /data/attributes value' do
|
217
217
|
deserializer = Class.new(FunWithJsonApi::Deserializer).create
|
218
218
|
[
|
219
219
|
{
|
@@ -221,7 +221,7 @@ describe FunWithJsonApi::PreDeserializer do
|
|
221
221
|
}
|
222
222
|
].each do |document|
|
223
223
|
expect do
|
224
|
-
|
224
|
+
described_class.parse(document, deserializer)
|
225
225
|
end.to raise_error(FunWithJsonApi::Exceptions::InvalidDocument) do |e|
|
226
226
|
expect(e.payload.size).to eq 1
|
227
227
|
|
@@ -234,7 +234,7 @@ describe FunWithJsonApi::PreDeserializer do
|
|
234
234
|
end
|
235
235
|
end
|
236
236
|
end
|
237
|
-
it '
|
237
|
+
it 'handles an invalid /data/relationships value' do
|
238
238
|
deserializer = Class.new(FunWithJsonApi::Deserializer).create
|
239
239
|
[
|
240
240
|
{
|
@@ -242,7 +242,7 @@ describe FunWithJsonApi::PreDeserializer do
|
|
242
242
|
}
|
243
243
|
].each do |document|
|
244
244
|
expect do
|
245
|
-
|
245
|
+
described_class.parse(document, deserializer)
|
246
246
|
end.to raise_error(FunWithJsonApi::Exceptions::InvalidDocument) do |e|
|
247
247
|
expect(e.payload.size).to eq 1
|
248
248
|
|
@@ -255,7 +255,7 @@ describe FunWithJsonApi::PreDeserializer do
|
|
255
255
|
end
|
256
256
|
end
|
257
257
|
end
|
258
|
-
it '
|
258
|
+
it 'handles an invalid relationship value' do
|
259
259
|
deserializer = Class.new(FunWithJsonApi::Deserializer).create
|
260
260
|
[
|
261
261
|
{
|
@@ -269,7 +269,7 @@ describe FunWithJsonApi::PreDeserializer do
|
|
269
269
|
}
|
270
270
|
].each do |document|
|
271
271
|
expect do
|
272
|
-
|
272
|
+
described_class.parse(document, deserializer)
|
273
273
|
end.to raise_error(FunWithJsonApi::Exceptions::InvalidDocument) do |e|
|
274
274
|
expect(e.payload.size).to eq 1
|
275
275
|
|