Skip to content

Commit

Permalink
Fix hash class tested for inheritance (#201)
Browse files Browse the repository at this point in the history
* Fix hash class tested for inheritance

Fixes the check against MediaTypes::Hash instead of ::Hash

* Bump the version

* Update CHANGELOG.md

* Fix `locals` passing for HTML serialization
  • Loading branch information
SleeplessByte authored Aug 17, 2023
1 parent fb71750 commit 80d8944
Show file tree
Hide file tree
Showing 7 changed files with 247 additions and 15 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 2.0.4

- ✨ Add more loose tests
- 🐛 Fix `locals` passing for HTML serialization
- 🐛 Fix for status passing in rack-like environments

## 2.0.3

- 🐛 Fix debian packages not containing files
Expand Down
169 changes: 169 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
PATH
remote: .
specs:
media_types-serialization (2.0.4)
actionpack (>= 6.0.0)
activesupport (>= 6.0.0)
media_types (>= 2.2.3, < 3.0.0)

GEM
remote: https://rubygems.org/
specs:
actioncable (6.1.7.4)
actionpack (= 6.1.7.4)
activesupport (= 6.1.7.4)
nio4r (~> 2.0)
websocket-driver (>= 0.6.1)
actionmailbox (6.1.7.4)
actionpack (= 6.1.7.4)
activejob (= 6.1.7.4)
activerecord (= 6.1.7.4)
activestorage (= 6.1.7.4)
activesupport (= 6.1.7.4)
mail (>= 2.7.1)
actionmailer (6.1.7.4)
actionpack (= 6.1.7.4)
actionview (= 6.1.7.4)
activejob (= 6.1.7.4)
activesupport (= 6.1.7.4)
mail (~> 2.5, >= 2.5.4)
rails-dom-testing (~> 2.0)
actionpack (6.1.7.4)
actionview (= 6.1.7.4)
activesupport (= 6.1.7.4)
rack (~> 2.0, >= 2.0.9)
rack-test (>= 0.6.3)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.2.0)
actiontext (6.1.7.4)
actionpack (= 6.1.7.4)
activerecord (= 6.1.7.4)
activestorage (= 6.1.7.4)
activesupport (= 6.1.7.4)
nokogiri (>= 1.8.5)
actionview (6.1.7.4)
activesupport (= 6.1.7.4)
builder (~> 3.1)
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.1, >= 1.2.0)
activejob (6.1.7.4)
activesupport (= 6.1.7.4)
globalid (>= 0.3.6)
activemodel (6.1.7.4)
activesupport (= 6.1.7.4)
activerecord (6.1.7.4)
activemodel (= 6.1.7.4)
activesupport (= 6.1.7.4)
activestorage (6.1.7.4)
actionpack (= 6.1.7.4)
activejob (= 6.1.7.4)
activerecord (= 6.1.7.4)
activesupport (= 6.1.7.4)
marcel (~> 1.0)
mini_mime (>= 1.1.0)
activesupport (6.1.7.4)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 1.6, < 2)
minitest (>= 5.1)
tzinfo (~> 2.0)
zeitwerk (~> 2.3)
awesome_print (1.9.2)
builder (3.2.4)
concurrent-ruby (1.2.2)
crass (1.0.6)
date (3.3.3)
erubi (1.12.0)
globalid (1.1.0)
activesupport (>= 5.0)
i18n (1.14.1)
concurrent-ruby (~> 1.0)
loofah (2.21.3)
crass (~> 1.0.2)
nokogiri (>= 1.12.0)
mail (2.8.1)
mini_mime (>= 0.1.1)
net-imap
net-pop
net-smtp
marcel (1.0.2)
media_types (2.3.2)
method_source (1.0.0)
mini_mime (1.1.5)
minitest (5.19.0)
net-imap (0.3.7)
date
net-protocol
net-pop (0.1.2)
net-protocol
net-protocol (0.2.1)
timeout
net-smtp (0.3.3)
net-protocol
nio4r (2.5.9)
nokogiri (1.15.4-x64-mingw32)
racc (~> 1.4)
oj (3.16.0)
racc (1.7.1)
rack (2.2.8)
rack-test (2.1.0)
rack (>= 1.3)
rails (6.1.7.4)
actioncable (= 6.1.7.4)
actionmailbox (= 6.1.7.4)
actionmailer (= 6.1.7.4)
actionpack (= 6.1.7.4)
actiontext (= 6.1.7.4)
actionview (= 6.1.7.4)
activejob (= 6.1.7.4)
activemodel (= 6.1.7.4)
activerecord (= 6.1.7.4)
activestorage (= 6.1.7.4)
activesupport (= 6.1.7.4)
bundler (>= 1.15.0)
railties (= 6.1.7.4)
sprockets-rails (>= 2.0.0)
rails-dom-testing (2.2.0)
activesupport (>= 5.0.0)
minitest
nokogiri (>= 1.6)
rails-html-sanitizer (1.6.0)
loofah (~> 2.21)
nokogiri (~> 1.14)
railties (6.1.7.4)
actionpack (= 6.1.7.4)
activesupport (= 6.1.7.4)
method_source
rake (>= 12.2)
thor (~> 1.0)
rake (13.0.6)
sprockets (4.2.0)
concurrent-ruby (~> 1.0)
rack (>= 2.2.4, < 4)
sprockets-rails (3.4.2)
actionpack (>= 5.2)
activesupport (>= 5.2)
sprockets (>= 3.0.0)
thor (1.2.2)
timeout (0.4.0)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
websocket-driver (0.7.6)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
zeitwerk (2.6.11)

PLATFORMS
x64-mingw32

DEPENDENCIES
awesome_print
bundler
media_types-serialization!
minitest (~> 5.0)
oj
rails (~> 6.0)
rake (~> 13.0)

BUNDLED WITH
2.1.4
19 changes: 13 additions & 6 deletions lib/media_types/serialization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def allow_output_html(as: nil, view: nil, layout: nil, formats: [:html], variant
options[:template] = view unless view.nil?
options[:formats] = formats unless formats.nil?
options[:variants] = variants unless variants.nil?
options[:locals] = victim if victim.is_a?(Hash)
options[:locals] = victim if victim.is_a?(::Hash)
options[:assigns] = { media: victim }

controller.render_to_string(**options)
Expand Down Expand Up @@ -448,7 +448,7 @@ def resolve_media_type(request, registration, allow_last: true)
end

return @serialization_override_accept if registration.has? @serialization_override_accept

# Always render problems in api viewer if we can't show chosen override
return 'application/problem+json' if registration.has? 'application/problem+json'

Expand Down Expand Up @@ -541,10 +541,14 @@ def serializer_freeze_io_internal
self
)
rescue InputValidationFailedError => e
serializers = @serialization_input_validation_failed_serializer || [
MediaTypes::Serialization::Serializers::ProblemSerializer,
MediaTypes::Serialization::Serializers::InputValidationErrorSerializer
]
serializers = if defined?(@serialization_input_validation_failed_serializer)
@serialization_input_validation_failed_serializer
else
[
MediaTypes::Serialization::Serializers::ProblemSerializer,
MediaTypes::Serialization::Serializers::InputValidationErrorSerializer
]
end
registrations = SerializationRegistration.new(:output)
serializers.each do |s|
registrations = registrations.merge(s.outputs_for(views: [nil, :html]))
Expand Down Expand Up @@ -671,6 +675,9 @@ def serialization_render_resolved(obj:, identifier:, serializer:, registrations:
end

if context.serialization_custom_render.nil?
status = options.delete(:status)
response.status = status if status

render body: result, **options

response.content_type = registrations.identifier_for(identifier)
Expand Down
2 changes: 1 addition & 1 deletion lib/media_types/serialization/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module MediaTypes
module Serialization
VERSION = '2.0.3'
VERSION = '2.0.4'
end
end
2 changes: 1 addition & 1 deletion media_types-serialization.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Gem::Specification.new do |spec|

spec.add_dependency 'actionpack', '>= 6.0.0'
spec.add_dependency 'activesupport', '>= 6.0.0'
spec.add_dependency 'media_types', '>= 2.2.0', '< 3.0.0'
spec.add_dependency 'media_types', '>= 2.2.3', '< 3.0.0'

spec.add_development_dependency 'awesome_print'
spec.add_development_dependency 'bundler'
Expand Down
59 changes: 54 additions & 5 deletions test/media_types/loose_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'active_support/callbacks'
require 'abstract_controller/callbacks'
require 'abstract_controller/rendering'
require 'rack/utils'
require 'rack/response'
require 'action_dispatch/http/content_security_policy'
require 'action_controller'
Expand All @@ -16,6 +17,8 @@
require 'media_types'

require 'oj'
require 'awesome_print'


class MediaTypes::LooseTest < Minitest::Test
class MyResourceMediaType
Expand Down Expand Up @@ -71,12 +74,42 @@ def action
end
end

class FakeLooseController < BaseController
allow_input_serializer(MyResourceSerializer)
allow_output_serializer(MyResourceSerializer)
freeze_io!

def action
data = deserialize!(request)
data[:_links][:self] = { href: 'https://example.org/loose' }

render_media(data, status: 201)
end
end

def setup
@controller = FakeStrictController.new
@response = ActionDispatch::Response.new
@strict_controller = FakeStrictController.new
end

def test_strict_on_input
response = ActionDispatch::Response.new
loose_controller = FakeLooseController.new

content_type = 'application/vnd.mydomain.my_resource.v1+json'
request = ActionDispatch::Request.new({
Rack::RACK_INPUT => StringIO.new({ test: 1, _links: { } }.to_json),
'CONTENT_TYPE' => content_type,
'HTTP_ACCEPT' => "#{content_type}, application/problem+json; q=0.1"
})

# Input is not valid because example is unexpected
status, _headers, _body = loose_controller.dispatch(:action, request, response)
assert_equal 400, status
end

def test_input_validates
def test_loose_on_input_only
response = ActionDispatch::Response.new

content_type = 'application/vnd.mydomain.my_resource.v1+json'
request = ActionDispatch::Request.new({
Rack::RACK_INPUT => StringIO.new({ test: 1, _links: { example: { href: "https://example.org/" } } }.to_json),
Expand All @@ -86,8 +119,24 @@ def test_input_validates

# Passing input to output should fail (output isn't loose)
assert_raises MediaTypes::Serialization::OutputValidationFailedError do
@controller.dispatch(:action, request, @response)
@strict_controller.dispatch(:action, request, response)
end
end
end


def test_invariant
response = ActionDispatch::Response.new
loose_controller = FakeLooseController.new

content_type = 'application/vnd.mydomain.my_resource.v1+json'
request = ActionDispatch::Request.new({
Rack::RACK_INPUT => StringIO.new({ test: 1, _links: { example: { href: "https://example.org/" } } }.to_json),
'CONTENT_TYPE' => content_type,
'HTTP_ACCEPT' => "#{content_type}, application/problem+json; q=0.1"
})

# Passing input to output should pass (loose link is added)
status, _headers, _body = loose_controller.dispatch(:action, request, response)
assert_equal 201, status
end
end
5 changes: 3 additions & 2 deletions test/media_types/serialization_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ def self.organisation
version 1 do
attribute :name
attribute :number, Numeric
link :google
link :google do
attribute :foo
end
collection :items, allow_empty: true do
attribute :label
attribute :data, Object
Expand Down Expand Up @@ -129,4 +131,3 @@ def test_it_extracts_links
assert_equal "<https://google.com>; rel=google; foo=bar", @response['Link']
end
end

0 comments on commit 80d8944

Please sign in to comment.