我正在尝试使用swagger-php v2.0记录一个 api 端点,但未正确生成 swagger 文档。
我的用例是我需要从同一个端点接受 2 个不同的有效负载,并且某些键只能存在于给定的上下文中。
至于官方文档,它可以使用discriminator
with来实现allOf
,这是OpenAPI (Swagger) Specification 中给出的示例。
为清楚起见,我将在此处发布缩小版本
{
"definitions": {
"FooBar": {
"type": "object",
"discriminator": "type",
"properties": {
"name": {
"type": "string"
},
"type": {
"type": "string"
}
},
"required": [
"name",
"type"
]
},
"foo": {
"allOf": [
{
"$ref": "#/definitions/FooBar"
},
{
"type": "object",
"properties": {
"unique_to_foo_field": {
"type": "string",
}
},
"required": [
"unique_to_foo_field"
]
}
]
},
"bar": {
"allOf": [
{
"$ref": "#/definitions/FooBar"
},
{
"type": "object",
"properties": {
"unique_to_bar_field": {
"type": "string",
}
},
"required": [
"unique_to_bar_field"
]
}
]
}
}
}
我想将其转换为 swagger-php,我想出的方法如下,但似乎不正确。求建议改正,谢谢。
/**
* @SWG\Definition(
* definition="model:FooBar",
* type="object",
* discriminator="type",
* required={"type", "name"},
* @SWG\Property(property="type", type="string"),
* @SWG\Property(property="name", type="string"),
* )
*
* @SWG\Definition(
* definition="model:foo",
* allOf={
* @SWG\Schema(ref="#/definitions/model:FooBar"),
* @SWG\Schema(
* required={"unique_to_foo_field"},
* type="object",
* @SWG\Property(property="unique_to_foo_field", type="integer")
* )
* }
* )
*
* @SWG\Definition(
* definition="model:bar",
* allOf={
* @SWG\Schema(ref="#/definitions/model:FooBar"),
* @SWG\Schema(
* required={"unique_to_bar_field"},
* type="object",
* @SWG\Property(property="unique_to_foo_field", type="integer")
* )
* }
* )
*/