swagger-api / swagger-parser

Swagger Spec to Java POJOs
http://swagger.io
Apache License 2.0
781 stars 527 forks source link

3.1.0 parsing of schemas containing const throws errors #1975

Closed spacether closed 10 months ago

spacether commented 1 year ago

v3.1.0 parsing of a document that contains valid const values is incorrectly throwing errors

swagger-parser version: 2.1.1

For this spec:

openapi: 3.1.0
servers:
- url: https://someserver.com/v1
info:
  title: openapi 3.1.0 sample spec
  version: 0.0.1
  description: sample spec for testing openapi functionality, built from json schema
    tests for draft2020-12
tags: []
paths: {}
components:
  schemas:
    ConstValidation:
      $schema: https://json-schema.org/draft/2020-12/schema
      const: 2
    ConstWithObject:
      $schema: https://json-schema.org/draft/2020-12/schema
      const:
        foo: bar
        baz: bax
    ConstWithArray:
      $schema: https://json-schema.org/draft/2020-12/schema
      const:
      - foo: bar
    ConstWithNull:
      $schema: https://json-schema.org/draft/2020-12/schema
      const: null
    ConstWithFalseDoesNotMatch0:
      $schema: https://json-schema.org/draft/2020-12/schema
      const:
      - false
    ConstWithTrueDoesNotMatch1:
      $schema: https://json-schema.org/draft/2020-12/schema
      const:
      - true
    ConstWithAFalseDoesNotMatchA0:
      $schema: https://json-schema.org/draft/2020-12/schema
      const:
        a: false
    ConstWithATrueDoesNotMatchA1:
      $schema: https://json-schema.org/draft/2020-12/schema
      const:
        a: true
    ConstWith0DoesNotMatchOtherZeroLikeTypes:
      $schema: https://json-schema.org/draft/2020-12/schema
      const: 0
    ConstWith1DoesNotMatchTrue:
      $schema: https://json-schema.org/draft/2020-12/schema
      const: 1
    ConstWith20MatchesIntegerAndFloatTypes:
      $schema: https://json-schema.org/draft/2020-12/schema
      const: -2.0
    ConstFloatAndIntegersAreEqualUpTo64BitRepresentationLimits:
      $schema: https://json-schema.org/draft/2020-12/schema
      const: 9007199254740992
    ConstNulCharactersInStrings:
      $schema: https://json-schema.org/draft/2020-12/schema
      const: "hello\0there"

When parsing the document, these errors are incorrectly thrown:

 | Error count: 6, Warning count: 0
Errors: 
        -attribute components.schemas.ConstWithArray.const is not of type `string`
        -attribute components.schemas.ConstWithAFalseDoesNotMatchA0.const is not of type `string`
        -attribute components.schemas.ConstWithObject.const is not of type `string`
        -attribute components.schemas.ConstWithATrueDoesNotMatchA1.const is not of type `string`
        -attribute components.schemas.ConstWithFalseDoesNotMatch0.const is not of type `string`
        -attribute components.schemas.ConstWithTrueDoesNotMatch1.const is not of type `string`

This blocks compliance to json schema 2020-12, required by openapi v3.1.0 This came up when writing: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator/pull/241

Related: https://github.com/swagger-api/swagger-parser/issues/1966