使用外部 .yaml 文件在 openapi3.0 yaml 中添加模式定义

Adding schema definition in openapi3.0 yaml using external .yaml file

我有 openapi3.0 YAML 文件,它是根据 openapi3.0 format 编写的,我正在使用 $ swagger-cli validate simple_violation_bool.yaml,它给出了 True/False基于simple_violation_bool.yaml是否有效OpenAPI 3.0

下面是我的 OpenAPI3.0 yaml 文件的内容,即 simple_violation_bool.yaml,我正在尝试使用 $ref: './violation_schema.yaml#/NISE 添加模式定义,但它在 $ swagger-cli validate simple_violation_bool.yaml.[=28 期间出错=]

下面是我的 simple_violation_bool.yaml openapi3.0 YAML 文件。

simple_violation_bool.yaml

openapi: "3.0.0"
info:
  version: 1.0.0
  title: simple_violation_bool
  license:
    name: MIT
  description: |
    Simple violation in simple_violation_bool module
externalDocs:
  description: NISE simple_violation_bool.
servers:
  - url: https://swagger.io/specification/
paths: {}

components:
  schemas:
    NISE:
      type: object
      title: The Root Schema
      required:
        - description
        - id
        - name
        - ports
      properties:
        description:
          type: string
          title: The Descripton Schema
          schema:
            $ref: './violation_schema.yaml#/NISE'
        id:
          type: integer
          title: The Id Schema
          default: 0
          format: int64
          schema:
            $ref: './violation_schema.yaml#/NISE'
        name:
          type: string
          title: The Name Schema
          schema:
            $ref: './violation_schema.yaml#/NISE'
        ports:
          type: array
          title: The Ports Schema
          schema:
            $ref: './violation_schema.yaml#/NISE'
          items:
            type: integer
            title: The items Schema
            default: 0
            schema:
              $ref: './violation_schema.yaml#/NISE'
        value:
          type: object
          title: The Value Schema
          schema:
            $ref: './violation_schema.yaml#/NISE'

这是模式定义文件 violation_schema.yaml 的内容,我试图使用 $ref: './violation_schema.yaml.

添加

violation_schema.yaml

NISE:
  properties:
    description:
      type: string
    id:
      type: integer
    name:
      type: string
    ports:
      type: array
    value:
      type: object

下面是我运行$ swagger-cli validate simple_violation_bool.yaml

后的错误日志

错误日志:

Running swagger-cli validate on simple_violation_bool.yaml .....
Swagger schema validation failed. 
  Data does not match any schemas from 'oneOf' at #/components/schemas/NISE
    Data does not match any schemas from 'oneOf' at #/components/schemas/NISE/properties/value
      Additional properties not allowed: schema at #/properties/value
      Missing required property: $ref at #/properties/value
    Missing required property: $ref at #/components/schemas/NISE

JSON_OBJECT_VALIDATION_FAILED

对这个问题有什么见解吗?

最后,我以同样的方式尝试了一些简单的 openapi3.0 yaml 文件,并且没有任何问题。

我试过的例子

main.yaml

openapi: 3.0.1
info:
  title: ping test
  version: '1.0'
servers:
  - url: 'http://localhost:8000/'
paths:
  /some/ping:
    get:
      operationId: pingGet
      responses:
        '201':
          description: OK
          content:
            application/json:
              schema:
                $ref: './other.yaml#/SomeObj'
components:
  schemas: {}

other.yaml

SomeObj:
  properties:
    s1:
      type: string
    s3:
      type: string

这个简单的例子没有问题。即 $ swagger-cli validate main.yaml 输出:main.yaml 有效

期待早日听到并感谢您花时间研究这个描述性问题。

阿尔斯兰

您尝试从 simple_violation_bool.yaml 引用 violation_schema.yaml 中定义的架构的方式不正确。您不需要针对 simple_violation_bool.yaml 文件中定义的每个 属性 定义 $ref: './violation_schema.yaml#/NISE' 行。此外,由于您在不同的架构文件 violation_schema.yaml 中完全定义架构,因此您不需要在 simple_violation_bool.yaml 中再次定义所有这些值,即 id、名称、端口、值等。让我们尝试以简单的方式理解,假设您没有为架构定义单独的文件,而是从同一文件中引用它,即 simple_violation_bool.yaml,那么在这种情况下,事情将如下所示:

因此,基于以下观察,我更正了您的 yaml 文件,现在它已正确验证。

simple_violation_bool.yaml

openapi: "3.0.0"
info:
  version: 1.0.0
  title: simple_violation_bool
  license:
    name: MIT
  description: |
    Simple violation in simple_violation_bool module
externalDocs:
  description: NISE simple_violation_bool.
  url: "https://simple_violation_bool.net"
servers:
  - url: https://swagger.io/specification/
paths: {}

components:
  schemas:
    ROOT:
      type: object
      title: The Root Schema
      $ref: 'violation_schema.yaml#/NISE'

violation_schema.yaml

  NISE:
      required:
        - description
        - id
        - name
        - ports
      properties:
        description:
          type: string
          title: The Descripton Schema
        id:
          type: integer
          title: The Id Schema
          default: 0
          format: int64
        name:
          type: string
          title: The Name Schema
        ports:
          type: array
          title: The Ports Schema
          items:
            type: integer
            title: The items Schema
            default: 0
        value:
          type: object
          title: The Value Schema

swagger-cli validate simple_violation_bool.yaml

simple_violation_bool.yaml is valid

希望它能为您解决问题。让我知道是否有帮助!!