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

This commit is contained in:
2026-06-03 15:44:25 +08:00
parent 0fd10f9d46
commit 6af9324e4b
2 changed files with 18 additions and 13 deletions

View File

@@ -7,12 +7,13 @@ from __future__ import annotations
import re
from pathlib import Path
from typing import Dict, List, Optional, Union
from typing import Dict, List, Optional
import javalang
from javalang.tree import (
Annotation,
ClassDeclaration,
ElementValuePair,
FieldDeclaration,
FormalParameter,
Literal,
@@ -32,14 +33,18 @@ def _ann_simple_name(ann: Annotation) -> str:
def _literal_str(node) -> str:
"""Literal 节点提取字符串值。"""
"""AST 节点提取字符串或布尔值。"""
if node is None:
return ""
if isinstance(node, Literal):
v = node.value or ""
return str(v).strip('"').strip("'")
v = node.value
if isinstance(v, bool):
return str(v).lower()
return str(v or "").strip('"').strip("'")
if isinstance(node, MemberReference):
return node.member
if isinstance(node, bool):
return str(node).lower()
return str(node).strip('"').strip("'")
@@ -52,12 +57,12 @@ def _collect_ann_members(ann: Annotation) -> Dict[str, str]:
el = ann.element
if el is None:
return members
if isinstance(el, list):
for item in el:
if hasattr(item, "name") and hasattr(item, "value"):
members[item.name] = _literal_str(item.value)
elif hasattr(el, "name") and hasattr(el, "value"):
if isinstance(el, ElementValuePair):
members[el.name] = _literal_str(el.value)
elif isinstance(el, list):
for item in el:
if isinstance(item, ElementValuePair):
members[item.name] = _literal_str(item.value)
else:
members["value"] = _literal_str(el)
return members
@@ -348,15 +353,15 @@ def parse_controller_files(
:param file_contents: {文件路径: 源码内容}
:return: 所有端点
"""
source_dir = repo_root / source_subdir.replace("/", Path.sep)
source_dir = (repo_root / source_subdir).resolve()
parser = ControllerAstParser(repo_root, source_dir)
endpoints: List[ApiEndpoint] = []
for path in file_paths:
content = file_contents.get(path)
norm = path.replace("\\", "/")
content = file_contents.get(norm)
if not content:
continue
norm = path.replace("\\", "/")
endpoints.extend(parser.parse_file_content(content, norm))
return endpoints