ruby-grape / ruby-grape/grape

String route params not matched for email

Open
#1,742 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug?
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.