OpenAPI 规范 - 鉴别器和 oneOf 的使用 - 频谱列表

发布于 2025-01-15 10:23:33 字数 2771 浏览 4 评论 0原文

使用 oneOf 的 OpenAPI 鉴别器

使用带有 openApi 规范的鉴别器并使用 Spectral 进行 linting 的最小示例。

错误消息:

~/git/openapi_discriminator/openapi/v1/api.yaml
 22:23  error  oas3-valid-media-example  "example" property must match exactly one schema in oneOf  paths./discriminatortest.get.responses[200].content.application/json.example

具有简单 GET 方法的后台

OpenAPI 架构,该方法可以返回不同类型的 Animal

定义了Animal 的子类,它可以是ChickenDog

动物唯一拥有的属性是。 鉴别器用于区分,其中两条 code> 和一条 四条 腿。

我的目的

是验证请求响应中的示例是否仅匹配一个模式。

问题

我认为使用鉴别器可能意味着任何具有 two legs 的东西都是 Chicken ,任何具有 four >legs 是一只

我是否弄错了,拥有两条仍然是合法的,这就是它出错的原因?

我可以将其更改为 anyOf 但鉴别器就没用了?

代码

代码仓库 - openapi_discriminator

openapi_discriminator/openapi/v1/api.yaml

openapi: "3.0.3"
info:
  title: Open API Discriminator Example
  version: "v1"

tags:
  - name: discriminator

paths:
  /discriminatortest:
    get:
      tags:
        - discriminator
      summary: Example using discriminator
      description: "Demonstrate a minimal example"
      responses:
        "200":
          description: Created
          content:
            application/json:
              schema: {$ref: "schemas.yaml#/components/schemas/Animal"}
              example:
                legs: "two"

openapi_discriminator/openapi/v1/schemas.yaml

openapi: "3.0.3"

components:
  schemas:

    Animal:
      type: object
      discriminator:
        propertyName: legs
        mapping:
          two: Chicken
          four: Dog
      oneOf:
        - $ref: '#/components/schemas/Dog'
        - $ref: '#/components/schemas/Chicken'

    Chicken:
      type: object
      required:
        - legs
      properties:
        legs:
          type: string

    Dog:
      type: object
      required:
        - legs
      properties:
        legs:
          type: string

openapi_discriminator/openapi/.spectral.yml

extends: spectral:oas
rules:
  info-contact: false
  info-description: false
  oas3-api-servers: false
  openapi-tags: true
  operation-tags: true
  operation-operationId: false
  operation-description: true

运行 linting 命令:spectral lint "openapi/v1/api.yaml" --ruleset openapi/.spectral.yml

OpenAPI Discriminator using oneOf

A minimal example of using a discriminator with an openApi spec and linting with Spectral.

Error message:

~/git/openapi_discriminator/openapi/v1/api.yaml
 22:23  error  oas3-valid-media-example  "example" property must match exactly one schema in oneOf  paths./discriminatortest.get.responses[200].content.application/json.example

Background

OpenAPI schema with simple GET method which can return different types of Animal.

A subclass of Animal is defined which can either be a Chicken or a Dog.

The only property Animals have are legs.
A discriminator is used to distinguish between a Chicken or Dog where a Chicken has two legs and a Dog has four legs.

Aim

I was to verify that the example in a request response matches only one schema.

Question

I thought using a discriminator might mean that anything with two legs is a Chicken and anything with four legs is a Dog.

Am I mistaken and it is still legitimate for a Dog to have two legs, and this is why it's erroring?

I could change it to anyOf but then the discriminator has no use?

Code

Code repo - openapi_discriminator

openapi_discriminator/openapi/v1/api.yaml:

openapi: "3.0.3"
info:
  title: Open API Discriminator Example
  version: "v1"

tags:
  - name: discriminator

paths:
  /discriminatortest:
    get:
      tags:
        - discriminator
      summary: Example using discriminator
      description: "Demonstrate a minimal example"
      responses:
        "200":
          description: Created
          content:
            application/json:
              schema: {$ref: "schemas.yaml#/components/schemas/Animal"}
              example:
                legs: "two"

openapi_discriminator/openapi/v1/schemas.yaml:

openapi: "3.0.3"

components:
  schemas:

    Animal:
      type: object
      discriminator:
        propertyName: legs
        mapping:
          two: Chicken
          four: Dog
      oneOf:
        - $ref: '#/components/schemas/Dog'
        - $ref: '#/components/schemas/Chicken'

    Chicken:
      type: object
      required:
        - legs
      properties:
        legs:
          type: string

    Dog:
      type: object
      required:
        - legs
      properties:
        legs:
          type: string

openapi_discriminator/openapi/.spectral.yml

extends: spectral:oas
rules:
  info-contact: false
  info-description: false
  oas3-api-servers: false
  openapi-tags: true
  operation-tags: true
  operation-operationId: false
  operation-description: true

Run linting command: spectral lint "openapi/v1/api.yaml" --ruleset openapi/.spectral.yml

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里南柯 2025-01-22 10:23:33

考虑到您很久以前就问过这个问题,不确定您是否仍在寻找答案。这里的问题是您没有明确声明腿的可用值。你知道,我也知道狗有四条腿,鸡有两条腿,但模式却没有。
换句话说,判别器必须仅根据 propertyName 的值进行判别,因此示例必须匹配该值。

添加enum值将解决您的问题:

openapi: "3.0.3"

components:
  schemas:

    Animal:
      type: object
      discriminator:
        propertyName: legs
        mapping:
          two: Chicken
          four: Dog
      oneOf:
        - $ref: '#/components/schemas/Dog'
        - $ref: '#/components/schemas/Chicken'

    Chicken:
      type: object
      required:
        - legs
      properties:
        legs:
          type: string
          enum:
            - two

    Dog:
      type: object
      required:
        - legs
      properties:
        legs:
          type: string
          enum:
            - four

此外,mapping现在对您来说可能不太有用:

          two: Chicken
          four: Dog

取决于您的用例,例如,如果您正在生成架构中的文档,这将意味着您生成的文档显示两个选项(“两个”和“四个”)。所以也许你可以考虑这样做:

          chicken: Chicken
          dog: Dog

Not sure if you're still looking for an answer, considering you asked this so long ago. The problem here is that you haven't explicitly declared the available values for legs. You know and I know that a dog has four legs and a chicken has two legs, but the schema doesn't.
Put another way, the discriminator has to discriminate based only on the value of the propertyName, so that is what the example has to match.

Adding in the enum values will fix your problem:

openapi: "3.0.3"

components:
  schemas:

    Animal:
      type: object
      discriminator:
        propertyName: legs
        mapping:
          two: Chicken
          four: Dog
      oneOf:
        - $ref: '#/components/schemas/Dog'
        - $ref: '#/components/schemas/Chicken'

    Chicken:
      type: object
      required:
        - legs
      properties:
        legs:
          type: string
          enum:
            - two

    Dog:
      type: object
      required:
        - legs
      properties:
        legs:
          type: string
          enum:
            - four

Also, the mapping is probably not so useful for you right now:

          two: Chicken
          four: Dog

Depending on your use case, e.g. if you're generating documentation from the schema, this will mean your generated docs show two options ("two" and "four"). So perhaps you might consider making it:

          chicken: Chicken
          dog: Dog
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文