如何在OpenAPI(Swagger)中将$ ref属性声明为readOnly? [英] How to declare a $ref property as readOnly in OpenAPI (Swagger)?
问题描述
在此示例中,我尝试为房子"添加一个只读字段.这房子是我要设为只读的另一个模型.
I am trying to add a read-only field for 'House' in this example. The house is another model that I want to be read-only.
在此示例中,可以将Dogs数组设置为readOnly
而没有错误,但是当我将House的单个定义设置为readOnly
时,在Swagger编辑器中会收到以下警告:
In this example, the array of Dogs can be set to readOnly
without an error, but when I set the single definition of House to readOnly
I get the following warning in the Swagger Editor:
$ refs不允许同级值
Sibling values are not allowed alongside $refs
我知道这是因为模型中的所有内容都在这里继承.那么,如何定义写API调用不能在此端点中定义房屋",而又允许在其他API端点中创建和更新房屋?
I understand that this is because everything in the model is inherited here. So how do I define that the write API calls cannot define a 'House' in this endpoint whilst also allowing House to be created and updated in another API endpoints?
Pets:
properties:
id:
type: string
example: AAAAE12-1123AEF-1122312123
readOnly: true
name:
type: string
example: My Default Name
text:
type: string
example: My Default Text
Dogs:
type: array
readOnly: true
items:
$ref: '#/definitions/Dog'
House:
readOnly: true
$ref: '#/definitions/House'
推荐答案
您需要allOf
才能将$ref
与其他属性组合:
You need allOf
to combine a $ref
with other attributes:
House:
readOnly: true
allOf:
- $ref: '#/definitions/House'
这篇关于如何在OpenAPI(Swagger)中将$ ref属性声明为readOnly?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!