Fix rendering of OpenAPI nullable defined with allOf. (#1033)

This commit is contained in:
Kevin Chen
2024-11-06 07:47:15 +08:00
committed by GitHub
parent 055b4df0e7
commit 4e76165ec3
3 changed files with 39 additions and 6 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"fumadocs-openapi": patch
---
Fix rendering of OpenAPI nullable defined with `allOf`.
+20 -5
View File
@@ -71,7 +71,11 @@ export function Schema({
const child: ReactNode[] = [];
// object type
if (isObject(schema) && parseObject) {
if (
isObject(schema) &&
parseObject &&
(schema.additionalProperties || schema.properties)
) {
const { additionalProperties, properties } = schema;
if (additionalProperties === true) {
@@ -249,10 +253,21 @@ function getSchemaType(schema: OpenAPI.SchemaObject, ctx: Context): string {
.map((one) => getSchemaType(noRef(one), ctx))
.join(' | ');
if (schema.allOf)
return schema.allOf
.map((one) => getSchemaType(noRef(one), ctx))
.join(' & ');
if (schema.allOf) {
const allTypeNames = schema.allOf.map((one) =>
getSchemaType(noRef(one), ctx),
);
const hasNull = allTypeNames.includes('null');
const nonNullTypes = allTypeNames.filter((v) => v !== 'null');
const nonNullTypeNames = nonNullTypes.join(' & ');
if (!hasNull) return nonNullTypeNames;
if (nonNullTypes.length === 0) return 'null';
else if (nonNullTypes.length === 1 || !hasNull)
return `${nonNullTypeNames} | null`;
else return `(${nonNullTypeNames}) | null`;
}
if (schema.not) return `not ${getSchemaType(noRef(schema.not), ctx)}`;
+14 -1
View File
@@ -8,10 +8,14 @@ export function combineSchema(
schema: OpenAPI.SchemaObject[],
): OpenAPI.SchemaObject {
const result: OpenAPI.SchemaObject = {
type: 'object',
type: undefined,
};
function add(s: OpenAPI.SchemaObject): void {
if (s.type) {
result.type = result.type ? 'object' : s.type;
}
if (s.properties) {
result.properties ??= {};
Object.assign(result.properties, s.properties);
@@ -32,6 +36,15 @@ export function combineSchema(
result.required.push(...s.required);
}
if (s.enum && s.enum.length > 0) {
result.enum ??= [];
result.enum.push(...s.enum);
}
if (s.nullable) {
result.nullable = true;
}
if (s.allOf) {
noRef(s.allOf).forEach(add);
}