Skip to content


Subversion checkout URL

You can clone with
Download ZIP


Required and optional parameters for route #490

ryanwi opened this Issue · 7 comments

5 participants


Per SO question ( it would be useful to have required and optional parameters in route strings for things like adding ".json" to a resource id to build a JSON response.

This works fine:

get '/widgets.?:format?'

But, this currently does not:

get '/widgets/:id.?:format?'

If you pass 'abc.json' to that it, the :id parameter will be 'abc.json' and the format parameter will be empty.

The compiled regex appears to be:


The "." is not part of the exclusion in the first group, so it doesn't stop there as it would for a "/" or "#".


I did get past this by going full regex on the route and excluding the "." from the first regex group. But, this seems like a fairly common use case that would be helpful to support in regular route strings.

get %r{/widgets\/([^\/?#\.]+)(?:\.|%2E)?([^\/?#]+)?}

Discussion about possible solutions:


+1 This issue is biting me, too. The Sinatra DSL should handle format extensions such as .xml, .json, and so forth in a sensible and consistent way.


Reading through the discussion in and subsequently in #492, shouldn't this be fixed with 762967f?


Yes, indeed.

@rkh rkh closed this

Does any official Sinatra release include this fix? I see that 1.3.3 is the latest, but this issue is listed as Milestone 1.4.0.

In the meantime, should we apply a similar workaround that the SO poster tried? How did he generate the compiled Sinatra regex from the route?


@omnilinguist you could also just run against Sinatra master. I'll see that I can prep a release soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.