forked from rails-api/active_model_serializers
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
271 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
module ActiveModel | ||
class Serializer | ||
module Adapter | ||
class JsonApi < Base | ||
class Error < Base | ||
def serializable_hash(*) | ||
@result = [] | ||
# TECHDEBT: clean up single vs. collection of resources | ||
if serializer.object.respond_to?(:each) | ||
@result = collection_errors.flat_map do |collection_error| | ||
collection_error.flat_map do |attribute_name, attribute_errors| | ||
attribute_error_objects(attribute_name, attribute_errors) | ||
end | ||
end | ||
else | ||
@result = object_errors.flat_map do |attribute_name, attribute_errors| | ||
attribute_error_objects(attribute_name, attribute_errors) | ||
end | ||
end | ||
{ root => @result } | ||
end | ||
|
||
def fragment_cache(cached_hash, non_cached_hash) | ||
JsonApi::FragmentCache.new.fragment_cache(root, cached_hash, non_cached_hash) | ||
end | ||
|
||
def root | ||
'errors'.freeze | ||
end | ||
|
||
private | ||
|
||
# @return [Array<symbol, Array<String>] i.e. attribute_name, [attribute_errors] | ||
def object_errors | ||
cache_check(serializer) do | ||
serializer.object.errors.messages | ||
end | ||
end | ||
|
||
def collection_errors | ||
cache_check(serializer) do | ||
serializer.object.flat_map do |elem| | ||
elem.errors.messages | ||
end | ||
end | ||
end | ||
|
||
def attribute_error_objects(attribute_name, attribute_errors) | ||
attribute_errors.map do |attribute_error| | ||
{ | ||
source: { pointer: "data/attributes/#{attribute_name}" }, | ||
detail: attribute_error | ||
} | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
class ActiveModel::Serializer::ErrorSerializer < ActiveModel::Serializer | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
require 'test_helper' | ||
|
||
=begin | ||
## http://jsonapi.org/format/#document-top-level | ||
A document MUST contain at least one of the following top-level members: | ||
- data: the document's "primary data" | ||
- errors: an array of error objects | ||
- meta: a meta object that contains non-standard meta-information. | ||
The members data and errors MUST NOT coexist in the same document. | ||
## http://jsonapi.org/format/#error-objects | ||
Error objects provide additional information about problems encountered while performing an operation. Error objects MUST be returned as an array keyed by errors in the top level of a JSON API document. | ||
An error object MAY have the following members: | ||
- id: a unique identifier for this particular occurrence of the problem. | ||
- links: a links object containing the following members: | ||
- about: a link that leads to further details about this particular occurrence of the problem. | ||
- status: the HTTP status code applicable to this problem, expressed as a string value. | ||
- code: an application-specific error code, expressed as a string value. | ||
- title: a short, human-readable summary of the problem that SHOULD NOT change from occurrence to occurrence of the problem, except for purposes of localization. | ||
- detail: a human-readable explanation specific to this occurrence of the problem. | ||
- source: an object containing references to the source of the error, optionally including any of the following members: | ||
- pointer: a JSON Pointer [RFC6901] to the associated entity in the request document [e.g. "/data" for a primary data object, or "/data/attributes/title" for a specific attribute]. | ||
- parameter: a string indicating which query parameter caused the error. | ||
- meta: a meta object containing non-standard meta-information about the error. | ||
=end | ||
|
||
module ActiveModel | ||
class Serializer | ||
module Adapter | ||
class JsonApi < Base | ||
class ErrorsTest < Minitest::Test | ||
include ActiveModel::Serializer::Lint::Tests | ||
|
||
def setup | ||
@resource = ModelWithErrors.new | ||
end | ||
|
||
def test_active_model_with_error | ||
options = { | ||
serializer: ActiveModel::Serializer::ErrorSerializer, | ||
adapter: :'json_api/error' | ||
} | ||
|
||
@resource.errors.add(:name, 'cannot be nil') | ||
|
||
serializable_resource = ActiveModel::SerializableResource.new(@resource, options) | ||
assert_equal serializable_resource.serializer_instance.attributes, {} | ||
assert_equal serializable_resource.serializer_instance.object, @resource | ||
|
||
expected_errors_object = | ||
{ 'errors'.freeze => | ||
[ | ||
{ | ||
source: { pointer: 'data/attributes/name' }, | ||
detail: 'cannot be nil' | ||
} | ||
] | ||
} | ||
assert_equal serializable_resource.as_json, expected_errors_object | ||
end | ||
|
||
def test_active_model_with_multiple_errors | ||
options = { | ||
serializer: ActiveModel::Serializer::ErrorSerializer, | ||
adapter: :'json_api/error' | ||
} | ||
|
||
@resource.errors.add(:name, 'cannot be nil') | ||
@resource.errors.add(:name, 'must be longer') | ||
@resource.errors.add(:id, 'must be a uuid') | ||
|
||
serializable_resource = ActiveModel::SerializableResource.new(@resource, options) | ||
assert_equal serializable_resource.serializer_instance.attributes, {} | ||
assert_equal serializable_resource.serializer_instance.object, @resource | ||
|
||
expected_errors_object = | ||
{ 'errors'.freeze => | ||
[ | ||
{ :source => { :pointer => 'data/attributes/name' }, :detail => 'cannot be nil' }, | ||
{ :source => { :pointer => 'data/attributes/name' }, :detail => 'must be longer' }, | ||
{ :source => { :pointer => 'data/attributes/id' }, :detail => 'must be a uuid' } | ||
] | ||
} | ||
assert_equal serializable_resource.as_json, expected_errors_object | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters