Skip to content

Commit 580101d

Browse files
authored
Schemas disclosure label causing client error (#3192)
1 parent 81b4a4d commit 580101d

File tree

6 files changed

+85
-40
lines changed

6 files changed

+85
-40
lines changed

.changeset/sharp-falcons-punch.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+
Fix schemas disclosure label causing client error

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -755,11 +755,16 @@ body:has(.openapi-select-popover) {
755755

756756
.openapi-disclosure:not(
757757
.openapi-disclosure-group .openapi-disclosure,
758-
.openapi-schema-alternatives .openapi-disclosure
758+
.openapi-schema-alternatives .openapi-disclosure,
759+
.openapi-schemas-disclosure .openapi-schema.openapi-disclosure
759760
) {
760761
@apply rounded-xl;
761762
}
762763

764+
.openapi-disclosure .openapi-schemas-disclosure .openapi-schema.openapi-disclosure {
765+
@apply !rounded-none;
766+
}
767+
763768
.openapi-disclosure:has(> .openapi-disclosure-trigger:hover) {
764769
@apply bg-tint-subtle;
765770
}

packages/react-openapi/src/OpenAPISchema.tsx

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { OpenAPIDisclosure } from './OpenAPIDisclosure';
1313
import { OpenAPISchemaName } from './OpenAPISchemaName';
1414
import type { OpenAPIClientContext } from './context';
1515
import { retrocycle } from './decycle';
16+
import { getDisclosureLabel } from './getDisclosureLabel';
1617
import { stringifyOpenAPI } from './stringifyOpenAPI';
1718
import { tString } from './translate';
1819
import { checkIsReference, resolveDescription, resolveFirstExample } from './utils';
@@ -606,6 +607,11 @@ function getSchemaTitle(schema: OpenAPIV3.SchemaObject): string {
606607
if (schema.format) {
607608
type += ` · ${schema.format}`;
608609
}
610+
611+
// Only add the title if it's an object (no need for the title of a string, number, etc.)
612+
if (type === 'object' && schema.title) {
613+
type += ` · ${schema.title.replaceAll(' ', '')}`;
614+
}
609615
}
610616

611617
if ('anyOf' in schema) {
@@ -620,25 +626,3 @@ function getSchemaTitle(schema: OpenAPIV3.SchemaObject): string {
620626

621627
return type;
622628
}
623-
624-
function getDisclosureLabel(props: {
625-
schema: OpenAPIV3.SchemaObject;
626-
isExpanded: boolean;
627-
context: OpenAPIClientContext;
628-
}) {
629-
const { schema, isExpanded, context } = props;
630-
let label: string;
631-
if (schema.type === 'array' && !!schema.items) {
632-
if (schema.items.oneOf) {
633-
label = tString(context.translation, 'available_items').toLowerCase();
634-
} else if (schema.items.enum || schema.items.type === 'object') {
635-
label = tString(context.translation, 'properties').toLowerCase();
636-
} else {
637-
label = schema.items.title ?? schema.title ?? getSchemaTitle(schema.items);
638-
}
639-
} else {
640-
label = schema.title || tString(context.translation, 'properties').toLowerCase();
641-
}
642-
643-
return tString(context.translation, isExpanded ? 'hide' : 'show', label);
644-
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use client';
2+
3+
import type { OpenAPIV3 } from '@gitbook/openapi-parser';
4+
import type { OpenAPIClientContext } from './context';
5+
import { tString } from './translate';
6+
7+
export function getDisclosureLabel(props: {
8+
schema: OpenAPIV3.SchemaObject;
9+
isExpanded: boolean;
10+
context: OpenAPIClientContext;
11+
}) {
12+
const { schema, isExpanded, context } = props;
13+
let label: string;
14+
if (schema.type === 'array' && !!schema.items) {
15+
if (schema.items.oneOf) {
16+
label = tString(context.translation, 'available_items').toLowerCase();
17+
} else {
18+
label = tString(context.translation, 'properties').toLowerCase();
19+
}
20+
} else {
21+
label = tString(context.translation, 'properties').toLowerCase();
22+
}
23+
24+
return tString(context.translation, isExpanded ? 'hide' : 'show', label);
25+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use client';
2+
3+
import { SectionBody } from '../StaticSection';
4+
5+
import type { OpenAPIV3 } from '@gitbook/openapi-parser';
6+
import { OpenAPIDisclosure } from '../OpenAPIDisclosure';
7+
import { OpenAPIRootSchema } from '../OpenAPISchemaServer';
8+
import { Section } from '../StaticSection';
9+
import type { OpenAPIClientContext } from '../context';
10+
import { getDisclosureLabel } from '../getDisclosureLabel';
11+
12+
export function OpenAPISchemaItem(props: {
13+
name: string;
14+
schema: OpenAPIV3.SchemaObject;
15+
context: OpenAPIClientContext;
16+
}) {
17+
const { schema, context, name } = props;
18+
19+
return (
20+
<OpenAPIDisclosure
21+
className="openapi-schemas-disclosure"
22+
key={name}
23+
icon={context.icons.plus}
24+
header={name}
25+
label={(isExpanded) => getDisclosureLabel({ schema, isExpanded, context })}
26+
>
27+
<Section className="openapi-section-schemas">
28+
<SectionBody>
29+
<OpenAPIRootSchema schema={schema} context={context} />
30+
</SectionBody>
31+
</Section>
32+
</OpenAPIDisclosure>
33+
);
34+
}

packages/react-openapi/src/schemas/OpenAPISchemas.tsx

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import type { OpenAPISchema } from '@gitbook/openapi-parser';
22
import clsx from 'clsx';
3-
import { OpenAPIDisclosure } from '../OpenAPIDisclosure';
43
import { OpenAPIExample } from '../OpenAPIExample';
54
import { OpenAPIRootSchema } from '../OpenAPISchemaServer';
6-
import { Section, SectionBody, StaticSection } from '../StaticSection';
5+
import { StaticSection } from '../StaticSection';
76
import {
87
type OpenAPIContextInput,
98
getOpenAPIClientContext,
109
resolveOpenAPIContext,
1110
} from '../context';
12-
import { t, tString } from '../translate';
11+
import { t } from '../translate';
1312
import { getExampleFromSchema } from '../util/example';
13+
import { OpenAPISchemaItem } from './OpenAPISchemaItem';
1414

1515
/**
1616
* OpenAPI Schemas component.
@@ -85,21 +85,12 @@ export function OpenAPISchemas(props: {
8585
<div className={clsx('openapi-schemas', className)}>
8686
{schemas.map(({ name, schema }) => {
8787
return (
88-
<OpenAPIDisclosure
89-
className="openapi-schemas-disclosure"
88+
<OpenAPISchemaItem
9089
key={name}
91-
icon={context.icons.chevronRight}
92-
header={name}
93-
label={(isExpanded) =>
94-
tString(context.translation, isExpanded ? 'hide' : 'show')
95-
}
96-
>
97-
<Section className="openapi-section-schemas">
98-
<SectionBody>
99-
<OpenAPIRootSchema schema={schema} context={clientContext} />
100-
</SectionBody>
101-
</Section>
102-
</OpenAPIDisclosure>
90+
name={name}
91+
context={clientContext}
92+
schema={schema}
93+
/>
10394
);
10495
})}
10596
</div>

0 commit comments

Comments
 (0)