Skip to content

Add wildcard segments support

David Fernandez requested to merge 10io-fix-wildcard-segments into master

What does this MR do?

relates to: gitlab-org/gitlab#20050 (closed) When defining paths within a Grape API, we can use wildcard segments. Example, *wildcard is the wildcard segment in the following example:

get 'oranges/:id/bananas/*wildcard/ananas' do
  'test'
end

There is an issue currently with wildcard segments and grape-path-helpers. See https://gitlab.com/gitlab-org/grape-path-helpers/issues/2.

The issue comes from the regexp used to split a route path. It includes * and thus when splitting a route path, wildcard segments will lose their * -> they will be considered as a static part of the url. As they are considered a static part, the following can be observed:

  1. the helper name is wrong. (the wildcard segment name appears in the helper name)
  2. we can't set the value of the wildcard segment when using the helper.

This MR tries to solve 2. while keeping the "buggy" behavior for 1.

Technical notes

  • grape-path-helpers is used quite extensively in GitLab. In order to not break things, the wildcard segment support is optional (default to false).
  • When using a path helper function, an optional argument is now available to indicate if we want to set wildcard segments or not.
  • Since we are solving 2. but not 1. (see above), we will need two regexps: the original and one that doesn't include *.
  • #path_segments_with_values is used when the helper name is built (1.) and when the helper is used to generate a url (2.)
  • #path_segments_with_values has now an optional parameter to decide if wildcard segments are included or not.
  • The proper regexp is selected according to this optional parameter.
  • This is not ideal but again, the focus of this MR is to add an optional support for wildcard segments and keep the original behavior for all the other cases.

Questions / Doubts

  • On the original regexp, I fail to see why we need a ? right at the end of the regexp: \??\*
Edited by Tim Rizzi

Merge request reports