File size: 1,937 Bytes
9159c06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""Analyzer for FastAPI and backend web-service code."""

from __future__ import annotations

from typing import Any, Dict

from schemas.response import AnalysisIssue, DomainAnalysis


def analyze_web_code(code: str, parsed: Dict[str, Any], complexity: Dict[str, Any]) -> DomainAnalysis:
    """Inspect API code for validation, routing, and backend safety concerns."""

    issues = []
    suggestions = []
    score = 0.76

    route_decorators = set(parsed.get("route_decorators", []))
    if route_decorators and not parsed.get("uses_pydantic"):
        issues.append(
            AnalysisIssue(
                title="Request validation model is missing",
                severity="high",
                description="Route handlers appear present, but no obvious Pydantic validation layer was detected.",
            )
        )
        suggestions.append("Add Pydantic request and response models for strict validation and type-safe contracts.")
        score -= 0.2

    if {"get", "post", "put", "delete"} & route_decorators and "async def" not in code:
        suggestions.append("Prefer async FastAPI endpoints when the route performs I/O or awaits downstream services.")
        score -= 0.08

    if "request.json()" in code or "request.body()" in code:
        suggestions.append("Validate raw request payloads before use; avoid trusting unchecked JSON input.")
        score -= 0.08

    if not suggestions:
        suggestions.append("Add domain-specific response models and centralize dependency injection for cleaner API structure.")

    return DomainAnalysis(
        domain="web",
        domain_score=max(0.05, round(score, 4)),
        issues=issues,
        suggestions=suggestions,
        highlights={
            "route_count": float(len(route_decorators)),
            "uses_validation": float(parsed.get("uses_pydantic", False)),
            "time_complexity": complexity["time_complexity"],
        },
    )