apisyouwonthate / style-guide

A shared and somewhat opinionated style guide for everyone to enjoy.
141 stars 20 forks source link

apisyouwonthate.yml unknown-error-format not returning desired output #17

Closed pedrodovale closed 2 years ago

pedrodovale commented 2 years ago

I'm trying to make a custom rule based on the unkown-error-format that can be found here here. Using this Open API document as an example:

openapi-generated.yaml

openapi: 3.0.3
info:
  title: API
  version: 1.0.0
servers:
- url: https://api.com/
paths:
  /:
    get:
      tags: []
      summary: Your GET endpoint
      description: Your GET endpoint
      operationId: get-endpoint
      responses:
        "500":
          description: Error
          content:
            application/json:
              schema:
                properties:
                  errorDescription:
                    type: string

and this rule set:

.spectral.yml

extends:
  - https://raw.githubusercontent.com/openapi-contrib/style-guides/master/apisyouwonthate.yml

rules:
  unknown-error-format: error

When running

spectral lint --ruleset .spectral.yml openapi-generated.yaml

I was expecting an error to be returned, because the 500 response content is application/json and not one of application/vnd.api+json, application/problem+xml, application/problem+json.

Instead no errors are found.

Additionally, I tried using this JSON Path Demo to check if the output array with "application/json" is returned for the path in the rule's given. It does.

Is there a problem with this rule or am I doing something wrong?

Thanks in advance.

savage-alex commented 2 years ago

come chat about it here https://apisyouwonthate.com/community

philsturgeon commented 2 years ago

I've confirmed this is not working, despite the JSON Path being valid. I'll get some of the crew at Stoplight to help figure it out.

philsturgeon commented 2 years ago

The rule thats trouble is:

  no-unknown-error-format:
    description: "Every error response SHOULD support either RFC 7807 (https://tools.ietf.org/html/rfc6648) or the JSON:API Error format."
    formats: [oas3]
    severity: warn
    given: $.paths.[*].responses[?(@property.match(/^(4|5)/))].content.*~
    then:
      function: enumeration
      functionOptions:
        values:
        - application/vnd.api+json
        - application/problem+xml
        - application/problem+json
philsturgeon commented 2 years ago

Fixed in 06bd2f7 thanks to help from @savage-alex