OpenAPI: Fix allOf schema display problem

This commit is contained in:
fuma-nama
2024-08-23 00:45:17 +08:00
parent 28bb673d21
commit 5660e1efcb
3 changed files with 30 additions and 20 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'fumadocs-openapi': patch
---
Fix `allOf` schema display problem
+11 -1
View File
@@ -175,7 +175,17 @@ paths:
content:
multipart/form-data:
schema:
$ref: '#/components/schemas/BuyMuseumTicketsRequest'
allOf:
- $ref: '#/components/schemas/BuyMuseumTicketsRequest'
- type: object
properties:
test:
type: string
enum:
- general
- gold
- platinum
- vip
examples:
general_entry:
$ref: '#/components/examples/BuyGeneralTicketsRequestExample'
+14 -19
View File
@@ -118,6 +118,16 @@ export function Schema({
return child;
}
if (schema.allOf && parseObject) {
return (
<Schema
name={name}
schema={combineSchema(noRef(schema.allOf))}
ctx={ctx}
/>
);
}
if (schema.description)
child.push(<Markdown key="description" text={schema.description} />);
@@ -156,7 +166,7 @@ export function Schema({
</p>,
);
if (isObject(schema) && !parseObject) {
if ((isObject(schema) || schema.allOf) && !parseObject) {
child.push(
<renderer.ObjectCollapsible key="attributes" name="Attributes">
<Schema
@@ -170,20 +180,6 @@ export function Schema({
/>
</renderer.ObjectCollapsible>,
);
} else if (schema.allOf) {
child.push(
<renderer.ObjectCollapsible key="attributes" name={name}>
<Schema
name={name}
schema={combineSchema(noRef(schema.allOf))}
ctx={{
...ctx,
parseObject: true,
required: false,
}}
/>
</renderer.ObjectCollapsible>,
);
} else {
const mentionedObjectTypes = [
...(schema.anyOf ?? schema.oneOf ?? []),
@@ -237,24 +233,23 @@ function combineSchema(schema: OpenAPI.SchemaObject[]): OpenAPI.SchemaObject {
};
function add(s: OpenAPI.SchemaObject): void {
result.properties ??= {};
if (s.properties) {
result.properties ??= {};
Object.assign(result.properties, s.properties);
}
result.additionalProperties ??= {};
if (s.additionalProperties === true) {
result.additionalProperties = true;
} else if (
s.additionalProperties &&
typeof result.additionalProperties !== 'boolean'
) {
result.additionalProperties ??= {};
Object.assign(result.additionalProperties, s.additionalProperties);
}
if (s.allOf) {
add(combineSchema(noRef(s.allOf)));
noRef(s.allOf).forEach(add);
}
}