Files
supabase/apps/docs/internals/api-request-body-markdown.test.ts
Alexis Aguilar 6ac0f3f231 fix(docs): include request bodies in the Management API markdown reference (#54343)
## Problem

Agents can't find request body fields for Management API endpoints. In
an eval for the access control guide
([DOCS-1489](https://linear.app/supabase/issue/DOCS-1489)), an agent
read the [Create organization
invitations](https://supabase.com/docs/reference/api/v2-create-organization-invitations)
page and concluded the body accepts only an email. It refused to invite
someone scoped to one project, even though the API accepts `role` and
`projects`.

Reference pages have no `.md` version. Their markdown 404 points agents
to the markdown reference (`llms/api.txt`), which lists path and query
parameters but no request body for any endpoint.

Resolves part of
[DOCS-1823](https://linear.app/supabase/issue/DOCS-1823).

## Solution

Each endpoint with a request body gets a **Request body** section in the
markdown reference. It lists every field as a nested list with its type,
whether it's required, its description, accepted values, defaults, and
constraints. Unions list each option by name, and map-like objects show
the shape of their values.

This changes only the generated markdown. Reference pages render the
same as before.

Reference pages don't have their own `.md` versions yet. That's tracked
in [DOCS-1332](https://linear.app/supabase/issue/DOCS-1332). The new
renderer takes one endpoint's request body and returns markdown, so a
per-page generator can reuse it.

## Follow-ups

- **Reference page HTML (rest of DOCS-1823):** nested schema fields
still reach the HTML only after someone opens their control. Moving
these controls to native `<details>` also needs three fixes found in
review:
- A union option puts an `<li>` inside another `<li>`, which breaks the
list once the content is in the HTML.
- The **Items** control for an array of union items, such as the
notebook `cells` array, opens an empty panel.
- The new summary styles repeat the current trigger's styles. They
should share one base.
- **Parameters:** path and query parameters still use the older one-line
format, so their accepted values don't appear in the markdown reference.
For example, `continent` on the available regions endpoint lists no
values.
- **Shared schema helpers:** the renderer copies `asSchemaArray` from
`Reference.ui.tsx` and adds its own schema type. Sharing both through
`Reference.api.utils.ts` keeps the HTML and the markdown in step.

## Review category (docs PRs only)

- [ ] Critical: needs a review as soon as possible
- [ ] Urgent: the feature ships soon, or it's blocking something else
- [x] Moderate: good to have soon, not urgent
- [ ] Low: get to it when you can

## Review instructions

1. Open the [Management API markdown reference on the
preview](https://docs-git-alexisa-docs-1823-supabase.vercel.app/docs/markdown/reference/api.md).
On production, the same file is served at `/llms/api.txt`.
2. Under **Creates organization invitations**, see the **Request body**
section with `data`, `type`, `attributes`, `email`, `role`, `projects`,
and `require_sso`.
3. Under **Assign or change an organization member role**, see
`data.attributes.role` and `projects`.

## Checklist

- [x] I have read
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
- [ ] If I wrote a new docs topic or edited an existing topic, I used
the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs
[style
guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide)
2026-12-01 11:15:33 -07:00

209 lines
6.7 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { renderRequestBodyMarkdown } from './api-request-body-markdown'
describe('renderRequestBodyMarkdown', () => {
it('should return null without a request body schema', () => {
expect(renderRequestBodyMarkdown(undefined)).toBeNull()
expect(renderRequestBodyMarkdown({ content: {} })).toBeNull()
})
it('should render nested array items and objects', () => {
expect(
renderRequestBodyMarkdown({
required: true,
content: {
'application/json': {
schema: {
type: 'object',
required: ['data'],
properties: {
data: {
type: 'array',
minItems: 1,
items: {
type: 'object',
required: ['attributes'],
properties: {
attributes: {
type: 'object',
required: ['role'],
properties: {
role: {
type: 'string',
description: 'Role name.',
enum: ['owner', 'developer'],
},
projects: {
type: 'array',
items: {
type: 'object',
required: ['ref'],
properties: { ref: { type: 'string' } },
},
},
},
},
},
},
},
},
},
},
},
})
).toMatchInlineSnapshot(`
"### Request body
Content type \`application/json\` (object, required)
- \`data\` (array of object, required): Constraints: min items 1.
- \`attributes\` (object, required)
- \`role\` (string, required): Role name. Accepted values: \`owner\`, \`developer\`.
- \`projects\` (array of object, optional)
- \`ref\` (string, required)"
`)
})
it('should render each option of a union with its accepted values', () => {
expect(
renderRequestBodyMarkdown({
content: {
'application/json': {
schema: {
type: 'object',
properties: {
variant: {
anyOf: [
{ type: 'string', enum: ['a', 'b'] },
{
type: 'object',
properties: {
size: { type: 'integer', minimum: 1, description: 'Size in GB' },
},
},
],
},
},
},
},
},
})
).toMatchInlineSnapshot(`
"### Request body
Content type \`application/json\` (object)
- \`variant\` (string or object, optional)
- Any of the following:
- Option 1 (string): Accepted values: \`a\`, \`b\`.
- Option 2 (object)
- \`size\` (integer, optional): Size in GB. Constraints: minimum 1."
`)
})
it('should render const values, option titles, maps, defaults, and nullable unions', () => {
expect(
renderRequestBodyMarkdown({
content: {
'application/json': {
schema: {
type: 'object',
properties: {
type: { type: 'string', const: 'endpoint' },
config: {
anyOf: [
{ title: 'webhook', type: 'object', properties: { url: { type: 'string' } } },
{
title: 'datadog',
type: 'object',
properties: { region: { type: 'string' } },
},
],
},
headers: { type: 'object', additionalProperties: { type: 'string' } },
extra: { type: 'object', additionalProperties: {} },
label: {
anyOf: [{ type: 'string', maxLength: 512 }, { type: 'null' }],
default: 'none',
},
files: { type: 'array', items: { type: 'string', format: 'binary' } },
},
},
},
},
})
).toMatchInlineSnapshot(`
"### Request body
Content type \`application/json\` (object)
- \`type\` (string, optional): Accepted values: \`endpoint\`.
- \`config\` (object, optional)
- Any of the following:
- Option 1: webhook (object)
- \`url\` (string, optional)
- Option 2: datadog (object)
- \`region\` (string, optional)
- \`headers\` (object of string, optional)
- Any other key (string)
- \`extra\` (object, optional)
- \`label\` (string, optional, nullable): Default: \`none\`. Constraints: max length 512.
- \`files\` (array of string, optional): Constraints: each item: format \`binary\`."
`)
})
it('should render body notes, boolean bounds, closed objects, refs, and backtick values', () => {
expect(
renderRequestBodyMarkdown({
required: true,
content: {
'application/json': {
schema: {
type: 'array',
maxItems: 100,
items: {
type: 'object',
additionalProperties: false,
properties: {
size: {
type: 'integer',
minimum: 0,
exclusiveMinimum: true,
maximum: Number.MAX_SAFE_INTEGER,
},
characters: { type: 'string', enum: ['a`b', ''] },
value: { $ref: '#/components/schemas/JsonValue' },
},
},
},
},
'application/octet-stream': {
schema: {
type: 'string',
format: 'binary',
description: 'Compressed file.\n\n```js\nawait upload(file)\n```',
},
},
},
})
).toMatchInlineSnapshot(`
"### Request body
Content type \`application/json\` (array of object, required): Constraints: max items 100, each item: no other keys.
- \`size\` (integer, optional): Constraints: greater than 0.
- \`characters\` (string, optional): Accepted values: \`\` a\`b \`\`, \`""\`.
- \`value\` (JsonValue, optional)
Content type \`application/octet-stream\` (string, required): Compressed file.
\`\`\`js
await upload(file)
\`\`\`
Constraints: format \`binary\`."
`)
})
})