From 6c321cd8625c5163f3bc429876901a952bc313fb Mon Sep 17 00:00:00 2001 From: Benjamin Fleischer Date: Tue, 17 May 2016 12:22:38 -0500 Subject: [PATCH] Assert Schema (#1677) * Assert Schema * Fix regression from #1695 where JSONAPI renders empty meta * Add changelog --- CHANGELOG.md | 1 + .../adapter/json_api.rb | 2 +- lib/active_model_serializers/test/schema.rb | 47 ++++++++++++++++--- test/serializers/meta_test.rb | 5 +- 4 files changed, 45 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5488ad17..15398370 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) 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; `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) diff --git a/lib/active_model_serializers/adapter/json_api.rb b/lib/active_model_serializers/adapter/json_api.rb index 6ee70e16..8085bc93 100644 --- a/lib/active_model_serializers/adapter/json_api.rb +++ b/lib/active_model_serializers/adapter/json_api.rb @@ -131,7 +131,7 @@ module ActiveModelSerializers hash[:links].update(pagination_links_for(serializer)) end - hash[:meta] = instance_options[:meta] if instance_options[:meta].is_a?(Hash) + hash[:meta] = instance_options[:meta] unless instance_options[:meta].blank? hash end diff --git a/lib/active_model_serializers/test/schema.rb b/lib/active_model_serializers/test/schema.rb index b356608e..1f4dccc0 100644 --- a/lib/active_model_serializers/test/schema.rb +++ b/lib/active_model_serializers/test/schema.rb @@ -10,19 +10,38 @@ module ActiveModelSerializers # get :index # assert_response_schema 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) end MissingSchema = Class.new(Minitest::Assertion) InvalidSchemaError = Class.new(Minitest::Assertion) - class AssertResponseSchema - attr_reader :schema_path, :response, :message + class AssertSchema + 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! + @request = request @response = response + @payload = payload @schema_path = schema_path || schema_path_default @message = message @document_store = JsonSchema::DocumentStore.new @@ -41,11 +60,11 @@ module ActiveModelSerializers attr_reader :document_store def controller_path - response.request.filtered_parameters[:controller] + request.filtered_parameters[:controller] end def action - response.request.filtered_parameters[:action] + request.filtered_parameters[:action] end def schema_directory @@ -68,6 +87,10 @@ module ActiveModelSerializers load_json(response.body) end + def request_params + request.env['action_dispatch.request.request_parameters'] + end + def json_schema @json_schema ||= JsonSchema.parse!(schema_data) 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" 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 diff --git a/test/serializers/meta_test.rb b/test/serializers/meta_test.rb index 45dc9ea0..ade2e81e 100644 --- a/test/serializers/meta_test.rb +++ b/test/serializers/meta_test.rb @@ -110,7 +110,7 @@ module ActiveModel assert_equal(expected, actual) 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( @blog, adapter: :json_api, @@ -122,8 +122,7 @@ module ActiveModel id: '1', type: 'blogs', attributes: { title: 'AMS Hints' } - }, - meta: {} + } } assert_equal(expected, actual) end