mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Require explicit adapter/serializer to render JSON API errors
- Separate collection errors from resource errors in adapter - Refactor to ErrorsSerializer; first-class json error methods - DOCS - Rails 4.0 requires assert exact exception class, boo
This commit is contained in:
@@ -8,7 +8,7 @@ module ActionController
|
||||
get :render_resource_with_errors
|
||||
|
||||
expected_errors_object =
|
||||
{ 'errors'.freeze =>
|
||||
{ :errors =>
|
||||
[
|
||||
{ :source => { :pointer => '/data/attributes/name' }, :detail => 'cannot be nil' },
|
||||
{ :source => { :pointer => '/data/attributes/name' }, :detail => 'must be longer' },
|
||||
@@ -30,7 +30,7 @@ module ActionController
|
||||
resource.errors.add(:name, 'cannot be nil')
|
||||
resource.errors.add(:name, 'must be longer')
|
||||
resource.errors.add(:id, 'must be a uuid')
|
||||
render json: resource, adapter: :json_api
|
||||
render json: resource, adapter: 'json_api/error', serializer: ActiveModel::Serializer::ErrorSerializer
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ module ActiveModelSerializers
|
||||
assert_equal serializable_resource.serializer_instance.object, @resource
|
||||
|
||||
expected_errors_object =
|
||||
{ 'errors'.freeze =>
|
||||
{ :errors =>
|
||||
[
|
||||
{
|
||||
source: { pointer: '/data/attributes/name' },
|
||||
@@ -49,7 +49,7 @@ module ActiveModelSerializers
|
||||
assert_equal serializable_resource.serializer_instance.object, @resource
|
||||
|
||||
expected_errors_object =
|
||||
{ 'errors'.freeze =>
|
||||
{ :errors =>
|
||||
[
|
||||
{ :source => { :pointer => '/data/attributes/name' }, :detail => 'cannot be nil' },
|
||||
{ :source => { :pointer => '/data/attributes/name' }, :detail => 'must be longer' },
|
||||
@@ -58,6 +58,20 @@ module ActiveModelSerializers
|
||||
}
|
||||
assert_equal serializable_resource.as_json, expected_errors_object
|
||||
end
|
||||
|
||||
# see http://jsonapi.org/examples/
|
||||
def test_parameter_source_type_error
|
||||
parameter = 'auther'
|
||||
error_source = ActiveModelSerializers::Adapter::JsonApi::Error.error_source(:parameter, parameter)
|
||||
assert_equal({ parameter: parameter }, error_source)
|
||||
end
|
||||
|
||||
def test_unknown_source_type_error
|
||||
value = 'auther'
|
||||
assert_raises(ActiveModelSerializers::Adapter::JsonApi::Error::UnknownSourceTypeError) do
|
||||
ActiveModelSerializers::Adapter::JsonApi::Error.error_source(:hyper, value)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -37,9 +37,13 @@ module ActiveModel
|
||||
options = nil
|
||||
resource = ModelWithErrors.new
|
||||
resource.errors.add(:name, 'must be awesome')
|
||||
serializable_resource = ActiveModel::SerializableResource.new(resource)
|
||||
serializable_resource = ActiveModel::SerializableResource.new(
|
||||
resource, {
|
||||
serializer: ActiveModel::Serializer::ErrorSerializer,
|
||||
adapter: 'json_api/error'
|
||||
})
|
||||
expected_response_document =
|
||||
{ 'errors'.freeze =>
|
||||
{ :errors =>
|
||||
[
|
||||
{ :source => { :pointer => '/data/attributes/name' }, :detail => 'must be awesome' }
|
||||
]
|
||||
@@ -53,9 +57,14 @@ module ActiveModel
|
||||
resources << resource = ModelWithErrors.new
|
||||
resource.errors.add(:title, 'must be amazing')
|
||||
resources << ModelWithErrors.new
|
||||
serializable_resource = ActiveModel::SerializableResource.new(resources)
|
||||
serializable_resource = ActiveModel::SerializableResource.new(
|
||||
resources, {
|
||||
serializer: ActiveModel::Serializer::ErrorsSerializer,
|
||||
each_serializer: ActiveModel::Serializer::ErrorSerializer,
|
||||
adapter: 'json_api/error'
|
||||
})
|
||||
expected_response_document =
|
||||
{ 'errors'.freeze =>
|
||||
{ :errors =>
|
||||
[
|
||||
{ :source => { :pointer => '/data/attributes/title' }, :detail => 'must be amazing' }
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user