Skip to content
This repository was archived by the owner on Nov 16, 2026. It is now read-only.

feat(validators): Attach OpenAPI descriptions to Valibot and Zod schemas - #128

Merged
BYK merged 2 commits into
mainfrom
feat/schema-descriptions
Nov 4, 2026
Merged

BYK merged 2 commits into
mainfrom
feat/schema-descriptions

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Independent of getsentry/sentry#129963, which adds the response field descriptions: this carries through whatever descriptions the spec has, so it can merge first. It has to land before the @sentry/api release that carries them (getsentry/toolkit#1853).

The generated Valibot and Zod schemas drop every OpenAPI description, so consumers can't read field docs at runtime. The toolkit CLI hand-writes pipe(field, description("…")) for its --help field tables and skill references, and those drift from the spec.

This turns on Hey API's metadata option for both plugins.

  • Valibot: the plugin emits v.metadata({ description }), and v.getDescription() doesn't read that. A new step, scripts/describe-valibot-schemas.mjs, rewrites it to v.description(…). It also drops the description pipe from top-level schemas, because v.pick/v.omit/v.partial/v.required reject piped object schemas. Their description is already in the JSDoc, and for responses it's mostly status text like "No Content". Every top-level export keeps its type head. The step fails closed on any metadata shape it doesn't expect.
  • Nullable fields: Hey API attaches descriptions only to typed IR schemas, not unions, so every OpenAPI 3.0 nullable field lost its description in both plugins. A plugin:handler:before hook copies the field's description onto its one non-null branch. types.gen.ts doesn't change.
  • Zod: used as Hey API emits it, .register(z.globalRegistry, { description }). .description and z.toJSONSchema() read that. The type is unchanged. This part is optional: drop the zod entry's options if we don't want Zod descriptions yet.
 export const vDeployResponse = v.object({
-    id: v.string(),
+    id: v.pipe(v.string(), v.description('The ID of the deploy')),
-    name: v.nullable(v.string()),
+    name: v.nullable(v.pipe(v.string(), v.description('The optional name of the deploy'))),

Coverage today

In openapi-derefed.json, 48 of 2,879 top-level 2xx response properties have a description (1.7%), or 62 of 8,263 counting nested ones (0.8%), across 11 operations. Request body properties and parameters are 99.9% described at the top level. So for now this mostly surfaces request descriptions. Response coverage has to come from the serializers in getsentry/sentry. getsentry/sentry#129963 raises it to 351 of 2,890 (12.1%) at the top level and 628 of 8,288 (7.6%) nested. That covers every API field the Sentry CLI describes by hand.

Size

before after
dist/valibot.js 1.45 MB 2.00 MB
dist/zod.js 1.23 MB 1.79 MB
dist/valibot.gen.d.ts 1.88 MB 3.11 MB (description literals are in the action types)
packed dist (gzip) ~0.96 MB ~1.35 MB
tree-shaken, vGetOrganizationResponse only (min / gzip) 951 B / 388 B unchanged
tree-shaken, 7 schemas the toolkit CLI uses (min / gzip) 16.8 KB / 3.2 KB 19.5 KB / 4.1 KB

The Valibot output has 2,376 descriptions. With the toolkit CLI's typecheck on this build, tsc memory goes up about 2.5%, and check time stays within noise. The CLI's npm JS and the @sentry/mcp-server bundle each grow about 2 KB.

via @BYK.

--

View Session in Sentry

Turn on the Hey API `metadata` option for both validator plugins, so field
schemas carry their OpenAPI description at runtime.

Valibot's plugin emits `v.metadata({ description })`, which
`v.getDescription()` does not read. A post-processing step rewrites it to
`v.description(...)`, and drops the description pipe from top-level schemas:
`v.pick`, `v.omit`, `v.partial` and `v.required` reject piped object schemas,
and the top-level description is already in the JSDoc comment. Zod's plugin
registers the description in `z.globalRegistry`, which `.description` and
`z.toJSONSchema()` read, so its output is used as is.

Co-Authored-By: BYK <[email protected]>
Hey API's validator plugins attach descriptions only to typed schemas, so an
OpenAPI 3.0 nullable field, a union with null in the IR, lost its description.
Copy it onto the field's non-null branch before the validator plugins run.

Co-Authored-By: BYK <[email protected]>
@sentry-junior
sentry-junior Bot marked this pull request as draft November 4, 2026 15:09
@sentry-junior
sentry-junior Bot marked this pull request as ready for review November 4, 2026 21:18
@BYK
BYK merged commit 1a34e27 into main Nov 4, 2026
11 checks passed
@BYK
BYK deleted the feat/schema-descriptions branch November 4, 2026 21:27
jackeddisciple pushed a commit to jackeddisciple/sentry that referenced this pull request Nov 5, 2026
…getsentry#129963)

TypedDict response fields can't carry a description into the OpenAPI
spec today. `resolve_type_hint` reads TypedDict hints without extras,
and only a nested TypedDict's class docstring ever shows up (there's a
`TODO: figure out solution for field descriptions`). As a result, only
**48 of 2,890 (1.7%)** top-level 2xx response properties in the public
spec have a description. Request bodies and parameters are at 99.9%.

### Mechanism

A new `ApiDescription` marker in `sentry.apidocs.descriptions`, used
through `Annotated`:

```python
class BaseTeamSerializerResponse(TypedDict):
    isMember: Annotated[bool, ApiDescription("Whether the requesting user is a member of the team")]
```

How it works:
- In the TypedDict branch of `resolve_type_hint`, the hints are also
read with `include_extras=True`. Each field's hint has
`Required`/`NotRequired`/`ReadOnly` and `Annotated` unwrapped, and the
property gets its `description` from the marker.
- Field types still resolve from the stripped hints exactly as before,
so `__required_keys__` and other `Annotated` metadata are unaffected.
- A field description replaces a nested TypedDict's docstring for that
property.

What stays the same:
- mypy only sees the inner type, so field types and runtime behavior
don't change.
- It works with `from __future__ import annotations` and with functional
`TypedDict(...)` definitions.

Tests are in `tests/sentry/apidocs/test_spectacular_ports.py`. That's
where the existing `resolve_type_hint` tests live; `tests/apidocs/`
holds endpoint doc tests.

### Fields

These are the response fields the Sentry CLI documents by hand today.
The wording is seeded from the CLI text and made API-neutral, e.g.
"Whether you bookmarked it" became "Whether the requesting user
bookmarked the project".

| Owner | Files | Fields |
|---|---|---|
| `@getsentry/app-backend` |
`api/serializers/models/{event,group,organization,project,team}.py`,
`api/serializers/types.py` (releases) | 12 + 11 + 10 + 12 + 4 + 19 = 68
|
| `@getsentry/replay` | `replays/post_process.py`
(`ReplayDetailsResponse`) | 30 |
| `@getsentry/alerts-monitors` |
`workflow_engine/.../{workflow,detector}_serializer.py` | 4 |
| no CODEOWNERS entry | `tagstore/types.py` | 2 |
| `@getsentry/owners-api` | `apidocs/spectacular_ports.py`,
`apidocs/descriptions.py`, tests | mechanism |

### Spec impact

I compared `make build-api-docs` output from `master` with output from
this branch:
- `openapi-derefed.json` gains **970 `description` keys**. No existing
value changes and nothing else differs.
- One exception: the workflow action `priority` enum comes out in a
different order, but that order changes from one build to the next, with
or without this change.
- 2xx response properties with a description:
  - top-level: 48/2,890 (1.7%) → **351/2,890 (12.1%)**
  - nested: 62/8,288 (0.7%) → **628/8,288 (7.6%)**
- Operations with any described response property: 11 → **61**.
- `validate-api-examples` and `build-investigation-api-docs` pass.

### Why

This is for getsentry/toolkit#1853. The CLI's `--help` field tables and
skill references wrap generated `@sentry/api` Valibot fields in
hand-written descriptions that drift from the API. The CLI has 84 such
descriptions on API fields; the spec described 4 of them before this
change, and it describes all 84 with it. That lets the CLI drop its
copies, and the public API docs gain field descriptions too.

Paired with getsentry/sentry-api-schema#128, which emits these
descriptions in the generated Valibot and Zod schemas. Once this merges
and the spec syncs, `@sentry/api` publishes them and the toolkit can
bump and remove its duplicates.

### Verification

- `tests/sentry/apidocs/` (117 passed).
- `tests/sentry/replays/test_post_process.py`,
`tests/sentry/tagstore/test_types.py` and
`tests/sentry/api/serializers/test_team.py` pass locally.
- mypy (pre-push hook) and `prek` pass on the touched files.
- The other serializer and `tests/apidocs` endpoint tests need Snuba,
which I don't have locally, so I left those to CI.

<!-- junior-request-attribution:start -->
via @BYK.
<!-- junior-request-attribution:end -->

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:agent%3A98eff8ed01bc8064aba01279b3ac82c3 -->

--

[View Session in
Sentry](https://sentry.sentry.io/explore/conversations/agent%3A98eff8ed01bc8064aba01279b3ac82c3/?project=4510944073809921)

<!-- junior-session-footer:end -->

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
Co-authored-by: BYK <[email protected]>
@betegon betegon mentioned this pull request Nov 5, 2026
8 of 39 tasks
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant