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 Spec Rules wildcard "*" is not accepted as a path param. #2647

Closed
DanCRichards opened this issue Jun 27, 2024 · 2 comments
Closed

OpenAPI Spec Rules wildcard "*" is not accepted as a path param. #2647

DanCRichards opened this issue Jun 27, 2024 · 2 comments

Comments

@DanCRichards
Copy link

Describe the bug
The spectral:oas ruleset does not allow for {*} to be included within the path params.

To Reproduce

  1. Consider the following endpoint which has been defined by fastify-swagger.
openapi: 3.0.3
info:
  title: "Wildcard Issue API"
  version: 4.2.0
  description: :) 
components:
  schemas: {}
paths:
  /documentation/static/{*}:
    head: &a1
      parameters:
        - schema:
            type: string
          in: path
          name: "*"
          required: true
      responses:
        "200":
          description: Default Response
    get: *a1
  1. Run this CLI command npx spectral lint openapi.yml -D
  2. See error error path-params Parameter "*" must be used in path "/documentation/static/{*}". paths./documentation/something/{*}.head.parameters[0]

Expected behavior
I would expect that {*} is an accepted path param
Screenshots
image

Problematic Code
All of this code is in oasPathParam.js

Code raising error. here

const ensureAllDefinedPathParamsAreUsedInPath = (path, params, expected, results) => {
    for (const p of Object.keys(params)) {
        if (!params[p]) {
            continue;
        }
        if (!expected.includes(p)) {
            const resPath = params[p];
            results.push(generateResult(`Parameter "${p}" must be used in path "${path}".`, resPath));
        }
    }
};

In the function above the expected argument is empty for the endpoint in question. The function is called by oasPathParams(path) function here

function oasPathParam(paths) {
         /*
            ... 
         */

        const pathElements = [];
        let match;
        while ((match = pathRegex.exec(path))) {
            const p = match[0].replace(/[{}?*;]/g, '');
            if (pathElements.includes(p)) {
                results.push(generateResult(`Path "${path}" must not use parameter "{${p}}" multiple times.`, ['paths', path]));
            }
            else {
                pathElements.push(p);
            }
        }

       /*
          ...
      */

       ensureAllExpectedParamsInPathAreDefined(path, definedParams, pathElements, operationPath, results);
}

When running the endpoint path in question isn't validated against the regex and isn't added to the pathElements array, which is then passed through as the 'expected' path params object which raises the error.

The regex defined here doesn't allow for the {*} to be defined. It is only looking for alphanumber characters and -.

Proof that this endpoint does not pass the regex can be found here

This can be fixed by adding * to the regex.

Additional context
This error relates to fastify-swagger generating the documentation/static/{} endpoint for some other people this may be /docs/static/{}

Any one dealing with this error can modify their .spectral.yaml file to be.

extends: "spectral:oas"
overrides:
  - files:
      - "**#/paths/~1documentation~1static~1%7B*%7D"
    rules:
      path-params: "off"
@DanCRichards DanCRichards changed the title OpenAPI Spec Rules wildcard / {*} not accepted as a path param OpenAPI Spec Rules wildcard "*" is not accepted as a path param. Jun 27, 2024
@DanCRichards
Copy link
Author

I have started to work on this.

https://github.com/DanCRichards/spectral/tree/bugfix/2647-OpenAPI-Ruleset-Allow-Wildcard-In-Path

Note my latest commit doesn't take into consideration the fact that a path object should only be either alphanumeric (including -) or a wild card.

I am looking into it but the regex that I am developing is causing failures with other test cases.

@ponelat
Copy link

ponelat commented Aug 23, 2024

There are two pieces.
One, Path Templates are a little vague, but wildcards that greedily capture whole paths aren't supported.

Two is that names of parameters, within the curlies {} have to be percent encoded. I don't think this ticket is about naming the path segment * but if it were, then it would need to be... /documentation/static/{%2A} and have a parameter named %2A :D

Going to close this out as expected behaviour.
If folks are interested in weighing in on the Path Template, feel free to head over to https://github.com/OAI/OpenAPI-Specification

@ponelat ponelat closed this as completed Aug 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants