Skip to content

Commit

Permalink
fix: skip filter now works with annotation filter methods
Browse files Browse the repository at this point in the history
fixes #65
  • Loading branch information
stakach committed Dec 13, 2022
1 parent e7f1710 commit d5d06c1
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 6 deletions.
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: action-controller
version: 5.5.1
version: 5.5.2
crystal: ">= 1.5.0"

dependencies:
Expand Down
2 changes: 1 addition & 1 deletion spec/open_api_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe ActionController::OpenAPI do
it "generates openapi docs" do
result = ActionController::OpenAPI.generate_open_api_docs("title", "version", description: "desc")
result[:openapi].should eq "3.0.3"
result[:paths].size.should eq 19
result[:paths].size.should eq 21
result[:info][:description].should eq "desc"
end
end
8 changes: 8 additions & 0 deletions spec/route_builder_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,12 @@ describe AC::Route::Builder do
result = client.post("/filtering/some_other_entry/", headers: headers, body: body)
result.body.should eq("3.14")
end

it "should skip filters as expected" do
result = client.get("/skipping_symbol")
result.status_code.should eq 403

result = client.get("/skipping_annotation")
result.status_code.should eq 200
end
end
25 changes: 24 additions & 1 deletion spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,40 @@ abstract class FilterOrdering < ActionController::Base
@in_around = false

before_action :set_trust
before_action :check_trust

def set_trust
@trusted = true
end

@[AC::Route::Filter(:before_action)]
def check_trust
render :forbidden, text: "Trust check failed" unless @trusted
end
end

class SkippingAnnotation < FilterOrdering
# `base "/skipping_annotation"` configured automatically

skip_action :set_trust
skip_action :check_trust

@[AC::Route::GET("/")]
def index
render text: "ok #{@trusted}"
end
end

class SkippingSymbol < FilterOrdering
# `base "/skipping_symbol"` configured automatically

skip_action :set_trust

@[AC::Route::GET("/")]
def index
render text: "ok #{@trusted}"
end
end

class Filtering < FilterOrdering
# `base "/filtering"` configured automatically

Expand Down
23 changes: 21 additions & 2 deletions src/action-controller/base.cr
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,11 @@ abstract class ActionController::Base
{% around_actions = around_actions.reject(&.==(method)) %}
{% end %}
{% end %}

{% filter_name = options[2] %}
{% if skipping.includes?(filter_name.id) %}
{% around_actions = around_actions.reject(&.==(method)) %}
{% end %}
{% end %}
{% around_actions = around_actions.reject { |act| skipping.includes?(act) } %}

Expand All @@ -439,6 +444,11 @@ abstract class ActionController::Base
{% before_actions = before_actions.reject(&.==(method)) %}
{% end %}
{% end %}

{% filter_name = options[2] %}
{% if skipping.includes?(filter_name.id) %}
{% before_actions = before_actions.reject(&.==(method)) %}
{% end %}
{% end %}
{% before_actions = before_actions.reject { |act| skipping.includes?(act) } %}

Expand Down Expand Up @@ -518,6 +528,11 @@ abstract class ActionController::Base
{% after_actions = after_actions.reject(&.==(method)) %}
{% end %}
{% end %}

{% filter_name = options[2] %}
{% if skipping.includes?(filter_name.id) %}
{% after_actions = after_actions.reject(&.==(method)) %}
{% end %}
{% end %}
{% after_actions = after_actions.reject { |act| skipping.includes?(act) } %}

Expand Down Expand Up @@ -664,7 +679,7 @@ abstract class ActionController::Base
end

macro __define_filter_macro__(name, store, method = nil)
macro {{name.id}}({% if method.nil? %} method, {% end %} only = nil, except = nil)
macro {{name.id}}({% if method.nil? %} method, {% end %} only = nil, except = nil, filter_name = nil)
{% if method %}
\{% method = {{method}} %}
{% else %}
Expand Down Expand Up @@ -694,7 +709,11 @@ abstract class ActionController::Base
\{% else %}
\{% except = prev_except %}
\{% end %}
\{% {{store}}[method] = {only, except} %}
\{% if filter_name %}
\{% else %}
\{% filter_name = method %}
\{% end %}
\{% {{store}}[method] = {only, except, filter_name} %}
end
end

Expand Down
2 changes: 1 addition & 1 deletion src/action-controller/router/builder.cr
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ module ActionController::Route::Builder
{% open_api_route[:params] = open_api_params %}
{% OPENAPI_FILTERS[@type.name.stringify + "#" + method_name.stringify] = open_api_route %}

{{filter_type}}({{function_wrapper_name.symbolize}}, only: {{ann[:only]}}, except: {{ann[:except]}})
{{filter_type}}({{function_wrapper_name.symbolize}}, only: {{ann[:only]}}, except: {{ann[:except]}}, filter_name: {{method_name}})

# :nodoc:
def {{function_wrapper_name}}
Expand Down

0 comments on commit d5d06c1

Please sign in to comment.