知识卡片

普通读书笔记卡 · 2275.as.6.f.3.c.9.b.1.b.1.b.2 · 生成模型:未记录

给API响应留扩展空间

理解等级:中级

开放性应在契约发布前设计。对未来可能增加的状态或类型,用开放字符串并说明现有值和未知值回退,避免自动生成客户端对闭合枚举的反序列化失败。供应方扩展时仍应保持既有取值的语义。仅在取值确实固定时选择闭合枚举。

对列表响应,从起初使用顶层对象包含数组,例如 data。这样以后可以在同一层增加分页和 meta。若最初直接返回顶层数组,再改成对象就改变了客户端解析的根类型。设计评审可一并检查这两类未来扩展点。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • 破坏性变更与 API 演进 4.12 Managing breaking changes · 查看原书依据

      Consider using an open-ended list of values instead of enums when the final list of values is unknown or when there is a likelihood that the list of values will change. Enums are properties that have a fixed set of values. Client integration code (created by developers or generated with automation tools from the OpenAPI definition) may not robustly handle new and unknown enum values.

    参考来源

    - 位置:《Automating API Delivery APIOps with OpenAPI》《第 04 章 破坏性变更与 API 演进》(4.12 Managing breaking changes;来源项目 book-source,extracted/Automating API Delivery APIOps with OpenAPI/_epub-src/OEBPS/Text/chapter-4.xhtml#p259) - 结论依据:说明闭合枚举扩展在旧客户端中的兼容风险。 - 原始内容:Consider using an open-ended list of values instead of enums when the final list of values is unknown or when there is a likelihood that the list of values will change. Enums are properties that have a fixed set of values. Client integration code (created by developers or generated with automation tools from the OpenAPI definition) may not robustly handle new and unknown enum values.