Rest 招摇过市中包含的资源
我正在使用swagger编写一个restapi,但在定义一个与jsonapi格式(jsonapi.org)一致的易于阅读的规范时遇到了困难。这一问题具体涉及所包括资源的定义 我有多个包含的资源,它们都具有不同的属性Rest 招摇过市中包含的资源,rest,swagger,swagger-2.0,Rest,Swagger,Swagger 2.0,我正在使用swagger编写一个restapi,但在定义一个与jsonapi格式(jsonapi.org)一致的易于阅读的规范时遇到了困难。这一问题具体涉及所包括资源的定义 我有多个包含的资源,它们都具有不同的属性 "included": [{ "type": "author", "id": "323454", "attributes": { "1": "Bob",
"included": [{
"type": "author",
"id": "323454",
"attributes": {
"1": "Bob",
"2": "Jim"
},
{
"type": "book",
"id": "323454",
"attributes": {
"3": "The Island"
"4": "The Other Island"
}]
我尝试在包含的数组下定义多个$ref对象,但这不起作用:
included:
type: array
items:
- $ref: "#/definitions/includedResource"
- $ref: "#/definitions/includedResource2"
- $ref: "#/definitions/includedResource3"
有没有好办法设置多个可选对象?我不想调用一个资源对象中的所有属性,因为读取属性或将属性与资源对齐并不容易。在OpenAPI(fka Swagger)2.0中,数组项只能是相同的类型,例如所有字符串或所有includedResource
除非我误解了什么,您的示例可以使用includedResource
的单个模式来描述,其中属性是一个关联数组/字典
definitions:
included:
type: array
items:
$ref: '#/definitions/includedResource'
includedResource:
type: object
# required: [type, id, attributes] # Indicate the required properties if needed
properties:
type:
type: string
# enum: [author, book] # Use enum if needed
id:
type: string
attributes:
type: object
additionalProperties:
type: string
谢谢你的回复。我试图避免使用这种格式,因为这意味着所有的“附加属性”,无论是作者的属性还是书籍的属性,都将被归为相同的类别。因此,您希望能够为作者
,书籍
等定义特定的属性
?