wework / speccy

Well Spectually 🤓 Enforce quality rules on your OpenAPI 3.0.x specifications.
http://speccy.io
MIT License
828 stars 78 forks source link

how to combine $ref and default? #469

Open maxlapshin opened 3 years ago

maxlapshin commented 3 years ago

Field in API can refer to some component and have default value. Speccy doesn't allow combining allOf $ref and default

Detailed description

For example:

components:
    frame_video_pix_fmt:
      enum:
        - yuv420p
        - yuvj420p
        - yuv422p
        - yuv444p
...

        pix_fmt:
          allOf:
            - $ref: "#/components/schemas/frame_video_pix_fmt"
          default: yuv420p

It is illegal to specify

        pix_fmt:
          $ref: "#/components/schemas/frame_video_pix_fmt"
          default: yuv420p

because $ref must rewrite completely current object and does not allow any siblings. Using allOf is a working hack to allow having siblings for $ref, but speccy doesn't allow default with $ref

I suppose that speccy should either validate default against referenced component, either do not complain about having default without type:

$ speccy lint   http://localhost:8080/flussonic/api/v3/schema
Specification schema is invalid.

#/components/schemas/track_info/properties/pix_fmt
expected Object {
  allOf: Array [ Object { $ref: '#/components/schemas/frame_video_pix_fmt' } ],
  default: 'yuv420p',
  deprecated: true
} to have property type

Your environment

$ speccy --version
0.11.0
$ node --version
v14.7.0
$ uname -a
Darwin maxbook-m1.e 20.6.0 Darwin Kernel Version 20.6.0: Wed Jun 23 00:26:27 PDT 2021; root:xnu-7195.141.2~5/RELEASE_ARM64_T8101 arm64
maxlapshin commented 3 years ago

Please, consider updating dependency from oas-kit:

https://github.com/Mermade/oas-kit/blob/main/packages/oas-validator/index.js#L220

It has laxDefault option now that allows to pass mismatch between default and type options