Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

2.7 KB
Raw Blame
rules:
    - id: api-response-must-match-schema
      message: |
          Returning a hand-assembled dict from a product viewset is a common
          source of spec drift. The OpenAPI schema for this endpoint is built
          from its `serializer_class` or `@extend_schema(responses=...)`
          declaration — nothing checks that the dict you returned actually
          matches that schema. When they diverge, generated FE types and MCP
          tool schemas silently lie about the wire shape.

          Why this matters: spectacular trusts the declaration, not the
          runtime. A `Response({"foo": ...})` next to a `@extend_schema(
          responses=FooSerializer)` produces a wrong-but-plausible spec that
          flows downstream into `api.schemas.ts`, MCP tool definitions, and
          anything else generated from `openapi.json`.

          Instead of:
              return Response({"id": str(run.id), "status": run.status, ...})

          Use:
              return Response(FooSerializer(instance=foo).data)

          For products on the facade layout (visual_review-style), construct
          a contract dataclass and pass it through a `DataclassSerializer` —
          that gives you runtime type validation on top of spec alignment. See
          the `isolating-product-facade-contracts` skill.

          This rule is currently informational (WARNING severity). Findings
          are the backlog of viewsets still assembling responses by hand.
          Once a product's count hits zero, the path can be narrowed and
          severity flipped to ERROR for that product.
      languages: [python]
      severity: WARNING
      paths:
          include:
              - 'products/*/backend/'
              # Test fixture lives here — must be in include so `semgrep --test` exercises the rule.
              - '.semgrep/rules/devex/'
          exclude:
              - '**/tests/**'
              - '**/test_*.py'
              - '**/conftest.py'
      patterns:
          - pattern-either:
                - pattern: return Response({...}, ...)
                - pattern: return Response({...})
                - pattern: return Response(data={...}, ...)
          # Error / pagination / ack idioms are conventional DRF shapes — not
          # data payloads. Match the dict's *first key* so we don't accidentally
          # exempt larger payloads that happen to contain one of these names.
          # `(?:data=)?` covers both `Response({...})` and `Response(data={...})`.
          - pattern-not-regex: 'Response\(\s*(?:status\s*=\s*[^,)]+,\s*)?(?:data\s*=\s*)?\{\s*"(detail|error|errors|message|status|success|ok|results|count|next|previous)"\s*:'
      metadata:
          category: best-practice
          subcategory:
              - audit