这是我在响应中使用的JSON格式:
{
"status": "success",
"error": null,
"data": {
"<product_id>": {
"productDetails": {
"name": "Name of product",
"currency": "USD",
...
},
...
}
}
}
在定义产品Map时,我在OpenAPI 3中添加了以下内容。
components:
schemas:
ApiResponse:
type: object
properties:
status:
type: string
default: success
enum:
- success
- failure
error:
type: string
example: "Internal error"
data:
type: object
additionalProperties:
$ref: '#/components/schemas/ProductData'
ProductData:
type: object
properties:
productDetails:
type: object
$ref: '#/components/schemas/ProductDetails'
ProductDetails:
type: object
properties:
name:
type: string
example: "Sample prod name"
currency:
type: string
example: "USD"
在swagger中,这将生成如下示例:
{
"status": "success",
"error": "Internal error",
"data": {
"additionalProp1": {
"productDetails": {
"name": "Sample prod name",
"currency": "USD"
}
},
"additionalProp2": {
"productDetails": {
"name": "Sample prod name",
"currency": "USD"
}
},
"additionalProp3": {
"productDetails": {
"name": "Sample prod name",
"currency": "USD"
}
}
}
}
然而,我想传递一个自定义示例作为密钥名称(比如,“d8467030-e737- 11 ed-a05 b-0242 ac 120003”),而不是 swagger 地填充密钥(如“additionalProp 1”,“additionalProp 2”等)。这怎么可能?
我从swagger检查了OpenAPI 3.0 documentation,但找不到这样做的方法。
1条答案
按热度按时间bpsygsoo1#
在OpenAPI v3.1中,您可以使用
propertyNames
关键字-https://json-schema.org/understanding-json-schema/reference/object.html#property-names