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

OpenAPI 2.0/3.x.y - validation of Path Templating #3517

Closed
char0n opened this issue Dec 11, 2023 · 2 comments
Closed

OpenAPI 2.0/3.x.y - validation of Path Templating #3517

char0n opened this issue Dec 11, 2023 · 2 comments

Comments

@char0n
Copy link
Member

char0n commented Dec 11, 2023

We are currently not able to validate and resolve Path Templating. I have created low level parser, validator and resolver library for OpenAPI Path Templating here: https://github.com/char0n/openapi-path-templating

openapi-path-templating is applicable for: OpenAPI 2.0 and 3.x.y.

We want to use it for every case related to Path Templating.

Warning

openapi-path-templating library doesn't handle computing appropriate Parameter Objects to be resolved against the template expression.


I actually see two different linting rules here and I do understand and approach them differently (correct me if I'm wrong).

Rule 1

We want a linting rule that validates if the template expressions defined within the URL are all present in the appropriate Parameter Objects.

Rule 2

We want a linting rule that validates that user did not forget to define template expressions for all the appropriate Parameter Objects.

For the purpose of this implementation I will assume we're talking about Rule 1.


There are multiple layers that we need to implement to achieve the final goal described in Rule 1.

Low level Path Templating handling

I've already had a code for this on my local dev computer. I've consolidated the code and pushed with (along with the documentation) to https://github.com/char0n/openapi-path-templating. The library is not yet released, I will release it probably during today. The library handles all the low level complexities and corner cases of parsing, validating and resolving the Path Templating.

The library can distinguish if the URL contains Path Templating or not.

image

Above is an excerpt from OpenAPI 3.x.y spec, which tells us how to handle escaping of "generic syntax" characters

High level Path Templating handling

image

Above OpenAPI 3.x.y excerpt tells us how to handle Path Templating

Collecting Parameter Objects

So, what we need to do it to collect all Parameter Objects from Path Item Object directly mapped to URL with Path Templating and all Parameter Objects from every Operation Object mapped from Path Item Object under the HTTP verb. (I'll help you with querying those out from ApiDOM later).

When we have all the Parameter Objects we have to apply following algorithms defined by OpenAPI:

Applies to both PathItem.paramters and Operation.parameters

The list MUST NOT include duplicated parameters.

A unique parameter is defined by a combination of a name and location.

Applies to Operation.parameters

If a parameter is already defined at the Path Item, the new definition will override it but can never remove it.

So basically we'll have to create a unique function specific to list of Parameter Objects or reuse the already implemented logic of normalize-parameters refractor plugin which already does this.

Matching Path Templating with Parameter Objects

Now we need to use https://github.com/char0n/openapi-path-templating to parse the relative URI Reference (with possible Path Templating) and match the parsed template-expression tokens against the collected Parameter Object.


And we must not forget the exception described in the OpenAPI excerpt, already mentioned above. If the Path Item Object is empty, the the Path Templating doesn't need to be matched with path template expressions. We'll just skip it. Emptiness of the Path Item Object is defined by lack of all fixed fields (Specification Extensions MAY be present).

OpenAPI excerpt
image

@char0n
Copy link
Member Author

char0n commented Dec 11, 2023

openapi-path-templating just released as https://www.npmjs.com/package/openapi-path-templating

@char0n
Copy link
Member Author

char0n commented Dec 12, 2023

We need to attach the metadat to URL Template (keys in Paths Object). That way our linting function will receive element, which represents the URL Template.

Then in linting function we can do following:

element.parent === MemberElement
element.parent.value === Path Item Object

And that basically is what we want, to be able to access contextual Path Item Object, depending on the current URL Template.

This applies to OpenAPI 2.0/3.0.x/3.1.0


Addressed by #3532

char0n added a commit that referenced this issue Dec 13, 2023
char0n added a commit that referenced this issue Dec 18, 2023
This linting rule that validates if the template expressions defined
within the path template are all present in the appropriate Parameter Objects.

Refs #3517

Co-authored-by: Vladimír Gorej <vladimir.gorej@gmail.com>
@char0n char0n closed this as completed Dec 18, 2023
char0n added a commit that referenced this issue Jan 2, 2024
No matches are now registered for template expressions
defined inside query parameters.

URLs with fragments are now supported, but no matches
are registered for template expressiones defined inside fragments.

Refs #3517
char0n added a commit that referenced this issue Jan 2, 2024
No matches are now registered for template expressions
defined inside query parameters.

URLs with fragments are now supported, but no matches
are registered for template expressiones defined inside fragments.

Refs #3517
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants