本文へスキップ
Skillpedia
APIとサーバー(バックエンド)41k · #6815

improving-drf-endpoints

できること

このスキルは、Django REST Frameworkのエンドポイントとシリアライザをレビューして修正する流れを、AIに案内します。APIドキュメントと生成されるコードを正確にしたい開発者向けです。

使う場面

  • ✓Django REST Frameworkのシリアライザやビューセットを編集し、フィールドの型を正しく付けたいとき。
  • ✓生成されたAPIの型に不明な値や誤った値が出て、原因を探す必要があるとき。
  • ✓APIの変更をレビューし、フィールドの説明とスキーマを確認したいとき。

スキルのリンク

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

コピーしてAI(Claude、ChatGPT、Cursor)に貼り付け、指示に従うよう頼んでください。

使い方

いちばん簡単な方法:メッセージをコピーしてAIに貼り付けるだけ。AIがスキルを読んで指示に従います。

貼り付け用メッセージ

このスキルを使って手伝ってください:https://github.com/PostHog/posthog/blob/master/.agents/skills/improving-drf-endpoints/SKILL.md このリンクのSKILL.mdファイルを読んで、私の作業でその指示に従ってください。
こういうスキルを、AIが自分で見つけてくれたら?無料のSkillpedia MCPをインストール。1行で、作業の前に15,000+のスキルから探してくれます。1分でインストール →

AIへの頼み方の例

“Django APIのシリアライザをレビューして、生成されるドキュメントと型が正しいフィールドと説明を示すよう手伝ってください。”

毎週5つの新しいスキル。1分で読めます。

仕組み

各フィールドの型、ヘルプテキスト、スキーマの注記を確認します。これらの情報はAPIドキュメントと、そこから生成されるコードに反映されるためです。その後、シリアライザのコードへの修正案を示します。

技術的な内容(元のSKILL.md)

作者による元のテキスト(英語)を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
よくある質問

このスキルを使うにはDjangoの知識が必要ですか?

はい。Django REST Frameworkを既に使っている開発者向けです。何を確認すべきかは説明しますが、コードを理解している必要があります。

主な利点は何ですか?

正しいヘルプテキストと型によって、生成されるAPIドキュメントとクライアントのコードが、各エンドポイントを正確に説明できるようになります。

どんなAPIでも使えますか?

drf-spectacularでスキーマを生成するDjango REST FrameworkのAPI向けです。

詳細

作者による元の説明

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

人気

GitHubのスター
41k
ランキング
#6815
ライセンス
—
更新日
2026/11/2

このスキルは役に立ちますか?

0

ログイン して投票しましょう。

コメント

  • …

このリポジトリの他のスキル

似ているスキル