OpenAPI定义中父对象非必填但子属性必填,请求时能否忽略父对象?
问题解答
可以完全忽略整个MyIdentity对象,原因如下:
- 从你提供的
FooData定义来看,identity字段并没有被加入required列表,这意味着调用API时,请求体里完全可以不包含这个字段,不会触发校验错误。 MyIdentity内部的userId必填要求,只有当你主动在请求体里包含identity对象的时候才需要遵守。简单说:你要是提供了identity,就得带上userId;但你要是压根不提供identity,这个规则对你不生效。
附上你给出的OpenAPI定义:
FooData: type: object additionalProperties: false description: Holds the foo data Information. required: - id - contact properties: id: description: Identifier type: string maxLength: 255 minLength: 1 pattern: ^[a-zA-Z0-9\-]{1,255}$ identity: description: Holds customer identity info. $ref: '#/MyIdentity' MyIdentity: type: object additionalProperties: false description: Holds the customer identity information. required: - userId properties: userId: type: integer format: int64 minimum: 0 maximum: 9223372036854775807 description: Unique Identification Number which is specific to individual user. firstName: type: string description: user's First name. maxLength: 32 minLength: 1 pattern: ^[a-zA-Z\s]{1,32}$
内容的提问来源于stack exchange,提问作者Harsh
相关产品推荐
相关产品推荐

