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.45k stars 235 forks source link

Error not picked up when an extra field which should be object is defined as string. #2087

Open KiranReddy0808 opened 2 years ago

KiranReddy0808 commented 2 years ago

Describe the bug Given a path to value which is not an object (string, number) from given in rule. When a field is defined for the rule. There is no error being triggered saying the field is not defined.

To Reproduce

  1. OAS
    openapi: 3.0.0  
    info:  
        title: Book Library API (OAS3.0)  
        version: '1.10' 
        apiname: api-two 
        description: Sample OAS API  
  2. Rule
    oas-title-of-apiname:
        description: apiname should have a title property.
        message: apiname doesn't have a title property
        severity: warn
        given: $.info.apiname
        then:
           field: title
           function: defined

Expected behavior An error should be expected that there is no field named "title" under path info.apiname. There is no error because getLintTargets(targetValue, field) returns the original targetValue and path as empty array (which is not of type void 0 for defined function to pickup) when typeof targetValue is not object.

image

Rules: https://github.com/KiranReddy0808/basic-oas-spec/blob/main/.spectral.yaml API: https://github.com/KiranReddy0808/basic-oas-spec/blob/main/api-two.yaml

GitHub Action Run: https://github.com/KiranReddy0808/basic-oas-spec/actions/runs/1967689613

Environment (remove any that are not applicable):

Additional context As extra fields are allowed in OAS. Incorrect definition of those fields will go through. I'm not sure if it's expected behaviour. So please close this ticket if it's already discussed or is correct!

Thanks!

P0lip commented 2 years ago

This is expected behavior, although I admit it's somewhat confusing. It's certainly one of these features I'd love to get changed, but it's very tricky since it's been working this way since the very first release of Spectral, so it'd be a notable breaking change. I'll keep this issue open to keep track of that behavior.

KiranReddy0808 commented 2 years ago

Thanks @P0lip for the answer!