Langsung ke konten
Skillpedia
API & server (backend)41k Β· #6815

improving-drf-endpoints

Fungsinya

Skill ini memandu AI dalam meninjau dan memperbaiki endpoint serta serializer Django REST Framework. Ditujukan untuk pengembang yang ingin dokumentasi API dan kode yang dihasilkannya akurat.

Kapan dipakai

  • βœ“Saat Anda sedang mengedit serializer atau viewset Django REST Framework dan ingin tipe fieldnya benar.
  • βœ“Saat tipe API yang dihasilkan menampilkan nilai tidak dikenal atau salah dan Anda perlu mencari penyebabnya.
  • βœ“Saat Anda meninjau perubahan API dan ingin memeriksa deskripsi serta skema fieldnya.

Link skill

https://github.com/PostHog/posthog/blob/master/.agents/skills/improving-drf-endpoints/SKILL.md

Salin lalu tempelkan ke AI Anda (Claude, ChatGPT, atau Cursor) dan minta AI mengikuti instruksinya.

Cara pakai

Cara paling mudah: salin pesan ini dan tempelkan ke AI Anda. AI akan membaca skill dan mengikuti instruksinya.

Pesan siap tempel

Gunakan skill ini untuk membantu saya: https://github.com/PostHog/posthog/blob/master/.agents/skills/improving-drf-endpoints/SKILL.md Baca file SKILL.md di link tersebut dan ikuti instruksinya untuk tugas saya.
Mau AI Anda menemukan skill seperti ini sendiri?Pasang MCP Skillpedia gratis: cukup satu baris, dan AI mencari di antara 15.000+ skill sebelum tiap tugas.Pasang dalam 1 menit β†’

Contoh permintaan ke AI

β€œBantu saya meninjau serializer API Django saya agar dokumentasi dan tipe yang dihasilkan menampilkan field dan deskripsi yang benar.”

5 skill baru setiap minggu. 1 menit baca.

Cara kerjanya

Skill memeriksa tipe setiap field, teks bantuan, dan catatan skema, karena detail ini mengalir ke dokumentasi API dan kode yang dihasilkan darinya. Setelah itu, skill menyarankan perbaikan pada kode serializer.

Konten teknis (SKILL.md asli)

Teks asli dari pembuat, dalam bahasa Inggris, langsung dari GitHub.

Improving DRF Endpoints

Before you propose a contract test against the generated OpenAPI schema, check things already tried. Six PRs took that idea, and none merged.

Overview

Serializer fields are the source of truth for PostHog's entire type pipeline:

Django serializer β†’ drf-spectacular β†’ OpenAPI JSON β†’ Orval β†’ Zod schemas β†’ MCP tools

Every help_text, every field type, every @extend_schema annotation flows downstream. A missing help_text means an agent guessing at parameters. A bare ListField() means z.unknown() in the generated Zod schema. Getting the serializer right means every consumer β€” frontend types, MCP tools, API docs β€” gets correct types and descriptions automatically.

When to use

  • Editing or reviewing any file that defines a Serializer or ViewSet
  • Fixing OpenAPI spec warnings or generated type issues
  • Preparing an endpoint for MCP tool exposure
  • Code review of API changes

Audit checklist

Triage: check the generated output first

Before diving into Python, look at the committed generated types to see what's broken. Find the generated files for the endpoint's product:

  • Core API: frontend/src/generated/core/
  • Product APIs: products/<product>/frontend/generated/

Each has two files:

  • api.schemas.ts β€” TypeScript interfaces derived from serializers. Search for the serializer name and look for unknown types (bare ListField/JSONField), missing JSDoc descriptions (missing help_text), or overly generic Record<string, unknown> shapes.
  • api.ts β€” API client functions. Check if the endpoint's operation exists at all β€” if missing, the viewset method likely lacks @extend_schema.

This tells you exactly which fields and endpoints to prioritize.

Serializer fields

Work through this list for every serializer and viewset you touch.

  1. Every field has help_text β€” describes purpose, format, constraints, valid values
  2. No bare ListField() or DictField() β€” always specify child= with a typed serializer or field
  3. No bare JSONField() β€” create a custom field class with @extend_schema_field(TypedSchema)
  4. SerializerMethodField has @extend_schema_field on its get_* method
  5. ChoiceField has explicit choices= with all valid values listed
  6. Define choices as a class β€” models.TextChoices in a product's internal modules, LabeledStrEnum or LabeledIntEnum from posthog/enums.py in facade contract files (backend/facade/contracts.py, backend/facade/enums.py), which must not import Django. The OpenAPI component is named after the class either way (EarlyAccessFeature.Stage -> EarlyAccessFeatureStageEnum, via ChoicesEnumNameOverrides in posthog/openapi/enum_names.py), so a class-backed enum never collides on field names like format, type, status, kind. Pass X.choices to choices=, and to name a labeled enum on a SerializerMethodField use @extend_schema_field(ChoiceField(choices=X.choices)), not a return type hint. Inline choices=[...] lists have no class to read, collide with existing choices, and fail CI under --fail-on-warn; an explicit ENUM_NAME_OVERRIDES entry in posthog/settings/web.py is the fallback for choice sets no class can carry (see serializer-fields.md)
  7. Read vs write serializers are separate when input shape differs from output
  8. Every success response is backed by a serializer β€” returning raw dicts or untyped lists means no generated types downstream

See serializer-fields.md for patterns and examples.

Viewset and action annotations

  1. Every custom @action has @extend_schema or @validated_request β€” without it, drf-spectacular discovers zero parameters
  2. Plain ViewSet methods have schema annotations β€” ModelViewSet with serializer_class is auto-discovered; plain ViewSet is not
  3. @extend_schema is on the actual method (get, post, create, list), not on a helper or the class itself
  4. Error responses are typed β€” use OpenApiResponse(response=ErrorSerializer), not OpenApiTypes.OBJECT
  5. List endpoints declare pagination β€” reset with pagination_class=None on custom actions that don't paginate
  6. Prefer @validated_request over manual serializer.is_valid() + @extend_schema β€” it handles both in one decorator
  7. **ViewSets o
Pertanyaan umum

Apakah saya perlu tahu Django untuk menggunakannya?

Ya. Skill ini ditujukan untuk pengembang yang sudah bekerja dengan Django REST Framework. Skill menjelaskan apa yang perlu diperiksa, tetapi Anda tetap harus memahami kodenya.

Apa manfaat utamanya?

Teks bantuan yang jelas dan tipe field yang benar membuat dokumentasi API dan kode klien yang dihasilkan menggambarkan setiap endpoint dengan akurat.

Apakah berfungsi untuk API apa pun?

Skill ini ditujukan untuk API Django REST Framework yang membuat skemanya dengan drf-spectacular.

Detail

Deskripsi asli dari pembuat

Use when editing, reviewing, or auditing DRF viewsets and serializers in PostHog. Triggers on files in posthog/api/, products/*/backend/api/, products/*/backend/presentation/, or any file importing rest_framework. Covers field typing, schema annotations, enum collision fixes, and OpenAPI spec quality β€” everything that flows downstream into generated TypeScript types and MCP tools.

repo: PostHog/posthog

Popularitas

Bintang GitHub
41k
Peringkat
#6815
Lisensi
β€”
Diperbarui
2/11/2026

Apakah skill ini berguna?

0

Masuk untuk memberi suara.

Komentar

  • …

Skill lain dari repositori ini

Skill serupa