知识卡片
Schema引用复用子模型
OpenAPI Schema 可以用 $ref 引用同一文档中的子模型:CreateOrderSchema 的 order 数组项复用 OrderItemSchema,不必重写字段约束。创建订单的请求与读取订单的响应可使用不同外层 Schema;验证嵌套载荷时仍要沿引用检查子项约束。
这张卡片看不懂?
这张卡片的反馈已满 10 条,暂时无法继续提交。
已有反馈 · 0 / 10 条
还没有人提交反馈。
原书出处
参考来源
- 位置:《Microservice APIs》第 2 章《A basic API implementation》(源文件:_epub-src/OEBPS/Text/02.htm)
- 结论依据:2.4 节的订单规范用 `$ref` 指向同文档的 `OrderItemSchema`,文字说明 `CreateOrderSchema.order` 是由该子模型定义的对象数组,并需逐项验证。
- 原始内容:We use a JSON pointer to reference another schema within the same document. We use GetOrderSchema when we return the details of an order from the server and CreateOrderSchema to validate an order placed by a customer. ... CreateOrderSchema only requires the presence of one property in the payload: the order property, which is an array of objects whose specification is defined by OrderItemSchema .