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

This commit is contained in:
2026-06-04 09:53:36 +08:00
parent a8cde16c17
commit 1b19e8366e
4 changed files with 245 additions and 205 deletions

View File

@@ -26,6 +26,44 @@ from models import ApiEndpoint, ApiParameter
MAPPING_ANNS = {"GetMapping", "PostMapping", "PutMapping", "DeleteMapping", "PatchMapping", "RequestMapping"}
CONTROLLER_ANNS = {"RestController", "Controller"}
# Spring MVC 框架自动注入参数,不属于 API 调用方入参,解析时忽略
FRAMEWORK_PARAM_TYPES = {
"HttpServletRequest",
"HttpServletResponse",
"HttpSession",
"ServletRequest",
"ServletResponse",
"WebRequest",
"NativeWebRequest",
"Model",
"ModelMap",
"RedirectAttributes",
"BindingResult",
"Errors",
"Authentication",
"Principal",
"Locale",
"TimeZone",
"InputStream",
"OutputStream",
"Reader",
"Writer",
"HttpHeaders",
"UriComponentsBuilder",
}
def _is_framework_param(type_name: str, param_name: str) -> bool:
"""判断是否为框架注入参数(非 API 调用方需要传递)。"""
simple = type_name.split(".")[-1].replace(">", "").replace("<", "").strip()
if simple in FRAMEWORK_PARAM_TYPES:
return True
if param_name in ("request", "response") and (
simple.endswith("Request") or simple.endswith("Response")
):
return True
return False
def _ann_simple_name(ann: Annotation) -> str:
"""获取注解简单类名。"""
@@ -272,14 +310,19 @@ class ControllerAstParser:
return None
def _extract_param(self, param: FormalParameter) -> List[ApiParameter]:
"""提取方法参数,@RequestBody 展开 DTO 字段。"""
"""提取方法参数,@RequestBody 展开 DTO 字段;忽略框架注入参数"""
type_name = _type_to_str(param.type)
name = _param_name(param)
if _is_framework_param(type_name, name):
return []
if _has_ann(param, "RequestBody"):
return self._expand_dto(type_name, "body")
return [
ApiParameter(
name=_param_name(param),
name=name,
type=type_name,
required=_param_required(param),
source=_param_source(param),