Compare commits

...

2 Commits

Author SHA1 Message Date
f79927192c 1
All checks were successful
API接口参数变更检测 / api-param-check (push) Successful in 16s
2026-06-05 14:24:08 +08:00
35d64a91b3 1 2026-06-05 14:23:46 +08:00
2 changed files with 108 additions and 130 deletions

View File

@@ -215,6 +215,11 @@ def compare_parameters(
return changes return changes
def _method_identity(ep: ApiEndpoint) -> Tuple[str, str]:
"""生成方法的唯一标识:(source_file, method_name)。"""
return (ep.source_file or "", ep.method_name)
def compare_endpoints( def compare_endpoints(
old_endpoints: Dict[str, ApiEndpoint], old_endpoints: Dict[str, ApiEndpoint],
new_endpoints: Dict[str, ApiEndpoint], new_endpoints: Dict[str, ApiEndpoint],
@@ -222,170 +227,142 @@ def compare_endpoints(
""" """
对比新旧两个版本的全部 Controller 端点,生成变更报告列表。 对比新旧两个版本的全部 Controller 端点,生成变更报告列表。
支持以下变更类型检测 匹配策略(方案 1
- HTTP 方法变更GET → POST 等) - 使用 (source_file, method_name) 作为「同一个 Java 方法」的唯一标识。
- URI 路径变更(路径重命名) - 只要同一个 Java 方法被修改,就分别判断「请求方式是否变」和「路径是否变」。
- 新增 / 删除接口 - 支持「同时改请求方式 + 路径」时生成两条独立报告。
- 参数变更 - 支持「同时改请求方式/路径 + 参数」时生成多条独立报告。
""" """
reports: List[EndpointChangeReport] = [] reports: List[EndpointChangeReport] = []
old_keys = set(old_endpoints.keys()) # 1. 构建基于方法标识的映射
new_keys = set(new_endpoints.keys()) old_by_identity: Dict[Tuple[str, str], List[ApiEndpoint]] = {}
new_by_identity: Dict[Tuple[str, str], List[ApiEndpoint]] = {}
removed_keys = old_keys - new_keys for ep in old_endpoints.values():
added_keys = new_keys - old_keys identity = _method_identity(ep)
common_keys = old_keys & new_keys old_by_identity.setdefault(identity, []).append(ep)
# 收集未匹配的 removed / added for ep in new_endpoints.values():
unmatched_removed: List[Tuple[str, ApiEndpoint]] = [] identity = _method_identity(ep)
unmatched_added: List[Tuple[str, ApiEndpoint]] = [] new_by_identity.setdefault(identity, []).append(ep)
for key in removed_keys: all_identities = set(old_by_identity.keys()) | set(new_by_identity.keys())
unmatched_removed.append((key, old_endpoints[key]))
for key in added_keys:
unmatched_added.append((key, new_endpoints[key]))
matched_removed: Set[str] = set() for identity in all_identities:
matched_added: Set[str] = set() old_list = old_by_identity.get(identity, [])
new_list = new_by_identity.get(identity, [])
# 1. HTTP 方法变更检测uri + controller 相同,但 method 不同) # 简单场景:同一个方法在旧版和新版各出现一次
# 放宽匹配条件:只要同一个 Controller 的同一个 URI 请求方式改变,就识别为「修改请求方式」 if len(old_list) == 1 and len(new_list) == 1:
# 不再要求 method_name 相同(允许方法重命名场景) old_ep = old_list[0]
# 如果同时有参数变更,生成两条独立报告(方法变更 + 参数变更),互不干扰 new_ep = new_list[0]
for r_key, r_ep in unmatched_removed:
for a_key, a_ep in unmatched_added: # 判断请求方式是否改变
if a_key in matched_added: if old_ep.http_method != new_ep.http_method:
continue
if (
r_ep.uri == a_ep.uri
and r_ep.controller_class == a_ep.controller_class
and r_ep.http_method != a_ep.http_method
):
# 先生成纯方法变更报告
reports.append( reports.append(
EndpointChangeReport( EndpointChangeReport(
uri=a_ep.uri, uri=new_ep.uri,
http_method=a_ep.http_method, http_method=new_ep.http_method,
controller_class=a_ep.controller_class, controller_class=new_ep.controller_class,
method_name=a_ep.method_name, method_name=new_ep.method_name,
source_file=a_ep.source_file, source_file=new_ep.source_file,
is_method_changed=True, is_method_changed=True,
old_http_method=r_ep.http_method, old_http_method=old_ep.http_method,
) )
) )
# 再检测参数变更,如果有则额外生成一条独立的参数变更报告 # 如果同时有参数变更,额外生成参数报告
param_changes = compare_parameters(r_ep.parameters, a_ep.parameters) param_changes = compare_parameters(old_ep.parameters, new_ep.parameters)
if param_changes: if param_changes:
reports.append( reports.append(
EndpointChangeReport( EndpointChangeReport(
uri=a_ep.uri, uri=new_ep.uri,
http_method=a_ep.http_method, http_method=new_ep.http_method,
controller_class=a_ep.controller_class, controller_class=new_ep.controller_class,
method_name=a_ep.method_name, method_name=new_ep.method_name,
source_file=a_ep.source_file, source_file=new_ep.source_file,
parameter_changes=param_changes, parameter_changes=param_changes,
) )
) )
matched_removed.add(r_key)
matched_added.add(a_key)
break
# 2. URI 路径变更检测method + controller + method_name 相同,但 uri 不同) # 判断路径是否改变
# 如果同时有参数变更,生成两条独立报告(路径变更 + 参数变更),互不干扰 if old_ep.uri != new_ep.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( reports.append(
EndpointChangeReport( EndpointChangeReport(
uri=a_ep.uri, uri=new_ep.uri,
http_method=a_ep.http_method, http_method=new_ep.http_method,
controller_class=a_ep.controller_class, controller_class=new_ep.controller_class,
method_name=a_ep.method_name, method_name=new_ep.method_name,
source_file=a_ep.source_file, source_file=new_ep.source_file,
is_renamed_endpoint=True, is_renamed_endpoint=True,
old_uri=r_ep.uri, old_uri=old_ep.uri,
) )
) )
# 再检测参数变更,如果有则额外生成一条独立的参数变更报告 # 如果同时有参数变更,额外生成参数报告(避免重复)
param_changes = compare_parameters(r_ep.parameters, a_ep.parameters) if old_ep.http_method == new_ep.http_method:
param_changes = compare_parameters(old_ep.parameters, new_ep.parameters)
if param_changes:
reports.append(
EndpointChangeReport(
uri=new_ep.uri,
http_method=new_ep.http_method,
controller_class=new_ep.controller_class,
method_name=new_ep.method_name,
source_file=new_ep.source_file,
parameter_changes=param_changes,
)
)
# 如果请求方式和路径都没变,只检测参数变更
if old_ep.http_method == new_ep.http_method and old_ep.uri == new_ep.uri:
param_changes = compare_parameters(old_ep.parameters, new_ep.parameters)
if param_changes: if param_changes:
reports.append( reports.append(
EndpointChangeReport( EndpointChangeReport(
uri=a_ep.uri, uri=new_ep.uri,
http_method=a_ep.http_method, http_method=new_ep.http_method,
controller_class=a_ep.controller_class, controller_class=new_ep.controller_class,
method_name=a_ep.method_name, method_name=new_ep.method_name,
source_file=a_ep.source_file, source_file=new_ep.source_file,
parameter_changes=param_changes, parameter_changes=param_changes,
) )
) )
matched_removed.add(r_key)
matched_added.add(a_key)
break
# 3. 剩余未匹配的 removed → 删除接口 elif len(old_list) == 0 and len(new_list) > 0:
for key, ep in unmatched_removed: # 新增接口
if key not in matched_removed: for new_ep in new_list:
reports.append( reports.append(
EndpointChangeReport( EndpointChangeReport(
uri=ep.uri, uri=new_ep.uri,
http_method=ep.http_method, http_method=new_ep.http_method,
controller_class=ep.controller_class, controller_class=new_ep.controller_class,
method_name=ep.method_name, method_name=new_ep.method_name,
source_file=ep.source_file, source_file=new_ep.source_file,
is_removed_endpoint=True, is_new_endpoint=True,
parameter_changes=[
ParameterChange(
change_type=ChangeType.ADDED,
param_name=p.name,
param_type=p.type,
required=p.required,
)
for p in new_ep.parameters
],
)
) )
)
# 4. 剩余未匹配的 added → 新增接口 elif len(old_list) > 0 and len(new_list) == 0:
for key, ep in unmatched_added: # 删除接口
if key not in matched_added: for old_ep in old_list:
reports.append( reports.append(
EndpointChangeReport( EndpointChangeReport(
uri=ep.uri, uri=old_ep.uri,
http_method=ep.http_method, http_method=old_ep.http_method,
controller_class=ep.controller_class, controller_class=old_ep.controller_class,
method_name=ep.method_name, method_name=old_ep.method_name,
source_file=ep.source_file, source_file=old_ep.source_file,
is_new_endpoint=True, is_removed_endpoint=True,
parameter_changes=[ )
ParameterChange(
change_type=ChangeType.ADDED,
param_name=p.name,
param_type=p.type,
required=p.required,
)
for p in ep.parameters
],
) )
)
# 5. 共同 URI对比参数变更
for key in common_keys:
old_ep = old_endpoints[key]
new_ep = new_endpoints[key]
param_changes = compare_parameters(old_ep.parameters, new_ep.parameters)
if param_changes:
reports.append(
EndpointChangeReport(
uri=new_ep.uri,
http_method=new_ep.http_method,
controller_class=new_ep.controller_class,
method_name=new_ep.method_name,
source_file=new_ep.source_file,
parameter_changes=param_changes,
)
)
return [r for r in reports if r.has_changes] return [r for r in reports if r.has_changes]

View File

@@ -70,6 +70,7 @@ public class CultureClockInController {
* @param response HttpServletResponse * @param response HttpServletResponse
*/ */
@GetMapping(value = "/random-preview/base641") @GetMapping(value = "/random-preview/base641")
// @PutMapping(value = "/random-preview/base64")
public ActionResult<Base64ImageVo> getRandomPicPreviewBase64(@RequestParam(value = "lastCombo1", required = false) String lastCombo, HttpServletRequest request, HttpServletResponse response) throws Exception { public ActionResult<Base64ImageVo> getRandomPicPreviewBase64(@RequestParam(value = "lastCombo1", required = false) String lastCombo, HttpServletRequest request, HttpServletResponse response) throws Exception {
MutablePair<String, BufferedImage> pair = cultureClockInService.getRandomPicPreview(lastCombo, requestUrl); MutablePair<String, BufferedImage> pair = cultureClockInService.getRandomPicPreview(lastCombo, requestUrl);