例如,我可以创造一种车辆,并且必须给它一种可以是“汽车”或“飞机”的类型。汽车需要轮胎尺寸参数,飞机需要翼展参数。关于REST API参数是否应该影响响应中需要哪些属性的类似问题。
<!-- ko with:moreinfo -->
<p data-bind="text: condition"></p>
<!-- /ko -->
答案 0 :(得分:2)
虽然这在实践中发生,但最好避免使用,因为它很难记录您的API。如果您使用Swagger等文档技术,并且不允许这些&#34;有条件地要求&#34;参数。通过添加它们,您实际上是在API中添加了额外的语义,这些语义未在Swagger文档中记录。
更好的方法是,不要使用&#34;类型&#34;不同车型的参数,只需为每种类型使用单独的URL。这样您就可以正确记录每种车型的必需/可选参数。
/vehicles/automobile:
post:
parameters:
schema:
required:
- tyresize
properties:
tyresize:
type: string
/vehicles/airplane:
post:
parameters:
schema:
required:
- wingspan
properties:
wingspan:
type: string