OpenAPI: Support array type in parameters

This commit is contained in:
Fuma Nama
2025-04-25 17:59:50 +08:00
parent 34274467c0
commit 2d18405b79
5 changed files with 31 additions and 25 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'fumadocs-openapi': patch
---
Support array type in parameters
+4 -4
View File
@@ -29,7 +29,7 @@ import { useApiContext, useServerSelectContext } from '@/ui/contexts/api';
import type { FetchResult } from '@/playground/fetcher';
import { FieldSet, JsonInput, ObjectInput } from './inputs';
import type {
PrimitiveRequestField,
ParameterField,
ReferenceSchema,
RequestSchema,
} from '@/playground/index';
@@ -89,7 +89,7 @@ export type ClientProps = HTMLAttributes<HTMLFormElement> & {
authorization?: Security & {
persistentId: string;
};
parameters?: PrimitiveRequestField[];
parameters?: ParameterField[];
body?: RequestSchema & {
mediaType: string;
};
@@ -101,8 +101,8 @@ export type ClientProps = HTMLAttributes<HTMLFormElement> & {
fields?: {
parameter?: CustomField<
`${PrimitiveRequestField['in']}.${string}`,
PrimitiveRequestField
`${ParameterField['in']}.${string}`,
ParameterField
>;
auth?: CustomField<'authorization', RequestSchema>;
body?: CustomField<'body', RequestSchema>;
+6 -10
View File
@@ -12,20 +12,16 @@ import { getSecurities } from '@/utils/get-security';
import { type ClientProps } from './client';
import { ClientLazy } from '@/playground/client.lazy';
interface BaseRequestField {
name: string;
description?: string;
}
interface BaseSchema {
description?: string;
isRequired: boolean;
}
export type PrimitiveRequestField = BaseRequestField &
PrimitiveSchema & {
in: 'cookie' | 'header' | 'query' | 'path';
};
export type ParameterField = (PrimitiveSchema | ArraySchema) & {
name: string;
description?: string;
in: 'cookie' | 'header' | 'query' | 'path';
};
interface PrimitiveSchema extends BaseSchema {
type: 'boolean' | 'string' | 'number';
@@ -198,7 +194,7 @@ function getIdFromSchema(
function parameterToField(
v: NoReference<ParameterObject>,
ctx: Context,
): PrimitiveRequestField {
): ParameterField {
const allowed = ['header', 'cookie', 'query', 'path'] as const;
if (!allowed.includes(v.in as (typeof allowed)[number]))
+2 -1
View File
@@ -482,6 +482,7 @@ function ArrayInput({
} & HTMLAttributes<HTMLDivElement>) {
const { references } = useSchemaContext();
const items = resolve(field.items, references);
const name = fieldName.split('.').at(-1) ?? '';
const { fields, append, remove } = useFieldArray({
name: fieldName,
});
@@ -493,7 +494,7 @@ function ArrayInput({
key={item.id}
name={
<span className="text-fd-muted-foreground">
{fieldName.split('.').at(-1)}[{index}]
{name}[{index}]
</span>
}
field={items}
@@ -5,22 +5,26 @@ export function getPathnameFromInput(
): string {
let pathname = url;
for (const key in path) {
const paramValue = path[key];
if (path[key] === '') continue;
if (typeof paramValue === 'string' && paramValue.length > 0)
pathname = pathname.replace(`{${key}}`, paramValue);
else if (typeof paramValue === 'number')
pathname = pathname.replace(`{${key}}`, paramValue.toString());
if (Array.isArray(path[key])) {
pathname = pathname.replace(`{${key}}`, path[key].join('/'));
} else {
pathname = pathname.replace(`{${key}}`, String(path[key]));
}
}
const searchParams = new URLSearchParams();
for (const key in query) {
const paramValue = query[key];
if (query[key] === '') continue;
if (typeof paramValue === 'string' && paramValue.length > 0)
searchParams.append(key, paramValue);
else if (typeof paramValue === 'number')
searchParams.append(key, paramValue.toString());
if (Array.isArray(query[key])) {
for (const value of query[key]) {
searchParams.append(key, String(value));
}
} else {
searchParams.set(key, String(query[key]));
}
}
return searchParams.size > 0 ? `${pathname}?${searchParams}` : pathname;