ruby-grape / ruby-grape/grape

Namespaced requests with optional URL params fail with empty parameter

Open
#932 4 comments 0 reactions 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

In short: /root returns 404 but expected to route into the get with empty type. It works only on root level requests.

class API < Grape::API
  namespace :root do
    get '/(:type)' do
      params[:type]
    end
  end
end

get '/root/some_type' # => some_type
get '/root' # => 404

Full test:

require 'spec_helper'

describe Grape::API do
  subject { Class.new(Grape::API) }

  def app
    subject
  end

  describe 'url parameters' do
    it 'routes to a route with a required named url parameter' do
      subject.namespace :root do
        get '/hello' do
          'hello'
        end

        get '/:type' do
          params[:type]
        end
      end

      get '/root/hello'
      expect(last_response.body).to eq('hello')

      get '/root/some_required_type'
      expect(last_response.body).to eq('some_required_type')
    end

    it 'routes to a route with optional parameter' do
      subject.get '/(:type)' do
        "type=#{params[:type]}"
      end

      get '/hello'
      expect(last_response.body).to eq('type=hello')

      get '/'
      expect(last_response.body).to eq('type=')
    end

    it 'routes to a namespaced route with optional parameter' do
      subject.namespace :root do
        get '/(:type)' do
          "type=#{params[:type]}"
        end
      end

      get '/root/some_type'
      expect(last_response.body).to eq('type=some_type')

      get '/root'
      expect(last_response.body).to eq('type=') # => getting "Not Found"
    end
  end
end

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 with the supplied Grape::API spec for “routes to a namespaced route with optional parameter” and run the existing test suite. Trace how namespace :root combines with the optional '/(:type)' route. Done means GET /root returns a successful response with type=, while /root/some_type still returns type=some_type.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.