stoplightio / spectral

A flexible JSON/YAML linter for creating automated style guides, with baked in support for OpenAPI v3.1, v3.0, and v2.0 as well as AsyncAPI v2.x.
https://stoplight.io/spectral
Apache License 2.0
2.43k stars 235 forks source link

Spectral no longer respecting rule overrides #2505

Closed br-tyler-milner closed 1 year ago

br-tyler-milner commented 1 year ago

Describe the bug Running Spectral outputs linter warnings/errors for paths that have rules turned off via the overrides in my .spectral.yaml. The issues seems to have coincided with the update of the spectral-core dependency from version 1.18.1 to version 1.18.2 as my builds were working fine before that update was made.

To Reproduce I created a sample project that demonstrates this issue, which includes detailed steps to reproduce. Abbreviated steps below:

  1. Given this OpenAPI document and this .spectral.yaml.
  2. Run this CLI command: spectral lint Demo.yaml --ruleset .spectral.yaml --verbose
  3. See path-casing and resource-names-plural errors produced for paths./v2.1/users/{userId}, which is configured to disable those rules via rule overrides.

Expected behavior No errors should be output.

Screenshots image

Environment:

Additional context See README in sample project linked above for additional details.

P0lip commented 1 year ago

Hey! JSON pointer in the URI should be percent-encoded, thus you'd need to change overrides to

overrides:
  - files:
      - "reference/Demo.yaml#/paths/~1v2.1~1users~1%7BuserId%7D"
      - "reference/Demo.yaml#/paths/~1user"
    rules:
      resource-names-plural: "off"

  - files:
      - "reference/Demo.yaml#/paths/~1v2.1~1users~1%7BuserId%7D"
    rules:
      path-casing: "warn"

LMK if you have any questions.

br-tyler-milner commented 1 year ago

Thanks, @P0lip! I can confirm that encoding the { } curly braces with %7B and %7D does work. Feel free to close this when ready, but it would be great if the Overrides Spectral documentation page could be updated to incorporate these new encodings as it currently only mentions needing to encode / --> ~1 and ~ --> ~0 and still shows example code that contains unencoded curly braces.

P0lip commented 1 year ago

Good point re the docs - will do.

To give you some extra context behind that change - that's been a bug for a few years in a library that's used across our entire ecosystem. That gap was closed in https://github.com/stoplightio/json/pull/122, but that resulted in a slight change in the behavior which you observed here. Long story short - JSON Pointers (the most common usage in Spectral) must be percent-encoded when used inside of URIs

If you want to get the right pointer, you can run Node.js REPL

node # REPL should open

and then run

require('@stoplight/json').pathToPointer(['abc', 'def', '{}']) # this will output you the valid pointer to use in the URI

Alternatively, go to https://npm.runkit.com/@stoplight/json and run it there

image
P0lip commented 1 year ago

Docs got updated in https://github.com/stoplightio/spectral/commit/69403c1dc1dd78937bf4dbab3c392f03c76e2201