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

Add rule exceptions per operation or path #1558

Closed
roubles opened this issue Mar 25, 2021 · 4 comments
Closed

Add rule exceptions per operation or path #1558

roubles opened this issue Mar 25, 2021 · 4 comments

Comments

@roubles
Copy link

roubles commented Mar 25, 2021

User story.
As a user, I want to be able to add exceptions, per operation, to certain rules in the open API spec to ignore failures.

This is related to, but different from the ask in #747 which was closed by providing the capability to have exceptions in rulesets via #939. The solution for that issue turns off the issue for the entire API spec.

We are asking for the rulesets to apply on the entire API spec, except on certain operations/APIs that are explicitly marked as exceptions. This is very similar to eslint's // eslint-disable-line no-use-before-define.

Is your feature request related to a problem?
Yes, today we do not have the flexibility to not apply certain rules on certain APIs. We can only turn off all rules using #939 or —skip-rule. This is not ideal because these options turn off the rule on the entire API spec. We need to be able to turn off the rule per* operation.

Describe the solution you'd like
The solution we'd like is a comment that can be added to the yaml or the json. In the following example, we do not want spectral to alert us on the rule operation-default-response to apply on a specific operation (post):

  # spectral-lint-disable operation-default-response
  post:
      summary: Create a pet
      operationId: createPets
      tags:
        - pets
      responses:
        "201":
          description: Null response

Note that this rule will still fire on all other operations/APIs in this API spec. You can see how this comment can be moved to the path level and apply to all operations on a certain path as well.

This works for yaml files, since yaml supports comments. However, json does not support comments. Open to suggestions on other approaches to achieve this usecase of turning off rules per operation.

@kscheirer
Copy link

kscheirer commented May 13, 2021

for json how about a "x-spectral-ignore": "operation-default-response". Not as elegant as yaml, but close enough?

        "/some/path": {
            "post": {
                "tags": [
                    "pets"
                ],
                "x-spectral-lint-disable": "operation-default-response",
                "summary": "Create a pet",
                "operationId": "createPets",
                "requestBody": { ...

@philsturgeon
Copy link
Contributor

philsturgeon commented Jun 2, 2022

This use case is currently covered by Overrides (#1021), which is done from the ruleset instead of messing with the OpenAPI document. Hopefully that's close enough, but if you'd like to try and implement this you could take a swing in a PR or join us on Discord for help implementing it.

@silverwind
Copy link

silverwind commented Oct 24, 2022

Per-Line or per-block rule diablement via YAML comments seems more natural than having to construct the property path in the config. Parsers generally ignore YAML comments so it is safe to do as well. For example

Disable rules for single line:

foo: true # spectral-disable-line rule1 rule2

Disable rules for all following lines:

# spectral-disable rule1 rule2
foo: true
# spectral-enable rule1 rule2

@philsturgeon
Copy link
Contributor

As I said above, this use case is being handled with Overrides, but if you would like to take a swing at implementing it the way you'd like that would be very welcome.

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

4 participants