Skip to content

Commit 47573a6

Browse files
committed
Improve OpenAPI schema style
1 parent c0b339e commit 47573a6

File tree

4 files changed

+58
-30
lines changed

4 files changed

+58
-30
lines changed

.changeset/ninety-sloths-think.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 schema style

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@
155155
/* unstyled */
156156
}
157157

158+
.openapi-schema-root-description.openapi-markdown {
159+
@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;
160+
}
161+
158162
.openapi-schema-properties {
159163
@apply flex flex-col;
160164
}
@@ -203,7 +207,7 @@
203207
.openapi-schema-name {
204208
/* To make double click on the property name select only the name,
205209
we disable selection on the parent and re-enable it on the children. */
206-
@apply select-none flex gap-x-2.5 items-baseline text-sm flex-wrap;
210+
@apply select-none text-sm text-balance *:whitespace-nowrap flex flex-wrap gap-y-1.5 gap-x-2.5;
207211
}
208212

209213
.openapi-schema-name .openapi-deprecated {
@@ -282,7 +286,7 @@
282286

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

288292
.openapi-schema-description.openapi-markdown pre:has(code) {
@@ -302,13 +306,15 @@
302306

303307
/* Schema Examples */
304308
.openapi-schema-example,
305-
.openapi-schema-pattern {
309+
.openapi-schema-pattern,
310+
.openapi-schema-default {
306311
@apply prose-sm text-tint;
307312
}
308313

309314
.openapi-schema-example code,
310315
.openapi-schema-pattern code,
311-
.openapi-schema-enum-value code {
316+
.openapi-schema-enum-value code,
317+
.openapi-schema-default code {
312318
@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;
313319
}
314320

@@ -322,7 +328,7 @@
322328
}
323329

324330
.openapi-securities-description.openapi-markdown {
325-
@apply prose-sm text-tint !font-normal select-text prose-strong:font-semibold prose-strong:text-inherit;
331+
@apply prose-sm text-tint !font-normal select-text text-pretty prose-strong:font-semibold prose-strong:text-inherit;
326332
}
327333

328334
.openapi-securities-label {
@@ -348,7 +354,7 @@
348354
}
349355

350356
.openapi-requestbody-description.openapi-markdown {
351-
@apply prose-sm text-tint !font-normal select-text prose-strong:font-semibold prose-strong:text-inherit;
357+
@apply prose-sm text-tint !font-normal text-pretty select-text prose-strong:font-semibold prose-strong:text-inherit;
352358
}
353359

354360
/* Responses */
@@ -365,7 +371,7 @@
365371
}
366372

367373
.openapi-response-description.openapi-markdown {
368-
@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;
374+
@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;
369375
}
370376

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

packages/react-openapi/src/OpenAPISchema.tsx

Lines changed: 37 additions & 15 deletions
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,27 +146,38 @@ 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

162169
return (
163-
<OpenAPISchemaProperty
164-
className="openapi-schema-root"
165-
property={{ schema }}
166-
context={context}
167-
circularRefs={parentCircularRefs}
168-
/>
170+
<>
171+
{description ? (
172+
<Markdown source={description} className="openapi-schema-root-description" />
173+
) : null}
174+
<OpenAPISchemaProperty
175+
className="openapi-schema-root"
176+
property={{ schema }}
177+
context={context}
178+
circularRefs={parentCircularRefs}
179+
/>
180+
</>
169181
);
170182
}
171183

@@ -322,15 +334,25 @@ function OpenAPISchemaPresentation(props: { property: OpenAPISchemaPropertyEntry
322334
{description ? (
323335
<Markdown source={description} className="openapi-schema-description" />
324336
) : null}
337+
{typeof schema.default !== 'undefined' ? (
338+
<span className="openapi-schema-default">
339+
Default:{' '}
340+
<code>
341+
{typeof schema.default === 'string' && schema.default
342+
? schema.default
343+
: stringifyOpenAPI(schema.default)}
344+
</code>
345+
</span>
346+
) : null}
325347
{typeof example === 'string' ? (
326-
<div className="openapi-schema-example">
348+
<span className="openapi-schema-example">
327349
Example: <code>{example}</code>
328-
</div>
350+
</span>
329351
) : null}
330352
{schema.pattern ? (
331-
<div className="openapi-schema-pattern">
353+
<span className="openapi-schema-pattern">
332354
Pattern: <code>{schema.pattern}</code>
333-
</div>
355+
</span>
334356
) : null}
335357
<OpenAPISchemaEnum schema={schema} />
336358
</div>

packages/react-openapi/src/OpenAPISchemaName.tsx

Lines changed: 2 additions & 8 deletions
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)