Skip to content

Commit 80cb52a

Browse files
authored
Handle OpenAPI alternatives from schema.items (#3235)
1 parent c6637b0 commit 80cb52a

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

.changeset/silly-gorillas-teach.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@gitbook/react-openapi': patch
3+
---
4+
5+
Handle OpenAPI alternatives from schema.items

packages/react-openapi/src/OpenAPISchema.tsx

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,10 @@ function OpenAPISchemaProperty(
8080
context={context}
8181
/>
8282
{index < alternatives.length - 1 ? (
83-
<span className="openapi-schema-alternative-separator">
84-
{(schema.anyOf || schema.oneOf) &&
85-
tString(context.translation, 'or')}
86-
{schema.allOf && tString(context.translation, 'and')}
87-
</span>
83+
<OpenAPISchemaAlternativeSeparator
84+
schema={schema}
85+
context={context}
86+
/>
8887
) : null}
8988
</div>
9089
))}
@@ -256,6 +255,28 @@ function OpenAPISchemaAlternative(props: {
256255
);
257256
}
258257

258+
function OpenAPISchemaAlternativeSeparator(props: {
259+
schema: OpenAPIV3.SchemaObject;
260+
context: OpenAPIClientContext;
261+
}) {
262+
const { schema, context } = props;
263+
264+
const anyOf = schema.anyOf || schema.items?.anyOf;
265+
const oneOf = schema.oneOf || schema.items?.oneOf;
266+
const allOf = schema.allOf || schema.items?.allOf;
267+
268+
if (!anyOf && !oneOf && !allOf) {
269+
return null;
270+
}
271+
272+
return (
273+
<span className="openapi-schema-alternative-separator">
274+
{(anyOf || oneOf) && tString(context.translation, 'or')}
275+
{allOf && tString(context.translation, 'and')}
276+
</span>
277+
);
278+
}
279+
259280
/**
260281
* Render a circular reference to a schema.
261282
*/
@@ -457,6 +478,14 @@ export function getSchemaAlternatives(
457478
schema: OpenAPIV3.SchemaObject,
458479
ancestors: Set<OpenAPIV3.SchemaObject> = new Set()
459480
): OpenAPIV3.SchemaObject[] | null {
481+
// Search for alternatives in the items property if it exists
482+
if (
483+
schema.items &&
484+
('oneOf' in schema.items || 'allOf' in schema.items || 'anyOf' in schema.items)
485+
) {
486+
return getSchemaAlternatives(schema.items, ancestors);
487+
}
488+
460489
const alternatives:
461490
| [AlternativeType, (OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject)[]]
462491
| null = (() => {

0 commit comments

Comments
 (0)