OpenAPI: Show required label on body parameters

This commit is contained in:
fuma-nama
2024-08-19 21:31:05 +08:00
parent 755554d6ac
commit 3e4627a0da
2 changed files with 32 additions and 17 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'fumadocs-openapi': patch
---
Show required label on body parameters
+27 -17
View File
@@ -7,7 +7,6 @@ import { Markdown } from './markdown';
const keys: {
[T in keyof OpenAPI.SchemaObject]: string;
} = {
example: 'Example',
default: 'Default',
minimum: 'Minimum',
maximum: 'Maximum',
@@ -64,21 +63,12 @@ export function Schema({
(schema.writeOnly === true && !ctx.writeOnly)
)
return null;
const parseObject = ctx.parseObject ?? true;
const stack = ctx.stack ?? [];
const { renderer } = ctx.render;
const child: ReactNode[] = [];
function field(key: string, value: string): void {
child.push(
<span key={key}>
{key}: <code>{value}</code>
</span>,
);
}
// object type
if (isObject(schema) && parseObject) {
const { additionalProperties, properties } = schema;
@@ -130,20 +120,39 @@ export function Schema({
if (schema.description)
child.push(<Markdown key="description" text={schema.description} />);
const fields: {
key: string;
value: string;
}[] = [];
for (const [key, value] of Object.entries(keys)) {
if (key in schema) {
field(value, JSON.stringify(schema[key as keyof OpenAPI.SchemaObject]));
fields.push({
key: value,
value: JSON.stringify(schema[key as keyof OpenAPI.SchemaObject]),
});
}
}
// enum types
if (schema.enum) {
field(
'Value in',
schema.enum.map((value) => JSON.stringify(value)).join(' | '),
);
fields.push({
key: 'Value in',
value: schema.enum.map((value) => JSON.stringify(value)).join(' | '),
});
}
if (fields.length > 0)
child.push(
<p key="fields">
{fields.map((field) => (
<span key={field.key}>
{field.key}: <code>{field.value}</code>
</span>
))}
</p>,
);
if (isObject(schema) && !parseObject) {
child.push(
<renderer.ObjectCollapsible key="attributes" name="Attributes">
@@ -208,6 +217,7 @@ export function Schema({
<renderer.Property
name={name}
type={getSchemaType(schema, ctx)}
required={ctx.required}
deprecated={schema.deprecated}
>
{child}