mirror of
https://github.com/hanzo-docs/docs.git
synced 2026-10-25 04:20:02 +00:00
Fix rendering of OpenAPI nullable defined with allOf. (#1033)
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"fumadocs-openapi": patch
|
||||
---
|
||||
|
||||
Fix rendering of OpenAPI nullable defined with `allOf`.
|
||||
@@ -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)}`;
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user