OpenAPI: Fix common parameters

This commit is contained in:
fuma-nama
2024-08-19 21:40:06 +08:00
parent 3e4627a0da
commit 10f6f39372
6 changed files with 32 additions and 11 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'fumadocs-openapi': patch
---
Fix common parameters
+5 -5
View File
@@ -19,15 +19,15 @@ export function buildRoutes(
): Map<string, RouteInformation[]> {
const map = new Map<string, RouteInformation[]>();
for (const [path, value] of Object.entries(document.paths)) {
if (!value) continue;
for (const [path, pathItem] of Object.entries(document.paths)) {
if (!pathItem) continue;
const methodMap = new Map<string, MethodInformation[]>();
for (const methodKey of methodKeys) {
const operation = value[methodKey];
const operation = pathItem[methodKey];
if (!operation) continue;
const info = createMethod(methodKey, operation);
const info = createMethod(methodKey, pathItem, operation);
const tags = operation.tags ?? [];
for (const tag of [...tags, 'all']) {
@@ -40,7 +40,7 @@ export function buildRoutes(
for (const [tag, methods] of methodMap.entries()) {
const list = map.get(tag) ?? [];
list.push({
...value,
...pathItem,
path,
methods,
});
+2 -2
View File
@@ -172,7 +172,7 @@ export function Schema({
<renderer.ObjectCollapsible key="attributes" name={name}>
<Schema
name={name}
schema={combineSchema(schema.allOf.map(noRef))}
schema={combineSchema(noRef(schema.allOf))}
ctx={{
...ctx,
parseObject: true,
@@ -251,7 +251,7 @@ function combineSchema(schema: OpenAPI.SchemaObject[]): OpenAPI.SchemaObject {
}
if (s.allOf) {
add(combineSchema(s.allOf.map(noRef)));
add(combineSchema(noRef(s.allOf)));
}
}
+11 -1
View File
@@ -1,13 +1,23 @@
import type { OpenAPIV3 as OpenAPI } from 'openapi-types';
import type { MethodInformation } from '@/types';
import { noRef } from '@/utils/schema';
/**
* Summarize method endpoint information
*/
export function createMethod(
method: string,
path: OpenAPI.PathItemObject,
operation: OpenAPI.OperationObject,
): MethodInformation {
return {
description: path.description,
summary: path.summary,
...operation,
parameters: (operation.parameters ?? []) as OpenAPI.ParameterObject[],
parameters: [
...noRef(operation.parameters ?? []),
...noRef(path.parameters ?? []),
],
method: method.toUpperCase(),
};
}
+6 -2
View File
@@ -46,9 +46,13 @@ export async function APIPage(props: ApiPageProps): Promise<ReactElement> {
return (
<ctx.renderer.Root baseUrl={ctx.baseUrl}>
{operations.map((item) => {
const operation = document.paths[item.path]?.[item.method];
const pathItem = document.paths[item.path];
if (!pathItem) return null;
const operation = pathItem[item.method];
if (!operation) return null;
const method = createMethod(item.method, operation);
const method = createMethod(item.method, pathItem, operation);
return (
<Operation
+3 -1
View File
@@ -1,6 +1,8 @@
import type { OpenAPIV3 as OpenAPI } from 'openapi-types';
type NoReference<T> = Exclude<T, OpenAPI.ReferenceObject>;
type NoReference<T> = T extends (infer I)[]
? NoReference<I>[]
: Exclude<T, OpenAPI.ReferenceObject>;
export function noRef<T>(v: T): NoReference<T> {
return v as NoReference<T>;