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

Issue with undefined core function and custom extensions #2495

Open christosgkoros opened 1 year ago

christosgkoros commented 1 year ago

Describe the bug When searching for custom extensions non-existence under $paths.*.* (verbs) any custom extension (starting with x-) under apath.* (resource name) will trigger the rule

To Reproduce

  1. Given this OpenAPI/AsyncAPI document
    
    openapi: 3.0.3
    info:
    version: 1.0.0
    title: Valid Definition
    description: A definition to perform positive test on the full ruleset
    contact:
    name: Test
    email: test@test.com
    servers:
    - url: "https://ruleset-beta.tech"

paths: /resource: x-test: test

and the given rule:

rules: test_verb_ext: formats:

Expected behavior No error since x-test is not mentioned in the ruleset.

Environment (remove any that are not applicable):

Additional context Add any other context about the problem here.

arno-di-loreto commented 1 year ago

I did some tests, and it seems that if given returns an atomic like a string (so not an object), then.field is ignored, and the function is applied to the atomic value.

In the example above, the given path returns the value of x-test, which is a string "test", so the field.then is ignored and the undefined function is applied to this value, so an issue is detected. If we replace the value of x-test with an object, as shown below, the bug doesn't occur. In that case, the returned value is value: value an object, so field is used and as this object doesn't contain the x-custom-extension property, no issue is detected (as expected).

openapi: 3.0.3
info:
  version: 1.0.0
  title: Valid Definition
  description: A definition to perform positive test on the full ruleset
  contact:
    name: Test
    email: test@test.com
servers:
  - url: "https://ruleset-beta.tech"

paths:
  /resource:
    x-test: 
      value: value

The expected behavior would be "if the value found by the given path is not an object and then.field is defined, the then.function is not executed".

Temporary workaround: In that specific case, as this rule aims to ensure that the extension is not present, it can be modified as follow (move the value of field in the given path) to avoid the bug.

rules:
 test_verb_ext:
  formats:
   - oas3
  given: $.paths[*][*].x-custom-extension
  then:
   function: undefined
  message: '{{error}}'
  description: test verb custom extension
  severity: warn