-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Respect content type set in superclass before filter #1649
Conversation
@namusyaka another regression has been identified in v2.1.0. |
@jkowens can you provide me which the patch/issue raised this regression? |
@namusyaka yes, it was PR #1606. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for working on this.
But I think we should do additional work a bit.
-
Originally,
@pinned_response
should be initialized for starting each request.
The@pinned_response
looks to be reusing for every request now.
Probably this won't be an issue in most cases because the filters are managed globally in the class, but not good for me. -
I don't think we need to pin the response for both before/after filters.
Does the following change work well?
diff --git a/lib/sinatra/base.rb b/lib/sinatra/base.rb
index 11cd284a..9d0ec49a 100644
--- a/lib/sinatra/base.rb
+++ b/lib/sinatra/base.rb
@@ -994,11 +994,11 @@ module Sinatra
# Run filters defined on the class and all superclasses.
# Accepts an optional block to call after each filter is applied.
- def filter!(type, base = settings)
- filter! type, base.superclass if base.superclass.respond_to?(:filters)
+ def filter!(type, base = settings, &block)
+ filter! type, base.superclass, &block if base.superclass.respond_to?(:filters)
base.filters[type].each do |args|
result = process_route(*args)
- yield result if block_given?
+ block.call(result) if block_given?
end
end
lib/sinatra/base.rb
Outdated
yield result if block_given? | ||
end | ||
base.filters[type].each { |args| process_route(*args) } | ||
@pinned_response = !response['Content-Type'].nil? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need to pin the response for both before/after filters?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, I don't think we do.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an excellent solution, I'll push an update now.
Could you also consider the initialization of Line 934 in 0aa8d50
|
It looks like Line 923 in 0aa8d50
Is that ok? |
No. It's executed at the boot time, not every request. |
Ah got it. Ok, initialization has been moved to the |
I'm sorry for the late, I'll review this in a few days. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your patience. I'm sorry for the late action.
LGTM with a few nits. Let's merge this after resolving my comments.
lib/sinatra/base.rb
Outdated
@@ -920,7 +920,6 @@ def initialize(app = nil) | |||
super() | |||
@app = app | |||
@template_cache = Tilt::Cache.new | |||
@pinned_response = nil # whether a before! filter pinned the content-type |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: You may want to leave this here. If I remember correctly, initializing instance variables at the stage of initialization is in line with ruby's way. This will show up a new warning like warning: instance variable @pinned_response not initialized
if you keep this change. Of course, I'm recognizing that there are a lot of warnings similar to that already, though.
Fixes #1647.