Skip to content

Namespaces and routes with multiple requirement regex's aren't working #1784

@darrellnash

Description

@darrellnash

I opened a PR with the test spec to demonstrate the issue: #1783 - but here's the same info to save you a click:

I believe there's a bug that was introduced sometime after Grape 0.13 (we were running a very old version). We have several nested namespaces the have requirements to allow for dots in the path parameter.

e.g. /:user_id/:attr_id, where both :user_id and :attr_id can have dots in them.

When I upgraded to the latest Grape, these stopped working for the first namespace (:user_id) - it appears it's because when you pass in a requirements hash on a route, it replaces any existing requirements on the wrapping namespace.

This spec demonstrates the expected (and previous) behavior, especially the last test case which is failing against master.

A simplified example:

With:

namespace ':user_id', requirements: { user_id: %r{[^\/]+} } do
  get ':attr_id',     requirements: { attr_id: %r{[^\/]+} } do
    "#{params[:user_id]}/#{params[:attr_id]}"
  end
end

Expected:

GET /user.with.dots/attribute.with.dots should work.

Current behavior:

GET /user.with.dots/attribute.with.dots returns a 404 error.

Spec test that's currently failing:

require 'spec_helper'

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

  def app
    subject
  end

  context 'get' do
    it 'routes to namespace and path params with dots, with merged requirements' do
      subject.namespace ':ns_with_dots', requirements: { ns_with_dots: %r{[^\/]+} } do
        get ':another_id_with_dots',     requirements: { another_id_with_dots: %r{[^\/]+} } do
          "#{params[:ns_with_dots]}/#{params[:another_id_with_dots]}"
        end
      end

      get '/test.id/test2.id'
      expect(last_response.status).to eq 200
      expect(last_response.body).to eq 'test.id/test2.id'
    end
  end
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions