Skip to content

Commit

Permalink
Fix CI: Get the build green for modern ruby and rack and rubocop-rspec (
Browse files Browse the repository at this point in the history
#939)

* Update how we use Rack::Utils to get status codes

As part of rack/rack#2137 (released in Rack 3) Rack
changed the values of `Rack::Utils::SYMBOL_TO_STATUS_CODE` so asking for
`:unprocessable_entity` now returns `nil`. The guidance has always been to use
`Rack::Util.status_code` (present in the API since Rack 1.1) so let's just do
that.

* Handle a missing `rack.input` in the env

Ever since rack/rack#2018 `rack.input` has been
optional so we can't assume it's there and call `read` or `rewind` on it in
`Apipie::Extractor::Recorder`. Using safe navigation to call these methods
seems like the simplest approach because we want to look for
`rack.request.form_hash` instead in both the situation where `rack.input` is
missing, or it's empty.

* Add rubocop-rspec_rails and get rubocop run to green

In rubocop/rubocop-rspec#1848 rubocop-rspec extracted
some cops to separate gems, and because we referenced one of them in our
rubocop_todo.yml the whole rubocop process wouldn't run because it doesn't like
config for cops it doesn't know about. We introducing the missing gem,
`rubocop-rspec_rails`, and fix the name in the config to let us run rubocop
again.

There is one infraction that exists in the source so we fix that to get us to a
green rubocop run.

* Only add rubocop-rspec_rails for ruby >-= 2.7

It's only available for 2.7+ and we still run this build on 2.6.
  • Loading branch information
h-lame authored Jul 9, 2024
1 parent a21bf0d commit 89e7ded
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 11 deletions.
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require:
- rubocop-rails
- rubocop-rspec
- rubocop-rspec_rails
- rubocop-performance

inherit_from: .rubocop_todo.yml
Expand Down
2 changes: 1 addition & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1159,7 +1159,7 @@ RSpec/PredicateMatcher:
# Offense count: 1
# This cop supports unsafe autocorrection (--autocorrect-all).
# Configuration parameters: Inferences.
RSpec/Rails/InferredSpecType:
RSpecRails/InferredSpecType:
Exclude:
- 'spec/controllers/pets_controller_spec.rb'

Expand Down
1 change: 1 addition & 0 deletions apipie-rails.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Gem::Specification.new do |s|
s.add_development_dependency 'rubocop-rails'
s.add_development_dependency 'rubocop-rspec'
s.add_development_dependency 'rubocop-performance'
s.add_development_dependency 'rubocop-rspec_rails' if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.7")
s.add_development_dependency "simplecov"
s.add_development_dependency "sqlite3"
end
8 changes: 6 additions & 2 deletions lib/apipie/error_description.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ def initialize(code_or_options, desc = nil, options = {})
@metadata = code_or_options[:meta]
@description = code_or_options[:desc] || code_or_options[:description]
else
@code =
@code =
if code_or_options.is_a? Symbol
Rack::Utils::SYMBOL_TO_STATUS_CODE[code_or_options]
begin
Rack::Utils.status_code(code_or_options)
rescue ArgumentError
nil
end
else
code_or_options
end
Expand Down
4 changes: 2 additions & 2 deletions lib/apipie/extractor/recorder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ def analyse_env(env)
@params = Rack::Utils.parse_nested_query(@query)
@params.merge!(env["action_dispatch.request.request_parameters"] || {})
rack_input = env["rack.input"]
if data = parse_data(rack_input.read)
if data = parse_data(rack_input&.read)
@request_data = data
elsif form_hash = env["rack.request.form_hash"]
@request_data = reformat_multipart_data(form_hash)
end
rack_input.rewind
rack_input&.rewind
end

def analyse_controller(controller)
Expand Down
15 changes: 10 additions & 5 deletions lib/apipie/response_description.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,16 @@ def initialize(method_description, code, options, scope, block, adapter)

@method_description = method_description

if code.is_a? Symbol
@code = Rack::Utils::SYMBOL_TO_STATUS_CODE[code]
else
@code = code
end
@code =
if code.is_a? Symbol
begin
Rack::Utils.status_code(code)
rescue ArgumentError
nil
end
else
code
end

@description = options[:desc]
if @description.nil?
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/swagger/swagger_dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ def have_field?(field, expected_name, breadcrumb)
expect(schema).to have_field(:pet_id, 'number', {:description => 'id of pet'})
expect(schema).to have_field(:pet_name, 'string', {:description => 'Name of pet', :required => false})
expect(schema).to have_field(:animal_type, 'string', {:description => 'Type of pet', :enum => %w[dog cat iguana kangaroo]})
expect(schema).not_to have_field(:partial_match_allowed, 'boolean', {:required => false}) # rubocop:disable Capybara/NegationMatcher
expect(schema).not_to have_field(:partial_match_allowed, 'boolean', {:required => false})
end

it "creates a swagger definition with all input parameters" do
Expand Down

0 comments on commit 89e7ded

Please sign in to comment.