jsonapi-serializers 0.9.0 → 0.10.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +25 -24
- data/jsonapi-serializers.gemspec +1 -0
- data/lib/jsonapi-serializers/serializer.rb +25 -2
- data/lib/jsonapi-serializers/version.rb +1 -1
- data/spec/serializer_spec.rb +64 -15
- metadata +18 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ccad99bd078e37fa43bab5ffd42d9fae85414c33
|
4
|
+
data.tar.gz: c4d9d97a4550a955c7d9f95c9b59e70cc60896f2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6eb60b10f45d7892c4828b125b5a0efc139f41e1c0f737a48bcb4ffedea7162e78e5189d05e916ade934a197dec7f400b1a844c6f34bf5c1647896cee2098657
|
7
|
+
data.tar.gz: f3a8ec61f5c47a1ee73760865bf7dfc6903c77d12b7aa587753ee4c4626556f3e81697455f60a9118a14002ed4978a21129e7138b86fc118d0b9e2af58aabaed
|
data/README.md
CHANGED
@@ -325,6 +325,27 @@ errors = [{ "title": "Invalid Attribute", "detail": "First name must contain at
|
|
325
325
|
JSONAPI::Serializer.serialize_errors(errors)
|
326
326
|
```
|
327
327
|
|
328
|
+
If you are using Rails models (ActiveModel by default), you can pass in an object's errors:
|
329
|
+
|
330
|
+
```ruby
|
331
|
+
JSONAPI::Serializer.serialize_errors(user.errors)
|
332
|
+
```
|
333
|
+
|
334
|
+
A more complete usage example (assumes ActiveModel):
|
335
|
+
|
336
|
+
```
|
337
|
+
class Api::V1::ReposController < Api::V1::BaseController
|
338
|
+
def create
|
339
|
+
post = Post.create(post_params)
|
340
|
+
if post.errors
|
341
|
+
render json: JSONAPI::Serializer.serialize_errors(post.errors)
|
342
|
+
else
|
343
|
+
render json: JSONAPI::Serializer.serialize(post)
|
344
|
+
end
|
345
|
+
end
|
346
|
+
end
|
347
|
+
```
|
348
|
+
|
328
349
|
### Explicit serializer discovery
|
329
350
|
|
330
351
|
By default, jsonapi-serializers assumes that the serializer class for `Namespace::User` is `Namespace::UserSerializer`. You can override this behavior on a per-object basis by implementing the `jsonapi_serializer_class_name` method.
|
@@ -654,35 +675,15 @@ module Api
|
|
654
675
|
end
|
655
676
|
```
|
656
677
|
|
678
|
+
## Changelog
|
679
|
+
|
680
|
+
See [Releases](https://github.com/fotinakis/jsonapi-serializers/releases).
|
681
|
+
|
657
682
|
## Unfinished business
|
658
683
|
|
659
|
-
* Support for passing `context` through to serializers is partially complete, but needs more work.
|
660
684
|
* Support for the `fields` spec is planned, would love a PR contribution for this.
|
661
685
|
* Support for pagination/sorting is unlikely to be supported because it would likely involve coupling to ActiveRecord, but please open an issue if you have ideas of how to support this generically.
|
662
686
|
|
663
|
-
## Release notes
|
664
|
-
|
665
|
-
* v0.9.0: Add initial `serialize_errors` support.
|
666
|
-
* v0.8.0: Pass context through recursive relationships.
|
667
|
-
* v0.7.0: Support for root error objects.
|
668
|
-
* v0.6.5: Exclude attributes when empty.
|
669
|
-
* v0.6.4: Fix regression of including links when empty.
|
670
|
-
* v0.6.3: Fix support for underscore-formatted attribute names.
|
671
|
-
* v0.6.2: Internal style updates and performance fixes.
|
672
|
-
* v0.6.1: Spec compliance fix, include intermediate resources on compound documents.
|
673
|
-
* v0.6.0: Support for polymorphic collections and inheriting serializer attributes.
|
674
|
-
* v0.5.0: Support for explicit serializer discovery.
|
675
|
-
* v0.4.0: Support for declaring multiple `attributes`.
|
676
|
-
* v0.3.1: Improve performance of loading included relationships.
|
677
|
-
* v0.3.0: Add top-level `meta` support.
|
678
|
-
* v0.2.6: Add `base_url` support.
|
679
|
-
* v0.2.5: Allow disabling ambiguous collection checks for Sequel support.
|
680
|
-
* v0.2.4: Improve handling for nil relationship links.
|
681
|
-
* v0.2.3: Support serializers with no attributes.
|
682
|
-
* v0.2.2: Compliance fix for excluding empty relationship objects.
|
683
|
-
* v0.2.1: Compliance fix for self links.
|
684
|
-
* v0.2.0: Initial release with support for the final v1 JSON API spec.
|
685
|
-
|
686
687
|
## Contributing
|
687
688
|
|
688
689
|
1. Fork it ( https://github.com/fotinakis/jsonapi-serializers/fork )
|
data/jsonapi-serializers.gemspec
CHANGED
@@ -316,8 +316,31 @@ module JSONAPI
|
|
316
316
|
result
|
317
317
|
end
|
318
318
|
|
319
|
-
def self.serialize_errors(
|
320
|
-
|
319
|
+
def self.serialize_errors(raw_errors)
|
320
|
+
if is_activemodel_errors?(raw_errors)
|
321
|
+
{'errors' => activemodel_errors(raw_errors)}
|
322
|
+
else
|
323
|
+
{'errors' => raw_errors}
|
324
|
+
end
|
325
|
+
end
|
326
|
+
|
327
|
+
def self.activemodel_errors(raw_errors)
|
328
|
+
raw_errors.to_hash(full_messages: true).inject([]) do |result, (attribute, messages)|
|
329
|
+
result += messages.map { |message| single_error(attribute.to_s, message) }
|
330
|
+
end
|
331
|
+
end
|
332
|
+
|
333
|
+
def self.is_activemodel_errors?(raw_errors)
|
334
|
+
raw_errors.respond_to?(:to_hash) && raw_errors.respond_to?(:full_messages)
|
335
|
+
end
|
336
|
+
|
337
|
+
def self.single_error(attribute, message)
|
338
|
+
{
|
339
|
+
'source' => {
|
340
|
+
'pointer' => "/data/attributes/#{attribute.dasherize}"
|
341
|
+
},
|
342
|
+
'detail' => message
|
343
|
+
}
|
321
344
|
end
|
322
345
|
|
323
346
|
def self.serialize_primary(object, options = {})
|
data/spec/serializer_spec.rb
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
require 'active_model/errors'
|
2
|
+
require 'active_model/naming'
|
3
|
+
require 'active_model/translation'
|
4
|
+
|
1
5
|
describe JSONAPI::Serializer do
|
2
6
|
def serialize_primary(object, options = {})
|
3
7
|
# Note: intentional high-coupling to protected method for tests.
|
@@ -350,6 +354,65 @@ describe JSONAPI::Serializer do
|
|
350
354
|
end
|
351
355
|
end
|
352
356
|
|
357
|
+
# The members data and errors MUST NOT coexist in the same document.
|
358
|
+
describe 'JSONAPI::Serializer.serialize_errors' do
|
359
|
+
it 'can include a top level errors node' do
|
360
|
+
errors = [
|
361
|
+
{
|
362
|
+
'source' => { 'pointer' => '/data/attributes/first-name' },
|
363
|
+
'title' => 'Invalid Attribute',
|
364
|
+
'detail' => 'First name must contain at least three characters.'
|
365
|
+
},
|
366
|
+
{
|
367
|
+
'source' => { 'pointer' => '/data/attributes/first-name' },
|
368
|
+
'title' => 'Invalid Attribute',
|
369
|
+
'detail' => 'First name must contain an emoji.'
|
370
|
+
}
|
371
|
+
]
|
372
|
+
expect(JSONAPI::Serializer.serialize_errors(errors)).to eq({'errors' => errors})
|
373
|
+
end
|
374
|
+
|
375
|
+
it 'works for active_record' do
|
376
|
+
# DummyUser exists so we can test calling user.errors.to_hash(full_messages: true)
|
377
|
+
class DummyUser
|
378
|
+
extend ActiveModel::Naming
|
379
|
+
extend ActiveModel::Translation
|
380
|
+
|
381
|
+
def initialize
|
382
|
+
@errors = ActiveModel::Errors.new(self)
|
383
|
+
@errors.add(:email, :invalid, message: 'is invalid')
|
384
|
+
@errors.add(:email, :blank, message: "can't be blank")
|
385
|
+
@errors.add(:first_name, :blank, message: "can't be blank")
|
386
|
+
end
|
387
|
+
|
388
|
+
attr_accessor :first_name, :email
|
389
|
+
attr_reader :errors
|
390
|
+
|
391
|
+
def read_attribute_for_validation(attr)
|
392
|
+
send(attr)
|
393
|
+
end
|
394
|
+
end
|
395
|
+
user = DummyUser.new
|
396
|
+
jsonapi_errors = [
|
397
|
+
{
|
398
|
+
'source' => { 'pointer' => '/data/attributes/email' },
|
399
|
+
'detail' => 'Email is invalid'
|
400
|
+
},
|
401
|
+
{
|
402
|
+
'source' => { 'pointer' => '/data/attributes/email' },
|
403
|
+
'detail' => "Email can't be blank"
|
404
|
+
},
|
405
|
+
{
|
406
|
+
'source' => { 'pointer' => '/data/attributes/first-name' },
|
407
|
+
'detail' => "First name can't be blank"
|
408
|
+
}
|
409
|
+
]
|
410
|
+
expect(JSONAPI::Serializer.serialize_errors(user.errors)).to eq({
|
411
|
+
'errors' => jsonapi_errors,
|
412
|
+
})
|
413
|
+
end
|
414
|
+
end
|
415
|
+
|
353
416
|
describe 'JSONAPI::Serializer.serialize' do
|
354
417
|
# The following tests rely on the fact that serialize_primary has been tested above, so object
|
355
418
|
# primary data is not explicitly tested here. If things are broken, look above here first.
|
@@ -381,6 +444,7 @@ describe JSONAPI::Serializer do
|
|
381
444
|
'data' => serialize_primary(post, {serializer: MyApp::PostSerializer}),
|
382
445
|
})
|
383
446
|
end
|
447
|
+
# TODO: remove this code on next major release
|
384
448
|
it 'can include a top level errors node - deprecated' do
|
385
449
|
post = create(:post)
|
386
450
|
errors = [
|
@@ -400,21 +464,6 @@ describe JSONAPI::Serializer do
|
|
400
464
|
'data' => serialize_primary(post, {serializer: MyApp::PostSerializer}),
|
401
465
|
})
|
402
466
|
end
|
403
|
-
it 'can include a top level errors node' do
|
404
|
-
errors = [
|
405
|
-
{
|
406
|
-
"source" => { "pointer" => "/data/attributes/first-name" },
|
407
|
-
"title" => "Invalid Attribute",
|
408
|
-
"detail" => "First name must contain at least three characters."
|
409
|
-
},
|
410
|
-
{
|
411
|
-
"source" => { "pointer" => "/data/attributes/first-name" },
|
412
|
-
"title" => "Invalid Attribute",
|
413
|
-
"detail" => "First name must contain an emoji."
|
414
|
-
}
|
415
|
-
]
|
416
|
-
expect(JSONAPI::Serializer.serialize_errors(errors)).to eq({'errors' => errors})
|
417
|
-
end
|
418
467
|
it 'can serialize a single object with an `each` method by passing skip_collection_check: true' do
|
419
468
|
post = create(:post)
|
420
469
|
post.define_singleton_method(:each) do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonapi-serializers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.10.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mike Fotinakis
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-04-
|
11
|
+
date: 2016-04-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -80,6 +80,20 @@ dependencies:
|
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '4.5'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: activemodel
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '4.2'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '4.2'
|
83
97
|
description: ''
|
84
98
|
email:
|
85
99
|
- mike@fotinakis.com
|
@@ -123,7 +137,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
123
137
|
version: '0'
|
124
138
|
requirements: []
|
125
139
|
rubyforge_project:
|
126
|
-
rubygems_version: 2.
|
140
|
+
rubygems_version: 2.4.5
|
127
141
|
signing_key:
|
128
142
|
specification_version: 4
|
129
143
|
summary: Pure Ruby readonly serializers for the JSON:API spec.
|
@@ -132,3 +146,4 @@ test_files:
|
|
132
146
|
- spec/spec_helper.rb
|
133
147
|
- spec/support/factory.rb
|
134
148
|
- spec/support/serializers.rb
|
149
|
+
has_rdoc:
|