OpenAPITools / OpenAPITools/openapi-diff

False positive when adding optional property

未关闭
#490 3 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Breaking/Non-Breaking classification
主要语言
Java
星标
1.1k
派生
190
PR 合并指标
30 天内没有已合并 PR

描述

When comparing these 2 schemas:

test/old.json

{
  "openapi": "3.0.0",
  "servers": [{ "url": "https://myserver.com" }],
  "info": { "title": "My API", "description": "Description", "version": "0.2.0" },
  "paths": {
    "/v1/entity/{id}": {
      "put": {
        "operationId": "myOperation",
        "description": "Some description",
        "requestBody": { "$ref": "#/components/requestBodies/updateBody" }
      }
    }
  },
  "components": {
    "schemas": {},
    "responses": {},
    "requestBodies": {
      "updateBody": {
        "description": "Update description",
        "content": {
          "application/json": {
            "schema": {
              "type": "object",
              "properties": {
                "url": { "type": "string", "description": "Some URL" }
              },
              "title": "updateBody",
              "additionalProperties": false
            }
          }
        }
      }
    },
    "parameters": {}
  }
}

test/new.json

{
  "openapi": "3.0.0",
  "servers": [{ "url": "https://myserver.com" }],
  "info": { "title": "My API", "description": "Description", "version": "0.2.0" },
  "paths": {
    "/v1/entity/{id}": {
      "put": {
        "operationId": "myOperation",
        "description": "Some description",
        "requestBody": { "$ref": "#/components/requestBodies/updateBody" }
      }
    }
  },
  "components": {
    "schemas": {},
    "responses": {},
    "requestBodies": {
      "updateBody": {
        "description": "Update description",
        "content": {
          "application/json": {
            "schema": {
              "type": "object",
              "properties": {
                "url": { "type": "string", "description": "Some URL" },
                "url2": { "type": "string", "description": "Some URL" }
              },
              "title": "updateBody",
              "additionalProperties": false
            }
          }
        }
      }
    },
    "parameters": {}
  }
}

I get a broken compatibility message:

docker run --rm -t -v $(pwd)/test/:/specs:ro openapitools/openapi-diff:latest --fail-on-incompatible  /specs/old.json /specs/new.json 
==========================================================================
==                            API CHANGE LOG                            ==
==========================================================================
                                  My API                                  
--------------------------------------------------------------------------
--                            What's Changed                            --
--------------------------------------------------------------------------
- PUT    /v1/entity/{id}
  Request:
        - Changed application/json
          Schema: Broken compatibility
--------------------------------------------------------------------------
--                                Result                                --
--------------------------------------------------------------------------
                 API changes broke backward compatibility                 
--------------------------------------------------------------------------

Note that the only change is the addition of the url2 optional property:

diff -u test/old.json test/new.json
--- test/old.json       2023-03-14 17:06:02
+++ test/new.json       2023-03-14 17:06:33
@@ -22,7 +22,8 @@
             "schema": {
               "type": "object",
               "properties": {
-                "url": { "type": "string", "description": "Some URL" }
+                "url": { "type": "string", "description": "Some URL" },
+                "url2": { "type": "string", "description": "Some URL" }
               },
               "title": "updateBody",
               "additionalProperties": false

Since the required field is absent, I would expect the addition of url2 to not create a broken compatibility error.

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从提供的 test/old.json 和 test/new.json fixture 开始,并使用报告中的 Docker 命令复现结果。跟踪针对新增属性但没有必需字段的请求体 schema 兼容性检查,然后添加一个回归测试,并确认比较结果不再报告向后不兼容的变更。

由索引模型根据 Issue 内容生成。

评估

技术栈
java
领域
api
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
42/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。