py修改
All checks were successful
API接口参数变更检测 / api-param-check (push) Successful in 16s

This commit is contained in:
2026-06-05 10:21:34 +08:00
parent 07f070e97d
commit 4974eda17d
2 changed files with 80 additions and 11 deletions

View File

@@ -89,6 +89,8 @@ class EndpointChangeReport:
is_removed_endpoint: bool = False
is_renamed_endpoint: bool = False
old_uri: Optional[str] = None
is_method_changed: bool = False
old_http_method: Optional[str] = None
@property
def has_changes(self) -> bool:
@@ -97,6 +99,7 @@ class EndpointChangeReport:
self.is_new_endpoint
or self.is_removed_endpoint
or self.is_renamed_endpoint
or self.is_method_changed
or len(self.parameter_changes) > 0
)
@@ -219,7 +222,11 @@ def compare_endpoints(
"""
对比新旧两个版本的全部 Controller 端点,生成变更报告列表。
支持 URI 重命名检测(路径变更)。
支持以下变更类型检测:
- HTTP 方法变更GET → POST 等)
- URI 路径变更(路径重命名)
- 新增 / 删除接口
- 参数变更
"""
reports: List[EndpointChangeReport] = []
@@ -230,7 +237,7 @@ def compare_endpoints(
added_keys = new_keys - old_keys
common_keys = old_keys & new_keys
# 1. URI 重命名检测:在 removed + added 中找 method+controller+method_name 相同的配对
# 收集未匹配的 removed / added
unmatched_removed: List[Tuple[str, ApiEndpoint]] = []
unmatched_added: List[Tuple[str, ApiEndpoint]] = []
@@ -242,15 +249,44 @@ def compare_endpoints(
matched_removed: Set[str] = set()
matched_added: Set[str] = set()
# 1. HTTP 方法变更检测uri + controller + method_name 相同,但 method 不同)
for r_key, r_ep in unmatched_removed:
for a_key, a_ep in unmatched_added:
if a_key in matched_added:
continue
# 匹配条件method 相同 + controller 相同 + method_name 相同
if (
r_ep.uri == a_ep.uri
and r_ep.controller_class == a_ep.controller_class
and r_ep.method_name == a_ep.method_name
and r_ep.http_method != a_ep.http_method
):
reports.append(
EndpointChangeReport(
uri=a_ep.uri,
http_method=a_ep.http_method,
controller_class=a_ep.controller_class,
method_name=a_ep.method_name,
source_file=a_ep.source_file,
is_method_changed=True,
old_http_method=r_ep.http_method,
)
)
matched_removed.add(r_key)
matched_added.add(a_key)
break
# 2. URI 路径变更检测method + controller + method_name 相同,但 uri 不同)
for r_key, r_ep in unmatched_removed:
if r_key in matched_removed:
continue
for a_key, a_ep in unmatched_added:
if a_key in matched_added:
continue
if (
r_ep.http_method == a_ep.http_method
and r_ep.controller_class == a_ep.controller_class
and r_ep.method_name == a_ep.method_name
and r_ep.uri != a_ep.uri
):
reports.append(
EndpointChangeReport(
@@ -267,7 +303,7 @@ def compare_endpoints(
matched_added.add(a_key)
break
# 2. 剩余未匹配的 removed → 删除接口
# 3. 剩余未匹配的 removed → 删除接口
for key, ep in unmatched_removed:
if key not in matched_removed:
reports.append(
@@ -281,7 +317,7 @@ def compare_endpoints(
)
)
# 3. 剩余未匹配的 added → 新增接口
# 4. 剩余未匹配的 added → 新增接口
for key, ep in unmatched_added:
if key not in matched_added:
reports.append(
@@ -304,7 +340,7 @@ def compare_endpoints(
)
)
# 4. 共同 URI对比参数变更
# 5. 共同 URI对比参数变更
for key in common_keys:
old_ep = old_endpoints[key]
new_ep = new_endpoints[key]