Complete backend MVP
This commit is contained in:
@@ -30,10 +30,31 @@ def operation_id(controller: str, handler: str) -> str:
|
||||
|
||||
def openapi_path(path: str) -> tuple[str, list[dict[str, Any]]]:
|
||||
parameters: list[dict[str, Any]] = []
|
||||
segments = path.split("/")
|
||||
placeholder_names: dict[int, str] = {}
|
||||
|
||||
for position, segment in enumerate(segments):
|
||||
match = re.fullmatch(r"\{(\d+)\}", segment)
|
||||
if not match:
|
||||
continue
|
||||
|
||||
index = int(match.group(1))
|
||||
if index == 1:
|
||||
placeholder_names[index] = "id"
|
||||
continue
|
||||
|
||||
previous = segments[position - 1] if position > 0 else ""
|
||||
if previous.endswith("ies"):
|
||||
stem = previous[:-3] + "y"
|
||||
elif previous.endswith("s") and len(previous) > 1:
|
||||
stem = previous[:-1]
|
||||
else:
|
||||
stem = previous or f"param{index}"
|
||||
placeholder_names[index] = f"{stem}_id"
|
||||
|
||||
def replace(match: re.Match[str]) -> str:
|
||||
index = int(match.group(1))
|
||||
name = "id" if index == 1 else f"param{index}"
|
||||
name = placeholder_names.get(index, f"param{index}")
|
||||
parameters.append(
|
||||
{
|
||||
"name": name,
|
||||
|
||||
@@ -34,6 +34,27 @@ class GenerateOpenApiTest(unittest.TestCase):
|
||||
self.assertEqual(operation["operationId"], "SampleController.detail")
|
||||
self.assertEqual(operation["parameters"][0]["name"], "id")
|
||||
|
||||
def test_names_secondary_path_parameters_from_previous_segment(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
controllers = Path(tmp)
|
||||
(controllers / "SampleController.h").write_text(
|
||||
"""
|
||||
class SampleController {
|
||||
METHOD_LIST_BEGIN
|
||||
ADD_METHOD_TO(SampleController::flagMessage,
|
||||
"/api/v1/reports/{1}/messages/{2}/flags",
|
||||
drogon::Post);
|
||||
METHOD_LIST_END
|
||||
};
|
||||
""",
|
||||
encoding="utf-8",
|
||||
)
|
||||
routes = generate_openapi.parse_routes(controllers)
|
||||
operation = routes[
|
||||
("/api/v1/reports/{id}/messages/{message_id}/flags", "post")
|
||||
]
|
||||
self.assertEqual(operation["parameters"][1]["name"], "message_id")
|
||||
|
||||
def test_manual_operation_overrides_generated_defaults(self) -> None:
|
||||
generated = {
|
||||
("/api/v1/samples", "get"): {
|
||||
|
||||
Reference in New Issue
Block a user