You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 19:20:40