Skip to content

Commit 45404ae

Browse files
committed
Support for x-gitbook-enum
1 parent 90b25a7 commit 45404ae

File tree

4 files changed

+55
-18
lines changed

4 files changed

+55
-18
lines changed

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,11 @@
244244

245245
/* Schema Enum */
246246
.openapi-schema-enum {
247-
@apply flex flex-row text-sm leading-relaxed flex-wrap text-tint;
247+
@apply flex flex-row text-sm leading-relaxed gap-2 flex-wrap text-tint;
248+
}
249+
250+
.openapi-schema-enum-list {
251+
@apply flex flex-row gap-1.5 items-center;
248252
}
249253

250254
.openapi-schema-enum-value {

packages/openapi-parser/src/types.ts

+15
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,21 @@ export interface OpenAPICustomOperationProperties {
4747
* Description in Document format.
4848
*/
4949
'x-gitbook-description-document'?: object;
50+
51+
/**
52+
* Enums with name and description
53+
*/
54+
'x-enumDescriptions'?: object;
55+
56+
/**
57+
* Enums with name and description
58+
*/
59+
'x-gitbook-enum'?: {
60+
[key: string]: {
61+
description?: string;
62+
name?: string;
63+
};
64+
};
5065
}
5166

5267
/**

packages/react-openapi/src/OpenAPISchema.tsx

+30-16
Original file line numberDiff line numberDiff line change
@@ -242,38 +242,52 @@ function OpenAPISchemaEnum(props: {
242242
}) {
243243
const { schema } = props;
244244

245-
if (!schema.enum || !schema.enum.length || !schema['x-enumDescriptions']) {
245+
if (!schema.enum?.length || (!schema['x-enumDescriptions'] && !schema['x-gitbook-enum'])) {
246246
return null;
247247
}
248248

249249
const enumValues = (() => {
250+
// Render x-gitbook-enum first, as it has a different format
251+
if (schema['x-gitbook-enum']) {
252+
return Object.entries(schema['x-gitbook-enum']).map(([name, { description }]) => {
253+
return {
254+
value: name,
255+
description,
256+
};
257+
});
258+
}
259+
260+
if (schema['x-enumDescriptions']) {
261+
return Object.entries(schema['x-enumDescriptions']).map(([value, description]) => {
262+
return {
263+
value,
264+
description,
265+
};
266+
});
267+
}
268+
250269
return schema.enum.map((value) => {
251270
return {
252271
value,
253-
description: schema['x-enumDescriptions']?.[value],
272+
description: undefined,
254273
};
255274
});
256275
})();
257276

258277
return (
259278
<div className="openapi-schema-enum">
260-
<span>
261-
Options:{' '}
279+
<span>Available options:</span>
280+
<div className="openapi-schema-enum-list">
262281
{enumValues.map((item, index) => (
263282
<span key={index} className="openapi-schema-enum-value">
264-
{item.description ? (
265-
<OpenAPITooltip label={item.description} delay={200}>
266-
<OpenAPITooltip.Button className="cursor-default">
267-
<code>{`${item.value}`}</code>
268-
</OpenAPITooltip.Button>
269-
</OpenAPITooltip>
270-
) : (
271-
<code>{`${item.value}`}</code>
272-
)}
273-
{index < enumValues.length - 1 ? ', ' : ''}
283+
<OpenAPITooltip label={item.description} delay={200}>
284+
<OpenAPITooltip.Button className="cursor-default">
285+
<code>{`${item.value}`}</code>
286+
</OpenAPITooltip.Button>
287+
</OpenAPITooltip>
274288
</span>
275289
))}
276-
</span>
290+
</div>
277291
</div>
278292
);
279293
}
@@ -443,7 +457,7 @@ function getSchemaTitle(schema: OpenAPIV3.SchemaObject): string {
443457
// Otherwise try to infer a nice title
444458
let type = 'any';
445459

446-
if (schema.enum) {
460+
if (schema.enum || schema['x-enumDescriptions'] || schema['x-gitbook-enum']) {
447461
type = `${schema.type} · enum`;
448462
// check array AND schema.items as this is sometimes null despite what the type indicates
449463
} else if (schema.type === 'array' && !!schema.items) {

packages/react-openapi/src/OpenAPITooltip.tsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@ import { Button, type ButtonProps, Tooltip, TooltipTrigger } from 'react-aria-co
44
function OpenAPITooltip(
55
props: TooltipTriggerProps & {
66
children: React.ReactNode;
7-
label: string;
7+
label?: string;
88
}
99
) {
1010
const { children, label } = props;
1111

12+
if (!label) {
13+
return children;
14+
}
15+
1216
return (
1317
<TooltipTrigger {...props}>
1418
{children}

0 commit comments

Comments
 (0)