Skip to content
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

Feature/ignore custom route #54

Merged
merged 8 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ Then fill it with your data. Below are the available configuration options:
- `config.autodiscover_request_body`: Automatically detects request bodies for create/update methods. Default is `true`.
- `config.autodiscover_responses`: Automatically detects responses from controller renders. Default is `true`.
- `config.api_path`: Sets the API path if your API is under a different namespace.
- `config.ignored_actions`: Sets an array with the controller or controller#action. No necessary to add api_path before.

### Authentication Settings

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@
# Default: false
# config.layout = "application"

# Excluding custom controlers or controlers#action
# Example: ["projects", "users#new"]
# config.ignored_actions = []

# #######################
# Authentication Settings
# #######################
Expand Down
2 changes: 2 additions & 0 deletions lib/oas_rails/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Configuration
:autodiscover_request_body,
:autodiscover_responses,
:api_path,
:ignored_actions,
:security_schemas,
:authenticate_all_routes_by_default,
:set_default_responses,
Expand All @@ -23,6 +24,7 @@ def initialize
@autodiscover_request_body = true
@autodiscover_responses = true
@api_path = "/"
@ignored_actions = []
@authenticate_all_routes_by_default = true
@security_schema = nil
@security_schemas = {}
Expand Down
22 changes: 22 additions & 0 deletions lib/oas_rails/extractors/route_extractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ def valid_api_route?(route)
return false if RAILS_DEFAULT_CONTROLLERS.any? { |default| route.defaults[:controller].start_with?(default) }
return false if RAILS_DEFAULT_PATHS.any? { |path| route.path.spec.to_s.include?(path) }
return false unless route.path.spec.to_s.start_with?(OasRails.config.api_path)
return false if ignore_custom_actions(route)

true
end
Expand All @@ -119,6 +120,27 @@ def valid_route_implementation?(route)
controller_class.instance_methods.include?(action_name.to_sym)
end
end

# Ignore user-specified paths in initializer configuration.
# Sanitize api_path by removing the "/" if it starts with that, and adding "/" if it ends without that.
# Support controller name only to ignore all controller actions.
# Support ignoring "controller#action"
# Ignoring "controller#action" AND "api_path/controller#action"
def ignore_custom_actions(route)
api_path = "#{OasRails.config.api_path.sub(%r{\A/}, '')}/".sub(%r{/+$}, '/')
ignored_actions = OasRails.config.ignored_actions.flat_map do |custom_route|
if custom_route.start_with?(api_path)
[custom_route]
else
["#{api_path}#{custom_route}", custom_route]
end
end

controller_action = "#{route.defaults[:controller]}##{route.defaults[:action]}"
controller_only = route.defaults[:controller]

ignored_actions.include?(controller_action) || ignored_actions.include?(controller_only)
end
end
end
end
Expand Down
26 changes: 26 additions & 0 deletions test/lib/oas_rails/extractors/route_extractor_test.rb
a-chacon marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module OasRails
module Builders
class RouteExtractorTest < Minitest::Test
def test_without_custom_routes
OasRails.config.ignored_actions = []
result = Extractors::RouteExtractor.host_routes
assert_equal 14, result.count
Extractors::RouteExtractor.clear_cache
end

def test_with_custom_controllers_actions
OasRails.config.ignored_actions = ["projects#index"]
result = Extractors::RouteExtractor.host_routes
assert_equal 13, result.count
Extractors::RouteExtractor.clear_cache
end

def test_with_custom_controller_only
OasRails.config.ignored_actions = ["projects"]
result = Extractors::RouteExtractor.host_routes
assert_equal 8, result.count
Extractors::RouteExtractor.clear_cache
end
end
end
end