jsonapi-resources 0.3.0 → 0.3.1
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/README.md +1 -1
- data/jsonapi-resources.gemspec +1 -1
- data/lib/jsonapi/request.rb +3 -10
- data/lib/jsonapi/resource.rb +1 -1
- data/lib/jsonapi/resource_serializer.rb +2 -0
- data/lib/jsonapi/resources/version.rb +1 -1
- data/test/controllers/controller_test.rb +57 -0
- data/test/fixtures/posts.yml +7 -1
- data/test/integration/requests/request_test.rb +36 -1
- data/test/test_helper.rb +1 -0
- data/test/unit/serializer/serializer_test.rb +9 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c58427d88b5c0bbe33915d8039ac02a1f6ea260b
|
4
|
+
data.tar.gz: 62dcacd2190abc5bffe3a10cefe1bba9d50a56b7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5ae8e6c9e4c87a8b5848110cd2acd98f98b6a1ab202edd83b62aabd9806df7880d4345a1678b551ed054706aaae14227261f07f9dce77161e3dbf260dcf1236c
|
7
|
+
data.tar.gz: 1d528f86653c12a040dbb52b1db0d0f8affbf7b11440ceb0a75e3eae17d70a219872cbbb34d7fcb7ad3d04ccd366921ba146f00023a7b0910a4e74ae5f40c86a
|
data/README.md
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
|
5
5
|
Like JSON API itself, JR's design is focused on the resources served by an API. JR needs little more than a definition of your resources, including their attributes and relationships, to make your server compliant with JSON API.
|
6
6
|
|
7
|
-
JR is designed to work with Rails
|
7
|
+
JR is designed to work with Rails 4.0+, and provides custom routes, controllers, and serializers. JR's resources may be backed by ActiveRecord models or by custom objects.
|
8
8
|
|
9
9
|
## Demo App
|
10
10
|
|
data/jsonapi-resources.gemspec
CHANGED
@@ -25,5 +25,5 @@ Gem::Specification.new do |spec|
|
|
25
25
|
spec.add_development_dependency 'minitest-spec-rails'
|
26
26
|
spec.add_development_dependency 'minitest-reporters'
|
27
27
|
spec.add_development_dependency 'simplecov'
|
28
|
-
spec.
|
28
|
+
spec.add_dependency 'rails', '>= 4.0'
|
29
29
|
end
|
data/lib/jsonapi/request.rb
CHANGED
@@ -198,16 +198,9 @@ module JSONAPI
|
|
198
198
|
end
|
199
199
|
|
200
200
|
def parse_add_operation(data)
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
parse_params(verify_and_remove_type(p),
|
205
|
-
@resource_klass.createable_fields(@context)))
|
206
|
-
end
|
207
|
-
else
|
208
|
-
@operations.push JSONAPI::CreateResourceOperation.new(@resource_klass,
|
209
|
-
parse_params(verify_and_remove_type(data),
|
210
|
-
@resource_klass.createable_fields(@context)))
|
201
|
+
Array.wrap(data).each do |p|
|
202
|
+
values = parse_params(verify_and_remove_type(p), @resource_klass.createable_fields(@context))
|
203
|
+
@operations.push JSONAPI::CreateResourceOperation.new(@resource_klass, values)
|
211
204
|
end
|
212
205
|
rescue JSONAPI::Exceptions::Error => e
|
213
206
|
@errors.concat(e.errors)
|
data/lib/jsonapi/resource.rb
CHANGED
@@ -110,6 +110,8 @@ module JSONAPI
|
|
110
110
|
add_included_object(@primary_class_name, id, object_hash(resource, requested_associations), true)
|
111
111
|
end
|
112
112
|
else
|
113
|
+
return {} if source.nil?
|
114
|
+
|
113
115
|
resource = source
|
114
116
|
id = resource.id
|
115
117
|
# ToDo: See if this is actually needed
|
@@ -1335,6 +1335,19 @@ class PostsControllerTest < ActionController::TestCase
|
|
1335
1335
|
assert_response :bad_request
|
1336
1336
|
assert_match /2,1 is not a valid value for id/, response.body
|
1337
1337
|
end
|
1338
|
+
|
1339
|
+
def test_show_has_one_relationship_nil
|
1340
|
+
get :show_association, {post_id: '17', association: 'author'}
|
1341
|
+
assert_response :success
|
1342
|
+
assert_hash_equals json_response,
|
1343
|
+
{
|
1344
|
+
data: nil,
|
1345
|
+
links: {
|
1346
|
+
self: 'http://test.host/posts/17/links/author',
|
1347
|
+
related: 'http://test.host/posts/17/author'
|
1348
|
+
}
|
1349
|
+
}
|
1350
|
+
end
|
1338
1351
|
end
|
1339
1352
|
|
1340
1353
|
class TagsControllerTest < ActionController::TestCase
|
@@ -1678,6 +1691,50 @@ class PeopleControllerTest < ActionController::TestCase
|
|
1678
1691
|
assert_equal json_response['data'][0]['id'], '1'
|
1679
1692
|
assert_equal json_response['data'][0]['name'], 'Joe Author'
|
1680
1693
|
end
|
1694
|
+
|
1695
|
+
def test_get_related_resource
|
1696
|
+
get :get_related_resource, {post_id: '2', association: 'author', :source=>'posts'}
|
1697
|
+
assert_response :success
|
1698
|
+
assert_hash_equals json_response,
|
1699
|
+
{
|
1700
|
+
data: {
|
1701
|
+
id: '1',
|
1702
|
+
type: 'people',
|
1703
|
+
name: 'Joe Author',
|
1704
|
+
email: 'joe@xyz.fake',
|
1705
|
+
dateJoined: '2013-08-07 16:25:00 -0400',
|
1706
|
+
links: {
|
1707
|
+
self: 'http://test.host/people/1',
|
1708
|
+
comments: {
|
1709
|
+
self: 'http://test.host/people/1/links/comments',
|
1710
|
+
related: 'http://test.host/people/1/comments'
|
1711
|
+
},
|
1712
|
+
posts: {
|
1713
|
+
self: 'http://test.host/people/1/links/posts',
|
1714
|
+
related: 'http://test.host/people/1/posts'
|
1715
|
+
},
|
1716
|
+
preferences: {
|
1717
|
+
self: 'http://test.host/people/1/links/preferences',
|
1718
|
+
related: 'http://test.host/people/1/preferences',
|
1719
|
+
linkage: {
|
1720
|
+
type: 'preferences',
|
1721
|
+
id: '1'
|
1722
|
+
}
|
1723
|
+
}
|
1724
|
+
}
|
1725
|
+
}
|
1726
|
+
}
|
1727
|
+
end
|
1728
|
+
|
1729
|
+
def test_get_related_resource_nil
|
1730
|
+
get :get_related_resource, {post_id: '17', association: 'author', :source=>'posts'}
|
1731
|
+
assert_response :success
|
1732
|
+
assert_hash_equals json_response,
|
1733
|
+
{
|
1734
|
+
data: nil
|
1735
|
+
}
|
1736
|
+
|
1737
|
+
end
|
1681
1738
|
end
|
1682
1739
|
|
1683
1740
|
class Api::V5::AuthorsControllerTest < ActionController::TestCase
|
data/test/fixtures/posts.yml
CHANGED
@@ -156,6 +156,41 @@ class RequestTest < ActionDispatch::IntegrationTest
|
|
156
156
|
assert_equal 201, status
|
157
157
|
end
|
158
158
|
|
159
|
+
def test_post_single_missing_data_contents
|
160
|
+
post '/posts',
|
161
|
+
{
|
162
|
+
'data' => {
|
163
|
+
}
|
164
|
+
}.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE
|
165
|
+
|
166
|
+
assert_equal 400, status
|
167
|
+
end
|
168
|
+
|
169
|
+
def test_post_single_minimal_valid
|
170
|
+
post '/comments',
|
171
|
+
{
|
172
|
+
'data' => {
|
173
|
+
'type' => 'comments'
|
174
|
+
}
|
175
|
+
}.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE
|
176
|
+
|
177
|
+
assert_equal 201, status
|
178
|
+
assert_nil json_response['data']['body']
|
179
|
+
assert_nil json_response['data']['links']['post']['linkage']
|
180
|
+
assert_nil json_response['data']['links']['author']['linkage']
|
181
|
+
end
|
182
|
+
|
183
|
+
def test_post_single_minimal_invalid
|
184
|
+
post '/posts',
|
185
|
+
{
|
186
|
+
'data' => {
|
187
|
+
'type' => 'posts'
|
188
|
+
}
|
189
|
+
}.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE
|
190
|
+
|
191
|
+
assert_equal 422, status
|
192
|
+
end
|
193
|
+
|
159
194
|
def test_update_association_without_content_type
|
160
195
|
ruby = Section.find_by(name: 'ruby')
|
161
196
|
patch '/posts/3/links/section', { 'data' => {type: 'sections', id: ruby.id.to_s }}.to_json
|
@@ -189,7 +224,7 @@ class RequestTest < ActionDispatch::IntegrationTest
|
|
189
224
|
def test_post_update_association_has_many
|
190
225
|
rogue = Comment.find_by(body: 'Rogue Comment Here')
|
191
226
|
post '/posts/5/links/comments', { 'data' => [{type: 'comments', id: rogue.id.to_s }]}.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE
|
192
|
-
|
227
|
+
|
193
228
|
assert_equal 204, status
|
194
229
|
end
|
195
230
|
|
data/test/test_helper.rb
CHANGED
@@ -57,6 +57,15 @@ class SerializerTest < ActionDispatch::IntegrationTest
|
|
57
57
|
)
|
58
58
|
end
|
59
59
|
|
60
|
+
def test_serializer_nil_handling
|
61
|
+
assert_hash_equals(
|
62
|
+
{
|
63
|
+
data: nil
|
64
|
+
},
|
65
|
+
JSONAPI::ResourceSerializer.new(PostResource).serialize_to_hash(nil)
|
66
|
+
)
|
67
|
+
end
|
68
|
+
|
60
69
|
def test_serializer_namespaced_resource
|
61
70
|
assert_hash_equals(
|
62
71
|
{
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonapi-resources
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dan Gebhardt
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-04-
|
12
|
+
date: 2015-04-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -102,7 +102,7 @@ dependencies:
|
|
102
102
|
- - ">="
|
103
103
|
- !ruby/object:Gem::Version
|
104
104
|
version: '4.0'
|
105
|
-
type: :
|
105
|
+
type: :runtime
|
106
106
|
prerelease: false
|
107
107
|
version_requirements: !ruby/object:Gem::Requirement
|
108
108
|
requirements:
|