Multi-segment path parameters in Swagger 2.0—interested in a patch?
Short version
We'd like to validate an API with a dynamic, multi-segment path in it.
e.g.
#!
/root/subFolder/subFolder/subFolder
OR
#!
/root/a/b/c
Where the depth of the subfolders is unknown. In our spec, we write a path
#!
/root/{path}
Where we want {path} to match /a/b/c
Longer version
You can take a look at this ticket on Open API 3 and the associated comments: https://github.com/OAI/OpenAPI-Specification/issues/1459
If you read all the way through you'll see
- What's requested there is similar to what is summarized above
- There is some consideration for including the feature in OpenAPI 3.1 though there are a couple of different proposed approaches.
Problem and Solution today
While it is nice to know this might become an officially supported OpenAPI 3.1 feature in the future, we have a problem today because swagger-request-validator cannot match dynamic multi-segment paths. ApiOperationResolver assumes that the request path to be matched must have the same number of segments as the path in the specification and therefore it always reports that no paths match.
I have developed a patch and test cases which modify ApiOperationResolver as follows:
- If the spec path ends in a parameter (e.g. /root/{path}), and,
- the incoming request matches otherwise (so /root/a/b matches but /foo/a/b does not), and,
- this is the most specific match (using the logic for 'most specific' recently introduced)
-> Then the path is a match and the operation resolves
Of course, in OpenAPI 2.0 there is no way in the specification itself to signal that we want this kind of "loose" matching. Therefore we allow the developer using ApiOperationResolver to explicitly specify which kind of matching they want in their Java code.
If this patch is accepted it will hopefully position swagger-request-validator to be able to quickly support OpenAPI 3.1 if this feature becomes part of the spec.
Please let me know if this sounds interesting. I will need to do some work to clear the patch for release so interesting in feedback first.
Comments (7)
-
-
reporter Great. I may need a couple of days to get it together.
-
reporter Pull request created:
-
reporter Ping? Any thoughts on this?
-
The initial work around supporting dynamic path matching is available in v1.4.5
-
reporter Thanks!
-
I believe I am getting a "validation.request.path.missing" error for a Swagger API path "/apis/v1/myapi/filetoken/{filepath}" and calling "/apis/v1/myapi/filetoken/test/wibble/wobble/file1.csv".
I am using v2.0.2 of the tool and OpenApi v3.0.
Reading through this issue and the merged changeset, is there something I need to do to avoid getting this error? Thanks : )
- Log in to comment
Thanks for taking the time to explain the situation so well. I'm definitely open to supporting this proposal.
My main areas of concern would be around:
x-
extension directive in the API spec itself or via a config option when setting up the validator)But it sounds like you've considered both of these.
Id be happy to review a PR whenever you're able to raise one (to merge though you need to sign the CLA - see the project README for details).
Cheers.