gpt4 book ai didi

api - Swagger API 文档 XOR

转载 作者:行者123 更新时间:2023-12-04 02:15:09 26 4
gpt4 key购买 nike

我想知道是否有可能在 swagger.yaml/json 中使用 XOR
例如,如果我有这样的事情:

PostableEntity:
properties:
first_property:
type: string
second_property:
type: string
minLength: 1
description: foo
third_property:
type: number
required:
- third_property

现在,如果 second_property 未设置,我怎样才能使 first_property 成为必需的,反之亦然?

最佳答案

条件属性无法建模,但您可以使用多态来寻求解决方法。您可以创建一个父模型 Pthird_property和两个子模型( C1C2 ),一个( C1 )和 first_property另一个( C2 )与 second_property .使用 allOf关键字和 discriminator字段,您将获得具有属性形式的模型 P+C1P+C2 .

见章组合和继承(多态)documentation .

Swagger allows combining and extending model definitions using the allOf property of JSON Schema, in effect offering model composition. allOf takes in an array of object definitions that are validated independently but together compose a single object.



提示:

当您的 swagger 编辑器使用 allOf 报告您以下错误时,请不要感到困惑。关键词: 不允许附加属性,但设置了属性

目前github上已经报告了一个bug,见 this link .

关于api - Swagger API 文档 XOR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34722634/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com