Skip to content

Commit 344842f

Browse files
authored
Improve OpenAPI circular references (#3821)
1 parent c51076e commit 344842f

File tree

3 files changed

+61
-41
lines changed

3 files changed

+61
-41
lines changed

.changeset/yellow-jobs-nail.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@gitbook/react-openapi': patch
3+
'gitbook': patch
4+
---
5+
6+
Improve OpenAPI circular references

packages/gitbook/src/components/DocumentView/OpenAPI/style.css

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@
182182

183183
/* Schema Presentation */
184184
.openapi-schema-presentation {
185-
@apply flex flex-col gap-1 font-normal;
185+
@apply flex flex-col gap-1 font-normal scroll-mt-[calc(var(--toc-top-offset)+0.5rem)];
186186
}
187187

188188
.openapi-schema-properties:last-child {
@@ -249,15 +249,15 @@
249249
}
250250

251251
.openapi-schema-circular {
252-
@apply text-xs text-tint;
252+
@apply text-sm text-tint;
253253
}
254254

255255
.openapi-schema-circular a {
256256
@apply underline;
257257
}
258258

259259
.openapi-schema-circular-glyph {
260-
@apply text-base;
260+
@apply text-base mr-1;
261261
}
262262

263263
/* Schema Enum */

packages/react-openapi/src/OpenAPISchema.tsx

Lines changed: 52 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,13 @@ function OpenAPISchemaProperty(
4747
const circularRefId = parentCircularRefs.get(schema);
4848
// Avoid recursing infinitely, and instead render a link to the parent schema
4949
if (circularRefId) {
50-
return <OpenAPISchemaCircularRef id={circularRefId} schema={schema} />;
50+
return (
51+
<OpenAPISchemaPresentation
52+
context={context}
53+
property={property}
54+
circularRefId={circularRefId}
55+
/>
56+
);
5157
}
5258

5359
const circularRefs = new Map(parentCircularRefs);
@@ -58,7 +64,7 @@ function OpenAPISchemaProperty(
5864
const ancestors = new Set(circularRefs.keys());
5965
const alternatives = getSchemaAlternatives(schema, ancestors);
6066

61-
const header = <OpenAPISchemaPresentation context={context} property={property} />;
67+
const header = <OpenAPISchemaPresentation id={id} context={context} property={property} />;
6268
const content = (() => {
6369
if (alternatives?.schemas) {
6470
const { schemas, discriminator } = alternatives;
@@ -101,10 +107,8 @@ function OpenAPISchemaProperty(
101107
return (
102108
<OpenAPIDisclosure
103109
icon={context.icons.plus}
104-
className={clsx('openapi-schema', className)}
105110
header={header}
106111
label={(isExpanded) => getDisclosureLabel({ schema, isExpanded, context })}
107-
{...rest}
108112
>
109113
{content}
110114
</OpenAPIDisclosure>
@@ -289,8 +293,8 @@ function OpenAPISchemaCircularRef(props: { id: string; schema: OpenAPIV3.SchemaO
289293

290294
return (
291295
<div className="openapi-schema-circular">
296+
<span className="openapi-schema-circular-glyph"></span>
292297
Circular reference to <a href={`#${id}`}>{getSchemaTitle(schema)}</a>{' '}
293-
<span className="openapi-schema-circular-glyph"></span>
294298
</div>
295299
);
296300
}
@@ -359,19 +363,23 @@ function OpenAPISchemaEnum(props: {
359363
* Render the top row of a schema. e.g: name, type, and required status.
360364
*/
361365
export function OpenAPISchemaPresentation(props: {
366+
id?: string;
362367
property: OpenAPISchemaPropertyEntry;
363368
context: OpenAPIClientContext;
369+
circularRefId?: string;
364370
}) {
365371
const {
372+
id,
366373
property: { schema, propertyName, required, isDiscriminatorProperty },
374+
circularRefId,
367375
context,
368376
} = props;
369377

370378
const description = resolveDescription(schema);
371379
const example = resolveFirstExample(schema);
372380

373381
return (
374-
<div className="openapi-schema-presentation">
382+
<div id={id} className="openapi-schema-presentation">
375383
<OpenAPISchemaName
376384
schema={schema}
377385
type={getSchemaTitle(schema)}
@@ -380,38 +388,44 @@ export function OpenAPISchemaPresentation(props: {
380388
required={required}
381389
context={context}
382390
/>
383-
{typeof schema['x-deprecated-sunset'] === 'string' ? (
384-
<div className="openapi-deprecated-sunset openapi-schema-description openapi-markdown">
385-
Sunset date:{' '}
386-
<span className="openapi-deprecated-sunset-date">
387-
{schema['x-deprecated-sunset']}
388-
</span>
389-
</div>
390-
) : null}
391-
{description ? (
392-
<Markdown source={description} className="openapi-schema-description" />
393-
) : null}
394-
{schema.default !== undefined ? (
395-
<span className="openapi-schema-default">
396-
Default:{' '}
397-
<code>
398-
{typeof schema.default === 'string' && schema.default
399-
? schema.default
400-
: stringifyOpenAPI(schema.default)}
401-
</code>
402-
</span>
403-
) : null}
404-
{typeof example === 'string' ? (
405-
<span className="openapi-schema-example">
406-
Example: <code>{example}</code>
407-
</span>
408-
) : null}
409-
{schema.pattern ? (
410-
<span className="openapi-schema-pattern">
411-
Pattern: <code>{schema.pattern}</code>
412-
</span>
413-
) : null}
414-
<OpenAPISchemaEnum schema={schema} context={context} />
391+
{circularRefId ? (
392+
<OpenAPISchemaCircularRef id={circularRefId} schema={schema} />
393+
) : (
394+
<>
395+
{typeof schema['x-deprecated-sunset'] === 'string' ? (
396+
<div className="openapi-deprecated-sunset openapi-schema-description openapi-markdown">
397+
Sunset date:{' '}
398+
<span className="openapi-deprecated-sunset-date">
399+
{schema['x-deprecated-sunset']}
400+
</span>
401+
</div>
402+
) : null}
403+
{description ? (
404+
<Markdown source={description} className="openapi-schema-description" />
405+
) : null}
406+
{schema.default !== undefined ? (
407+
<span className="openapi-schema-default">
408+
Default:{' '}
409+
<code>
410+
{typeof schema.default === 'string' && schema.default
411+
? schema.default
412+
: stringifyOpenAPI(schema.default)}
413+
</code>
414+
</span>
415+
) : null}
416+
{typeof example === 'string' ? (
417+
<span className="openapi-schema-example">
418+
Example: <code>{example}</code>
419+
</span>
420+
) : null}
421+
{schema.pattern ? (
422+
<span className="openapi-schema-pattern">
423+
Pattern: <code>{schema.pattern}</code>
424+
</span>
425+
) : null}
426+
<OpenAPISchemaEnum schema={schema} context={context} />
427+
</>
428+
)}
415429
</div>
416430
);
417431
}

0 commit comments

Comments
 (0)