question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Optional and multi-segment path parameters

See original GitHub issue

This is a meta issue representing a number of requests over the years including #1459 #1840 #892

Currently the OpenAPI specification does not allow optional path parameters nor path parameter values that allow characters such as the /. This means that if you had the following API description:

  /myfiles/{mypath}/{filename}:  
    parameters:
      - name: mypath
        type: string
      - name: filename
        type: string

then you would not be allowed to have myPath = “a/path/to/a/file” unless you escaped the forward slash character.

Also, the RFC6570 URI Template /myreports/{reportName}{/nonDefaultFormat} where the last path segment is optional is not supported by OAS.

The primary reason for not supporting these types of APIs is that it creates the potential of an ambiguous match between a URL and the corresponding path item. Some tooling depends on being able to identify the API description for a specific URL. If multiple pathItems match, then some kind of alternate selection algorithm must be defined.

There have been a number of suggestions on how that selection algorithm might work, with varying levels of complexity. The open question is if there is enough community demand to justify the work necessary to find an acceptable solution.

If you have real-world scenarios where adding support for either multi-segment path parameters or optional path segments would make your life easier, please share them in this issue.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:7
  • Comments:22 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
eugene-brightcommented, Apr 20, 2022

I propose to add support of {+path} expression from RFC6570. Please vote on this if you agree!

2reactions
awwrightcommented, Jul 23, 2021

uri-template-router tries to invent a general algorithm for reversing a URI Template and it is, indeed, somewhat complicated, in large part because URI Template isn’t designed for matching.

The first rule I try to follow is a simple, self-evident principle: If X is a strict subset of Y, then match X before Y.

This means that http://example.com/a/b/ will match templates in the following order:

  1. http://example.com/a/{+foo}/ and http://example.com/{+foo}/b/
  2. http://example.com/{a}/{b}/ (requires exactly three slash characters in the path)
  3. http://example.com/{+a}/{+b}/ (requires at least three slash characters in the path)
  4. http://example.com/{+foo}/ (requires at least two slash characters in the path)
  5. http://example.com/{+foo} (the biggest set)

Then for picking between disjoint URI templates, prefer templates that are strict subsets when only looking at the first characters. Therefore, http://example.com/a/{+foo}/ would be matched first because if you compare only the first 20 characters of matching URIs, http://example.com/a is a strict subset of http://example.com/{+foo} (where {+foo} matches zero-or-one characters).

Finally, I match empty strings by default. Because URI Template isn’t a matching syntax (like regexp), it doesn’t have a way for requiring a minimum number of matches. Developers requiring a match are expected to validate the values for the matches, and call Result#next if one of the variables is invalid (e.g. is empty, when a minimum of 1 character is expected).

I recall there being one or two small bugs with the algorithm that I have, I was in the process of mathematically trying to prove that an algorithm implementing all these requirements is computationally possible, and then got sidetracked.

Read more comments on GitHub >

github_iconTop Results From Across the Web

hapi — Multi-Segment/Wildcard Path Parameters - Future Studio
Last week's tutorial on hapi walked you through optional path parameters and showed you how to specify routes leveraging optional parameters ...
Read more >
Describing Parameters - Swagger
In OpenAPI, a path parameter is defined using in: path . The parameter name must be the same as specified ... Query parameters...
Read more >
Multi-segment path parameters in Swagger 2.0—interested in ...
Multi-segment path parameters in Swagger 2.0—interested in a patch? · If the spec path ends in a parameter (e.g. /root/{path}), and, · the ......
Read more >
call v9.0.0 - hapi.dev
It is important to be aware that only the last named parameter in a path can be optional. That means that /{one?}/{two}/ is...
Read more >
How to define a path with two optional parameters in OpenAPI ...
Path parameters are always required, i.e. they must have required: true . Query parameters are send in the query string, e.g. /tasks?page=...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found