知识卡片

普通读书笔记卡 · 3486.c

allOf组合兼容模型

理解等级:高级

allOf 可让响应模型同时满足多个子模式,例如复用创建订单的字段,再补上状态和创建时间。但组合只在各子模式约束兼容时可靠;若创建模式以后增加只适用于输入的字段,响应模型被连带改变,可能反而难维护。

这张卡片看不懂?

已有反馈 · 0 / 10 条

    还没有人提交反馈。

    原书出处

    参考来源

    - 位置:《Microservice APIs》第 5 章《Documenting REST APIs with OpenAPI》(源文件:_epub-src/OEBPS/Text/05.htm) - 结论依据:原文以 GetOrderSchema 组合 CreateOrderSchema 与状态/时间字段为例,随后指出模式只有严格兼容时才能安全组合。 - 原始内容:The special keyword allOf is used in these cases to indicate that the object requires all the properties in the listed schemas. ... Model composition results in a cleaner and more succinct specification, but it only works if the schemas are strictly compatible. If we decide to extend CreateOrderSchema with new properties, then this schema may no longer be transferable to the GetOrderSchema model.