Handle OpenAPI alternatives from schema.items (#3235)

This commit is contained in:
Nolann B.
2025-05-14 08:05:08 +02:00
committed by GitHub
parent c6637b09e2
commit 80cb52a237
2 changed files with 39 additions and 5 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@gitbook/react-openapi': patch
---
Handle OpenAPI alternatives from schema.items
+34 -5
View File
@@ -80,11 +80,10 @@ function OpenAPISchemaProperty(
context={context}
/>
{index < alternatives.length - 1 ? (
<span className="openapi-schema-alternative-separator">
{(schema.anyOf || schema.oneOf) &&
tString(context.translation, 'or')}
{schema.allOf && tString(context.translation, 'and')}
</span>
<OpenAPISchemaAlternativeSeparator
schema={schema}
context={context}
/>
) : null}
</div>
))}
@@ -256,6 +255,28 @@ function OpenAPISchemaAlternative(props: {
);
}
function OpenAPISchemaAlternativeSeparator(props: {
schema: OpenAPIV3.SchemaObject;
context: OpenAPIClientContext;
}) {
const { schema, context } = props;
const anyOf = schema.anyOf || schema.items?.anyOf;
const oneOf = schema.oneOf || schema.items?.oneOf;
const allOf = schema.allOf || schema.items?.allOf;
if (!anyOf && !oneOf && !allOf) {
return null;
}
return (
<span className="openapi-schema-alternative-separator">
{(anyOf || oneOf) && tString(context.translation, 'or')}
{allOf && tString(context.translation, 'and')}
</span>
);
}
/**
* Render a circular reference to a schema.
*/
@@ -457,6 +478,14 @@ export function getSchemaAlternatives(
schema: OpenAPIV3.SchemaObject,
ancestors: Set<OpenAPIV3.SchemaObject> = new Set()
): OpenAPIV3.SchemaObject[] | null {
// Search for alternatives in the items property if it exists
if (
schema.items &&
('oneOf' in schema.items || 'allOf' in schema.items || 'anyOf' in schema.items)
) {
return getSchemaAlternatives(schema.items, ancestors);
}
const alternatives:
| [AlternativeType, (OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject)[]]
| null = (() => {