Skip to content

Commit 70c4182

Browse files
authored
Improve OpenAPI schema style (#3079)
1 parent b62b101 commit 70c4182

File tree

4 files changed

+47
-24
lines changed

4 files changed

+47
-24
lines changed

.changeset/ninety-sloths-think.md

+6
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 schema style

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

+13-7
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,10 @@
151151
/* unstyled */
152152
}
153153

154+
.openapi-schema-root-description.openapi-markdown {
155+
@apply prose-sm text-balance mt-1.5 !text-[0.813rem] text-tint overflow-hidden !font-normal select-text prose-strong:font-semibold prose-strong:text-inherit;
156+
}
157+
154158
.openapi-schema-properties {
155159
@apply flex flex-col;
156160
}
@@ -199,7 +203,7 @@
199203
.openapi-schema-name {
200204
/* To make double click on the property name select only the name,
201205
we disable selection on the parent and re-enable it on the children. */
202-
@apply select-none flex gap-x-2.5 items-baseline text-sm flex-wrap;
206+
@apply select-none text-sm text-balance *:whitespace-nowrap flex flex-wrap gap-y-1.5 gap-x-2.5;
203207
}
204208

205209
.openapi-schema-name .openapi-deprecated {
@@ -278,7 +282,7 @@
278282

279283
/* Schema Description */
280284
.openapi-schema-description.openapi-markdown {
281-
@apply prose-sm text-tint overflow-hidden !font-normal select-text prose-strong:font-semibold prose-strong:text-inherit;
285+
@apply prose-sm text-tint overflow-hidden text-pretty !font-normal select-text prose-strong:font-semibold prose-strong:text-inherit;
282286
}
283287

284288
.openapi-schema-description.openapi-markdown pre:has(code) {
@@ -298,13 +302,15 @@
298302

299303
/* Schema Examples */
300304
.openapi-schema-example,
301-
.openapi-schema-pattern {
305+
.openapi-schema-pattern,
306+
.openapi-schema-default {
302307
@apply prose-sm text-tint;
303308
}
304309

305310
.openapi-schema-example code,
306311
.openapi-schema-pattern code,
307-
.openapi-schema-enum-value code {
312+
.openapi-schema-enum-value code,
313+
.openapi-schema-default code {
308314
@apply py-px px-1 min-w-[1.625rem] text-tint-strong font-normal w-fit justify-center items-center ring-1 ring-inset ring-tint bg-tint rounded text-xs leading-[calc(max(1.20em,1.25rem))] before:!content-none after:!content-none;
309315
}
310316

@@ -318,7 +324,7 @@
318324
}
319325

320326
.openapi-securities-description.openapi-markdown {
321-
@apply prose-sm text-tint !font-normal select-text prose-strong:font-semibold prose-strong:text-inherit;
327+
@apply prose-sm text-tint !font-normal select-text text-pretty prose-strong:font-semibold prose-strong:text-inherit;
322328
}
323329

324330
.openapi-securities-label {
@@ -344,7 +350,7 @@
344350
}
345351

346352
.openapi-requestbody-description.openapi-markdown {
347-
@apply prose-sm text-tint !font-normal select-text prose-strong:font-semibold prose-strong:text-inherit;
353+
@apply prose-sm text-tint !font-normal text-pretty select-text prose-strong:font-semibold prose-strong:text-inherit;
348354
}
349355

350356
/* Responses */
@@ -361,7 +367,7 @@
361367
}
362368

363369
.openapi-response-description.openapi-markdown {
364-
@apply text-left prose-sm text-[0.813rem] h-auto relative leading-[1.125rem] text-tint !font-normal truncate select-text prose-strong:font-semibold prose-strong:text-inherit;
370+
@apply text-left prose-sm text-[0.813rem] text-pretty h-auto relative leading-[1.125rem] text-tint !font-normal truncate select-text prose-strong:font-semibold prose-strong:text-inherit;
365371
}
366372

367373
.openapi-response-description.openapi-markdown::-webkit-scrollbar {

packages/react-openapi/src/OpenAPISchema.tsx

+26-9
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { OpenAPICopyButton } from './OpenAPICopyButton';
1111
import { OpenAPIDisclosure } from './OpenAPIDisclosure';
1212
import { OpenAPISchemaName } from './OpenAPISchemaName';
1313
import { retrocycle } from './decycle';
14+
import { stringifyOpenAPI } from './stringifyOpenAPI';
1415
import type { OpenAPIClientContext } from './types';
1516
import { checkIsReference, resolveDescription, resolveFirstExample } from './utils';
1617

@@ -145,17 +146,23 @@ function OpenAPIRootSchema(props: {
145146

146147
const id = useId();
147148
const properties = getSchemaProperties(schema);
149+
const description = resolveDescription(schema);
148150

149151
if (properties?.length) {
150152
const circularRefs = new Map(parentCircularRefs);
151153
circularRefs.set(schema, id);
152154

153155
return (
154-
<OpenAPISchemaProperties
155-
properties={properties}
156-
circularRefs={circularRefs}
157-
context={context}
158-
/>
156+
<>
157+
{description ? (
158+
<Markdown source={description} className="openapi-schema-root-description" />
159+
) : null}
160+
<OpenAPISchemaProperties
161+
properties={properties}
162+
circularRefs={circularRefs}
163+
context={context}
164+
/>
165+
</>
159166
);
160167
}
161168

@@ -322,15 +329,25 @@ function OpenAPISchemaPresentation(props: { property: OpenAPISchemaPropertyEntry
322329
{description ? (
323330
<Markdown source={description} className="openapi-schema-description" />
324331
) : null}
332+
{schema.default !== undefined ? (
333+
<span className="openapi-schema-default">
334+
Default:{' '}
335+
<code>
336+
{typeof schema.default === 'string' && schema.default
337+
? schema.default
338+
: stringifyOpenAPI(schema.default)}
339+
</code>
340+
</span>
341+
) : null}
325342
{typeof example === 'string' ? (
326-
<div className="openapi-schema-example">
343+
<span className="openapi-schema-example">
327344
Example: <code>{example}</code>
328-
</div>
345+
</span>
329346
) : null}
330347
{schema.pattern ? (
331-
<div className="openapi-schema-pattern">
348+
<span className="openapi-schema-pattern">
332349
Pattern: <code>{schema.pattern}</code>
333-
</div>
350+
</span>
334351
) : null}
335352
<OpenAPISchemaEnum schema={schema} />
336353
</div>

packages/react-openapi/src/OpenAPISchemaName.tsx

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { OpenAPIV3 } from '@gitbook/openapi-parser';
22
import type React from 'react';
3-
import { stringifyOpenAPI } from './stringifyOpenAPI';
43

54
interface OpenAPISchemaNameProps {
65
schema?: OpenAPIV3.SchemaObject;
@@ -19,7 +18,7 @@ export function OpenAPISchemaName(props: OpenAPISchemaNameProps) {
1918
const additionalItems = schema && getAdditionalItems(schema);
2019

2120
return (
22-
<div className="openapi-schema-name">
21+
<span className="openapi-schema-name">
2322
{propertyName ? (
2423
<span data-deprecated={schema?.deprecated} className="openapi-schema-propertyname">
2524
{propertyName}
@@ -41,7 +40,7 @@ export function OpenAPISchemaName(props: OpenAPISchemaNameProps) {
4140
<span className="openapi-schema-optional">optional</span>
4241
)}
4342
{schema?.deprecated ? <span className="openapi-deprecated">Deprecated</span> : null}
44-
</div>
43+
</span>
4544
);
4645
}
4746

@@ -56,11 +55,6 @@ function getAdditionalItems(schema: OpenAPIV3.SchemaObject): string {
5655
additionalItems += ` · max: ${schema.maximum || schema.maxLength || schema.maxItems}`;
5756
}
5857

59-
// If the schema has a default value, we display it
60-
if (typeof schema.default !== 'undefined') {
61-
additionalItems += ` · default: ${stringifyOpenAPI(schema.default)}`;
62-
}
63-
6458
if (schema.nullable) {
6559
additionalItems = ' | nullable';
6660
}

0 commit comments

Comments
 (0)