Skip to content

Commit

Permalink
Merge pull request #39 from a-chacon/32-204-no-content
Browse files Browse the repository at this point in the history
feat: disable autodiscover responses for endpoints with at least one …
  • Loading branch information
a-chacon authored Aug 23, 2024
2 parents ed5eef5 + ecd1318 commit 3bbed66
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/oas_rails/builders/responses_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def from_oas_route(oas_route)
end

def add_autodiscovered_responses(oas_route)
return self unless OasRails.config.autodiscover_responses
return self if !OasRails.config.autodiscover_responses || oas_route.docstring.tags(:response).any?

new_responses = Extractors::RenderResponseExtractor.extract_responses_from_source(@specification, source: oas_route.source_string)

Expand Down
20 changes: 15 additions & 5 deletions test/lib/oas_rails/builders/responses_builder_test.rb
Original file line number Diff line number Diff line change
@@ -1,35 +1,45 @@
require "test_helper"

module OasRails
module Builders
class ResponsesBuilderTest < Minitest::Test
def setup
@specification = Spec::Specification.new
@oas_route = OasRoute.new_from_rails_route(rails_route: Rails.application.routes.routes.find { |a| a.name == "users_login" })
@oas_route_login = OasRoute.new_from_rails_route(rails_route: Rails.application.routes.routes.find { |a| a.name == "users_login" })
@oas_route_show = OasRoute.new_from_rails_route(rails_route: Rails.application.routes.routes.find { |a| a.name == "user" })
end

def test_add_autodiscover_responses
OasRails.config.autodiscover_responses = true
@responses = ResponsesBuilder.new(@specification).add_autodiscovered_responses(@oas_route).build
@responses = ResponsesBuilder.new(@specification).add_autodiscovered_responses(@oas_route_login).build

assert @responses.responses.any?
end

def test_not_add_autodiscover_responses
OasRails.config.autodiscover_responses = false
@responses = ResponsesBuilder.new(@specification).add_autodiscovered_responses(@oas_route).build
@responses = ResponsesBuilder.new(@specification).add_autodiscovered_responses(@oas_route_login).build

assert @responses.responses.empty?
end

def test_add_default_responses
OasRails.config.set_default_responses = true
@responses = ResponsesBuilder.new(@specification).add_default_responses(@oas_route, true).build
@responses = ResponsesBuilder.new(@specification).add_default_responses(@oas_route_login, true).build

assert @responses.responses.any?
end

def test_not_add_default_responses
OasRails.config.set_default_responses = false
@responses = ResponsesBuilder.new(@specification).add_default_responses(@oas_route, false).build
@responses = ResponsesBuilder.new(@specification).add_default_responses(@oas_route_login, true).build

assert @responses.responses.empty?
end

def test_not_add_autodiscovered_responses_when_are_documented_responses
OasRails.config.autodiscover_responses = true
@responses = ResponsesBuilder.new(@specification).add_autodiscovered_responses(@oas_route_show).build

assert @responses.responses.empty?
end
Expand Down

0 comments on commit 3bbed66

Please sign in to comment.