Skip to content

Commit 6c321cd

Browse files
bf4NullVoxPopuli
authored andcommitted
Assert Schema (#1677)
* Assert Schema * Fix regression from #1695 where JSONAPI renders empty meta * Add changelog
1 parent 9f59398 commit 6c321cd

File tree

4 files changed

+45
-10
lines changed

4 files changed

+45
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Breaking changes:
66
- [#1662](https://github.com/rails-api/active_model_serializers/pull/1662) Drop support for Rails 4.0 and Ruby 2.0.0. (@remear)
77

88
Features:
9+
- [#1677](https://github.com/rails-api/active_model_serializers/pull/1677) Add `assert_schema`, `assert_request_schema`, `assert_request_response_schema`. (@bf4)
910
- [#1697](https://github.com/rails-api/active_model_serializers/pull/1697) Include actual exception message with custom exceptions;
1011
`Test::Schema` exceptions are now `Minitest::Assertion`s. (@bf4)
1112
- [#1699](https://github.com/rails-api/active_model_serializers/pull/1699) String/Lambda support for conditional attributes/associations (@mtsmfm)

lib/active_model_serializers/adapter/json_api.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def success_document
131131
hash[:links].update(pagination_links_for(serializer))
132132
end
133133

134-
hash[:meta] = instance_options[:meta] if instance_options[:meta].is_a?(Hash)
134+
hash[:meta] = instance_options[:meta] unless instance_options[:meta].blank?
135135

136136
hash
137137
end

lib/active_model_serializers/test/schema.rb

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,38 @@ module Schema
1010
# get :index
1111
# assert_response_schema
1212
def assert_response_schema(schema_path = nil, message = nil)
13-
matcher = AssertResponseSchema.new(schema_path, response, message)
13+
matcher = AssertResponseSchema.new(schema_path, request, response, message)
14+
assert(matcher.call, matcher.message)
15+
end
16+
17+
def assert_request_schema(schema_path = nil, message = nil)
18+
matcher = AssertRequestSchema.new(schema_path, request, response, message)
19+
assert(matcher.call, matcher.message)
20+
end
21+
22+
# May be renamed
23+
def assert_request_response_schema(schema_path = nil, message = nil)
24+
assert_request_schema(schema_path, message)
25+
assert_response_schema(schema_path, message)
26+
end
27+
28+
def assert_schema(payload, schema_path = nil, message = nil)
29+
matcher = AssertSchema.new(schema_path, request, response, message, payload)
1430
assert(matcher.call, matcher.message)
1531
end
1632

1733
MissingSchema = Class.new(Minitest::Assertion)
1834
InvalidSchemaError = Class.new(Minitest::Assertion)
1935

20-
class AssertResponseSchema
21-
attr_reader :schema_path, :response, :message
36+
class AssertSchema
37+
attr_reader :schema_path, :request, :response, :message, :payload
2238

23-
def initialize(schema_path, response, message)
39+
# Interface may change.
40+
def initialize(schema_path, request, response, message, payload = nil)
2441
require_json_schema!
42+
@request = request
2543
@response = response
44+
@payload = payload
2645
@schema_path = schema_path || schema_path_default
2746
@message = message
2847
@document_store = JsonSchema::DocumentStore.new
@@ -41,11 +60,11 @@ def call
4160
attr_reader :document_store
4261

4362
def controller_path
44-
response.request.filtered_parameters[:controller]
63+
request.filtered_parameters[:controller]
4564
end
4665

4766
def action
48-
response.request.filtered_parameters[:action]
67+
request.filtered_parameters[:action]
4968
end
5069

5170
def schema_directory
@@ -68,6 +87,10 @@ def response_body
6887
load_json(response.body)
6988
end
7089

90+
def request_params
91+
request.env['action_dispatch.request.request_parameters']
92+
end
93+
7194
def json_schema
7295
@json_schema ||= JsonSchema.parse!(schema_data)
7396
end
@@ -98,6 +121,18 @@ def require_json_schema!
98121
raise LoadError, "You don't have json_schema installed in your application. Please add it to your Gemfile and run bundle install"
99122
end
100123
end
124+
class AssertResponseSchema < AssertSchema
125+
def initialize(*)
126+
super
127+
@payload = response_body
128+
end
129+
end
130+
class AssertRequestSchema < AssertSchema
131+
def initialize(*)
132+
super
133+
@payload = request_params
134+
end
135+
end
101136
end
102137
end
103138
end

test/serializers/meta_test.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def test_meta_key_is_not_used_with_json_api
110110
assert_equal(expected, actual)
111111
end
112112

113-
def test_meta_key_is_present_when_empty_hash_with_json_api
113+
def test_meta_key_is_not_present_when_empty_hash_with_json_api
114114
actual = ActiveModelSerializers::SerializableResource.new(
115115
@blog,
116116
adapter: :json_api,
@@ -122,8 +122,7 @@ def test_meta_key_is_present_when_empty_hash_with_json_api
122122
id: '1',
123123
type: 'blogs',
124124
attributes: { title: 'AMS Hints' }
125-
},
126-
meta: {}
125+
}
127126
}
128127
assert_equal(expected, actual)
129128
end

0 commit comments

Comments
 (0)