Redocly / redocly-cli

⚒️ Redocly CLI makes OpenAPI easy. Lint/validate to any standard, generate beautiful docs, and more.
https://redocly.com/docs/cli/
MIT License
913 stars 139 forks source link

Lint rules for examples require exclusiveMinimum to be number in openapi 3.0.x #1610

Open gtsystem opened 3 months ago

gtsystem commented 3 months ago

Describe the bug

The linter is returning the error Example validation errored: exclusiveMinimum value must be ["number"], however in openapi 3.0.x exclusiveMinimum must be of type boolean, not number.

To Reproduce Steps to reproduce the behavior:

  1. Given the following openapi file:
openapi: 3.0.2
info:
  title: Example API v2
  description: |-
    An  example API.
  contact: {}
  version: 2.0.0

paths:
  /myPath:
    get:
      summary: my custom path
      operationId: getMyPath
      parameters:
        - $ref: '#/components/parameters/a'
      responses:
        '200':
          description: Response description
          content:
            application/json:
              schema: {}

components:
  parameters:
    a:
      name: a
      in: header
      description: Example 
      schema:
        type: integer
        minimum: 10
        exclusiveMinimum: true
        example: 11
  1. And the lint configuration:

    rules:
    no-invalid-schema-examples: error
  2. Run this command npx @redocly/cli lint openapi-example.yml --config openapi-example.conf.yml --format=stylish

  3. See error

(node:53722) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
validating /.../openapi-example.yml:
  30:9  error    no-invalid-schema-examples  Example validation errored: exclusiveMinimum value must be ["number"].

openapi-example.yml: validated in 12ms

❌ Validation failed with 1 error.
run `redocly lint --generate-ignore-file` to add all problems to the ignore file.

Expected behavior

The validation pass, because in openapi 3.0.x exclusiveMinimum MUST be of type boolean and is defined correctly. This is different in openapi 3.1.0, but the specification provided is for 3.0.2.

Redocly Version(s)

1.17.1

Node.js Version(s)

22.2.0

Additional context

Same issue is present using no-invalid-parameter-examples rule. See also changes between 3.0.x and 3.1.0: https://www.openapis.org/blog/2021/02/16/migrating-from-openapi-3-0-to-3-1-0

jeremyfiel commented 3 months ago

this is happening because the rule is written with OAS 3.1.0 types import. Either this rule should be modified to only work for 3.1.x descriptions with examples keyword, or it should be modified to import the correct Schema node type for the version being processed.

https://github.com/Redocly/redocly-cli/blob/a4adc7a17517df9c478154e1a830af37a3f66c5c/packages/core/src/rules/common/no-invalid-schema-examples.ts#L2

tatomyr commented 2 months ago

@jeremyfiel not exactly. The issue comes from AJV validating the schema. However, from the context of the schema it's not obvious it's an older draft.

@gtsystem just to point out, there's not much value in using exclusiveMinimum for integers. The issue is still there for numbers though.

jeremyfiel commented 2 months ago

When I looked, the impression I have is It's validating against the 3.1 type keyword

Ajv doesn't output the error message in the format seen here. That's from the redocly.ts type definition

lornajane commented 2 months ago

I see that this changed between OpenAPI 3.0 (exclusiveMinimum and exclusiveMaximum are booleans) and OpenAPI 3.1 (they are numbers). I can only recommend that you turn off this rule for now.

tatomyr commented 2 months ago

Alternatively, those particular errors could be suppressed by generating the ignore file: https://redocly.com/docs/cli/commands/lint#generate-ignore-file