mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 14:56:50 +00:00
Assert Schema (#1677)
* Assert Schema * Fix regression from #1695 where JSONAPI renders empty meta * Add changelog
This commit is contained in:
parent
9f59398f2b
commit
6c321cd862
@ -6,6 +6,7 @@ Breaking changes:
|
|||||||
- [#1662](https://github.com/rails-api/active_model_serializers/pull/1662) Drop support for Rails 4.0 and Ruby 2.0.0. (@remear)
|
- [#1662](https://github.com/rails-api/active_model_serializers/pull/1662) Drop support for Rails 4.0 and Ruby 2.0.0. (@remear)
|
||||||
|
|
||||||
Features:
|
Features:
|
||||||
|
- [#1677](https://github.com/rails-api/active_model_serializers/pull/1677) Add `assert_schema`, `assert_request_schema`, `assert_request_response_schema`. (@bf4)
|
||||||
- [#1697](https://github.com/rails-api/active_model_serializers/pull/1697) Include actual exception message with custom exceptions;
|
- [#1697](https://github.com/rails-api/active_model_serializers/pull/1697) Include actual exception message with custom exceptions;
|
||||||
`Test::Schema` exceptions are now `Minitest::Assertion`s. (@bf4)
|
`Test::Schema` exceptions are now `Minitest::Assertion`s. (@bf4)
|
||||||
- [#1699](https://github.com/rails-api/active_model_serializers/pull/1699) String/Lambda support for conditional attributes/associations (@mtsmfm)
|
- [#1699](https://github.com/rails-api/active_model_serializers/pull/1699) String/Lambda support for conditional attributes/associations (@mtsmfm)
|
||||||
|
|||||||
@ -131,7 +131,7 @@ module ActiveModelSerializers
|
|||||||
hash[:links].update(pagination_links_for(serializer))
|
hash[:links].update(pagination_links_for(serializer))
|
||||||
end
|
end
|
||||||
|
|
||||||
hash[:meta] = instance_options[:meta] if instance_options[:meta].is_a?(Hash)
|
hash[:meta] = instance_options[:meta] unless instance_options[:meta].blank?
|
||||||
|
|
||||||
hash
|
hash
|
||||||
end
|
end
|
||||||
|
|||||||
@ -10,19 +10,38 @@ module ActiveModelSerializers
|
|||||||
# get :index
|
# get :index
|
||||||
# assert_response_schema
|
# assert_response_schema
|
||||||
def assert_response_schema(schema_path = nil, message = nil)
|
def assert_response_schema(schema_path = nil, message = nil)
|
||||||
matcher = AssertResponseSchema.new(schema_path, response, message)
|
matcher = AssertResponseSchema.new(schema_path, request, response, message)
|
||||||
|
assert(matcher.call, matcher.message)
|
||||||
|
end
|
||||||
|
|
||||||
|
def assert_request_schema(schema_path = nil, message = nil)
|
||||||
|
matcher = AssertRequestSchema.new(schema_path, request, response, message)
|
||||||
|
assert(matcher.call, matcher.message)
|
||||||
|
end
|
||||||
|
|
||||||
|
# May be renamed
|
||||||
|
def assert_request_response_schema(schema_path = nil, message = nil)
|
||||||
|
assert_request_schema(schema_path, message)
|
||||||
|
assert_response_schema(schema_path, message)
|
||||||
|
end
|
||||||
|
|
||||||
|
def assert_schema(payload, schema_path = nil, message = nil)
|
||||||
|
matcher = AssertSchema.new(schema_path, request, response, message, payload)
|
||||||
assert(matcher.call, matcher.message)
|
assert(matcher.call, matcher.message)
|
||||||
end
|
end
|
||||||
|
|
||||||
MissingSchema = Class.new(Minitest::Assertion)
|
MissingSchema = Class.new(Minitest::Assertion)
|
||||||
InvalidSchemaError = Class.new(Minitest::Assertion)
|
InvalidSchemaError = Class.new(Minitest::Assertion)
|
||||||
|
|
||||||
class AssertResponseSchema
|
class AssertSchema
|
||||||
attr_reader :schema_path, :response, :message
|
attr_reader :schema_path, :request, :response, :message, :payload
|
||||||
|
|
||||||
def initialize(schema_path, response, message)
|
# Interface may change.
|
||||||
|
def initialize(schema_path, request, response, message, payload = nil)
|
||||||
require_json_schema!
|
require_json_schema!
|
||||||
|
@request = request
|
||||||
@response = response
|
@response = response
|
||||||
|
@payload = payload
|
||||||
@schema_path = schema_path || schema_path_default
|
@schema_path = schema_path || schema_path_default
|
||||||
@message = message
|
@message = message
|
||||||
@document_store = JsonSchema::DocumentStore.new
|
@document_store = JsonSchema::DocumentStore.new
|
||||||
@ -41,11 +60,11 @@ module ActiveModelSerializers
|
|||||||
attr_reader :document_store
|
attr_reader :document_store
|
||||||
|
|
||||||
def controller_path
|
def controller_path
|
||||||
response.request.filtered_parameters[:controller]
|
request.filtered_parameters[:controller]
|
||||||
end
|
end
|
||||||
|
|
||||||
def action
|
def action
|
||||||
response.request.filtered_parameters[:action]
|
request.filtered_parameters[:action]
|
||||||
end
|
end
|
||||||
|
|
||||||
def schema_directory
|
def schema_directory
|
||||||
@ -68,6 +87,10 @@ module ActiveModelSerializers
|
|||||||
load_json(response.body)
|
load_json(response.body)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def request_params
|
||||||
|
request.env['action_dispatch.request.request_parameters']
|
||||||
|
end
|
||||||
|
|
||||||
def json_schema
|
def json_schema
|
||||||
@json_schema ||= JsonSchema.parse!(schema_data)
|
@json_schema ||= JsonSchema.parse!(schema_data)
|
||||||
end
|
end
|
||||||
@ -98,6 +121,18 @@ module ActiveModelSerializers
|
|||||||
raise LoadError, "You don't have json_schema installed in your application. Please add it to your Gemfile and run bundle install"
|
raise LoadError, "You don't have json_schema installed in your application. Please add it to your Gemfile and run bundle install"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
class AssertResponseSchema < AssertSchema
|
||||||
|
def initialize(*)
|
||||||
|
super
|
||||||
|
@payload = response_body
|
||||||
|
end
|
||||||
|
end
|
||||||
|
class AssertRequestSchema < AssertSchema
|
||||||
|
def initialize(*)
|
||||||
|
super
|
||||||
|
@payload = request_params
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -110,7 +110,7 @@ module ActiveModel
|
|||||||
assert_equal(expected, actual)
|
assert_equal(expected, actual)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_meta_key_is_present_when_empty_hash_with_json_api
|
def test_meta_key_is_not_present_when_empty_hash_with_json_api
|
||||||
actual = ActiveModelSerializers::SerializableResource.new(
|
actual = ActiveModelSerializers::SerializableResource.new(
|
||||||
@blog,
|
@blog,
|
||||||
adapter: :json_api,
|
adapter: :json_api,
|
||||||
@ -122,8 +122,7 @@ module ActiveModel
|
|||||||
id: '1',
|
id: '1',
|
||||||
type: 'blogs',
|
type: 'blogs',
|
||||||
attributes: { title: 'AMS Hints' }
|
attributes: { title: 'AMS Hints' }
|
||||||
},
|
}
|
||||||
meta: {}
|
|
||||||
}
|
}
|
||||||
assert_equal(expected, actual)
|
assert_equal(expected, actual)
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user