String route params not matched for email
Nobody has claimed this yet.
- Dominant language
- Ruby
- Stars
- 10k
- Forks
- 1.2k
- Avg merge
- 14h 38m
- Merged PRs (30d)
- 92
Description
Hi,
We seem to have encountered a bug with the route system.
class MyClass < Grape::API
EMAIL_OR_ID_REGEXP = %r{([A-Z0-9._%a-z\-]+(@|%40)(?:[A-Z0-9a-z\-]+\.)+[A-Za-z]{2,14})|(\d+)}
desc "Returns a user"
params do
requires :user_identifier, type: String, allow_blank: false, desc: "ID or email"
end
get '/user/:user_identifier' do
puts "GET method"
end
desc "Creates a user"
params do
requires :user_identifier, type: String, allow_blank: false, desc: "ID or email"
end
post '/user/:user_identifier', requirements: { user_identifier: EMAIL_OR_ID_REGEXP } do
puts "POST method"
end
end
We try this with Swagger UI, which says that a command line equivalent would be :
curl -X GET --header 'Accept: application/json' 'http://127.0.0.1:9299/api/user/anything%40something.else
The thing is that we do not match the GET regex of the match? method of router.rb
Because of this, we continue and end up in the greedy_match? method. This one finds the POST endpoint, and /user/:user_identifier matches but not the "post", so it returns a 405 Method not allowed exception.
If we add the requirement (the same as the post endpoint), it succeeds, presumably because you add our regex to yours.
I believe the issue is due to our submitting that converts @ by %40, which is unsupported by grape. But it is a string nonetheless, no?
Thanks,
Simon
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in router.rb by tracing match? and greedy_match? for the GET and POST routes shown in the report. Reproduce the request with the provided curl command and inspect how the encoded email parameter is matched. Done means the GET endpoint is selected for the encoded email request instead of returning a 405 response.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- ruby
- Domain
- api
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100