diff --git a/.changeset/pretty-lies-marry.md b/.changeset/pretty-lies-marry.md new file mode 100644 index 000000000..3dddf9f10 --- /dev/null +++ b/.changeset/pretty-lies-marry.md @@ -0,0 +1,5 @@ +--- +'@gitbook/react-openapi': patch +--- + +Fix recursion in OpenAPISchemaAlternative diff --git a/packages/react-openapi/src/OpenAPISchema.tsx b/packages/react-openapi/src/OpenAPISchema.tsx index b10f65fce..a98aa373c 100644 --- a/packages/react-openapi/src/OpenAPISchema.tsx +++ b/packages/react-openapi/src/OpenAPISchema.tsx @@ -49,20 +49,13 @@ export function OpenAPISchemaProperty( if (alternatives?.[0]?.length) { return ( - - - {alternatives[0].map((alternative, index) => ( - - ))} - {parentCircularRef ? ( - - ) : null} - + ); } @@ -173,6 +166,25 @@ function OpenAPISchemaAlternative(props: { const id = useId(); const subProperties = getSchemaProperties(schema); const description = resolveDescription(schema); + const alternatives = getSchemaAlternatives(schema, new Set(circularRefs?.keys())); + + if (alternatives?.[0]?.length && !subProperties?.length) { + return ( + <> + {description ? ( + + ) : null} + + + + + ); + } return ( <> @@ -193,6 +205,35 @@ function OpenAPISchemaAlternative(props: { ); } +function OpenAPISchemaAlternativesItem( + props: OpenAPISchemaPropertyEntry & { + circularRefs?: CircularRefsIds; + context: OpenAPIClientContext; + alternatives: OpenAPISchemaAlternatives; + parentCircularRef?: string; + } +) { + const id = useId(); + const { schema, circularRefs, context, alternatives, parentCircularRef } = props; + + return ( + + + {alternatives[0].map((alternative, index) => ( + + ))} + {parentCircularRef ? ( + + ) : null} + + ); +} + /** * Render a circular reference to a schema. */ @@ -336,13 +377,18 @@ function getSchemaProperties(schema: OpenAPIV3.SchemaObject): null | OpenAPISche return null; } +type OpenAPISchemaAlternatives = [ + OpenAPIV3.SchemaObject[], + OpenAPIV3.DiscriminatorObject | undefined, +]; + /** * Get the alternatives to display for a schema. */ export function getSchemaAlternatives( schema: OpenAPIV3.SchemaObject, ancestors: Set = new Set() -): null | [OpenAPIV3.SchemaObject[], OpenAPIV3.DiscriminatorObject | undefined] { +): null | OpenAPISchemaAlternatives { const downAncestors = new Set(ancestors).add(schema); if (schema.anyOf) { @@ -408,7 +454,9 @@ export function getSchemaTitle( if (schema.format) { type += ` ยท ${schema.format}`; } - } else if ('anyOf' in schema) { + } + + if ('anyOf' in schema) { type = 'any of'; } else if ('oneOf' in schema) { type = 'one of';