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

Fix Rack::Lint #2438

Merged
merged 4 commits into from
May 11, 2024
Merged

Fix Rack::Lint #2438

merged 4 commits into from
May 11, 2024

Conversation

ericproulx
Copy link
Contributor

@ericproulx ericproulx commented May 11, 2024

Motivation

Rack::Lint is a middleware that validates applications, requests and responses according to Rack spec. I thought it would be great to have an idea of our compliance with the newest specs. Without any surprises, I've found a few violations but nothing major and most violations were found in specs.

This PR fixes all violations but do not prevent adding new ones. We would have to change a couple of things in the specs to add Rack::Lint in the specs but its another discussion.

Here what has been fixed:

Body yielded non-string value nil

# spec/grape/endpoint_spec.rb:848 
# spec/grape/endpoint_spec.rb:878

Under specific case, Grape might return a body equal to [nil] which is not valid according to the linter. Instead of relying on the received bodies, it returns systematically [] like Rack::Response does. See formatter

Body yielded non-string value :symbol, exception

# spec/grape/endpoint_spec.rb:811 
# spec/grape/endpoint_spec.rb:795
# spec/grape/api_spec.rb:1849 
# spec/grape/api_spec.rb:2081

When calling error! a user might not pass a string message. I've changed a bit the algorithm to make sure it's always a string. See txt.

Rewindable input

According to Rack 3 specs, input may not be rewindable. Instead of calling rewind all the time, I've added an if input.respond_to?(:rewind). See formatter

Body has not been closed

When cascading, we need to close the body if it can be closed respond_to?(:close)
See router

Fix Rack::Lint rack2 and rack3
Use Rack::MockResponse instead spec/support/chunks
Use Rack::Builder.app in spec instead of Rack::Builder.new
Remove useless Rack::Builder.new
== Changes ==
Returns [] when no entity body instead of received body
Grape::ErrorFormatter::Txt forces .to_s since it might be a symbol
Try close body in response if possible when dismissing response (cascade)
Rewind input only if rewindable
@ericproulx ericproulx marked this pull request as ready for review May 11, 2024 16:53
@dblock dblock merged commit 3f6a70a into ruby-grape:master May 11, 2024
44 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants