Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ordering of matches when no path is concrete #2356

Open
JonnySpruce opened this issue Sep 21, 2020 · 5 comments
Open

Ordering of matches when no path is concrete #2356

JonnySpruce opened this issue Sep 21, 2020 · 5 comments
Labels
clarification requests to clarify, but not change, part of the spec request matching Matching requests to URL templates, media types, etc.

Comments

@JonnySpruce
Copy link

JonnySpruce commented Sep 21, 2020

I'm a contributor for an OpenAPI Validator project, and one of our users has raised an issue that their the spec is not matching in the order they would expect (you can see the issue here).

The issue effectively boils down to the user expecting a path with just one templated segment to be matched over a path with more than one templated segment, i.e.:

/{id1}/{id2}/{id3}
/resource/{id}/new <- Expect this one to be picked

With a request path like: /resource/1/new.

Currently we are matching simply based on the order of the paths as defined by the user in their specification file (which is probably not the correct approach). My question is whether it is specified anywhere how to resolve/match multiple templated matches?

Looking at the relevant part of the OpenAPI3 spec, while it discusses preferring a concrete path over a path which uses templates, from the spec I couldn't see how paths would be chosen if both match the request, and both use templates in the path. Since the examples in the spec are fairly short, I wasn't sure if the above example would fall into the ambiguous category or not.

I did manage to find a discussion in your issues here which hints at the fact most people would match a path with more concrete segments, over one with less. Since we are a validation tool, I would prefer to be able to apply concrete rules.

Do you have any advice/further reading on matching paths like the above example?

@JonnySpruce JonnySpruce changed the title Ordering of matches when no URL is concrete Ordering of matches when no path is concrete Sep 21, 2020
@rwalle61
Copy link

rwalle61 commented Sep 25, 2020

Any comments on this?

Basically, if a request is made with path /foo/bar, and an OpenAPI document has these 2 paths,

/{a}/{b}
/foo/{c}

which OpenAPI path does the request match? (Or is it ambiguous, like one of these examples from the official OpenAPI Specification?)

Thanks!

@rwalle61
Copy link

Thanks for adding the Review tag to this, did it get discussed at the review meeting?

@jchook
Copy link

jchook commented Nov 14, 2020

...from the spec I couldn't see how paths would be chosen if both match the request, and both use templates in the path.

Your example may violate this stipulation:

Templated paths with the same hierarchy but different templated names MUST NOT exist as they are identical

If it doesn't, it may help to expand that "MUST NOT" to avoid other types of ambiguity.

Example 1

  • /resource/{id}
  • /resource/new

This seems ambiguous, even with smart matching. What if you used short codes for IDs and had a resource with id = new?

Example 2

  • /resource/edit/{id}
  • /resource/{id}/view

Does this count as "two templated paths with the same hierarchy but different templated names"? What does "same hierarchy" mean?

Example 3

  • /resource/new
  • /resource/x-{id}

Can you do partially templated path segments like this? If so, that may further complicate matters (see #1970).

Example 4

  • /resource/new
  • /resource/view/{id}

This solves the matching ambiguity problem in the other 3 examples simply by avoiding path ambiguity altogether.

@jchook
Copy link

jchook commented Nov 14, 2020

It seems that, even though we can conceive of tricky ways to resolve various types of ambiguity, avoiding it entirely would strengthen the rigorousness of the spec, ensure better interoperability of community tools, simpler/easier implementations, and ultimately simpler/faster routing.

@rwalle61
Copy link

rwalle61 commented Nov 15, 2020

Thanks @jchook, yea I agree that whatever the decision, users should avoid defining paths in their OpenAPI specs that are close to such ambiguity.

Still, a user of our dev tool wants to do it anyway, so it'd be good to know which of the 2 paths we should officially match. I.e., what is the official definition of paths with "same hierarchy"?

Re the 4 examples, I believe Example 1 is covered by

When matching URLs, concrete (non-templated) paths would be matched before their templated counterparts.

So the concrete path would be matched.

As you say, there's no ambiguity in example 4.

I guess example 3 would be two paths of "same hierarchy", like the example in the spec of 2 paths that are too similar (although yes it's unclear)

Example 2 is spot on - are the two paths of "same hierarchy"?

@handrews handrews added clarification requests to clarify, but not change, part of the spec request matching Matching requests to URL templates, media types, etc. labels Jan 28, 2024
@handrews handrews removed the review label Apr 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clarification requests to clarify, but not change, part of the spec request matching Matching requests to URL templates, media types, etc.
Projects
None yet
Development

No branches or pull requests

5 participants