Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/sharp-falcons-punch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@gitbook/react-openapi': patch
'gitbook': patch
---

Fix schemas disclosure label causing client error
Original file line number Diff line number Diff line change
Expand Up @@ -755,11 +755,16 @@ body:has(.openapi-select-popover) {

.openapi-disclosure:not(
.openapi-disclosure-group .openapi-disclosure,
.openapi-schema-alternatives .openapi-disclosure
.openapi-schema-alternatives .openapi-disclosure,
.openapi-schemas-disclosure .openapi-schema.openapi-disclosure
) {
@apply rounded-xl;
}

.openapi-disclosure .openapi-schemas-disclosure .openapi-schema.openapi-disclosure {
@apply !rounded-none;
}

.openapi-disclosure:has(> .openapi-disclosure-trigger:hover) {
@apply bg-tint-subtle;
}
Expand Down
28 changes: 6 additions & 22 deletions packages/react-openapi/src/OpenAPISchema.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { OpenAPIDisclosure } from './OpenAPIDisclosure';
import { OpenAPISchemaName } from './OpenAPISchemaName';
import type { OpenAPIClientContext } from './context';
import { retrocycle } from './decycle';
import { getDisclosureLabel } from './getDisclosureLabel';
import { stringifyOpenAPI } from './stringifyOpenAPI';
import { tString } from './translate';
import { checkIsReference, resolveDescription, resolveFirstExample } from './utils';
Expand Down Expand Up @@ -606,6 +607,11 @@ function getSchemaTitle(schema: OpenAPIV3.SchemaObject): string {
if (schema.format) {
type += ` · ${schema.format}`;
}

// Only add the title if it's an object (no need for the title of a string, number, etc.)
if (type === 'object' && schema.title) {
type += ` · ${schema.title.replaceAll(' ', '')}`;
}
}

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

return type;
}

function getDisclosureLabel(props: {
schema: OpenAPIV3.SchemaObject;
isExpanded: boolean;
context: OpenAPIClientContext;
}) {
const { schema, isExpanded, context } = props;
let label: string;
if (schema.type === 'array' && !!schema.items) {
if (schema.items.oneOf) {
label = tString(context.translation, 'available_items').toLowerCase();
} else if (schema.items.enum || schema.items.type === 'object') {
label = tString(context.translation, 'properties').toLowerCase();
} else {
label = schema.items.title ?? schema.title ?? getSchemaTitle(schema.items);
}
} else {
label = schema.title || tString(context.translation, 'properties').toLowerCase();
}

return tString(context.translation, isExpanded ? 'hide' : 'show', label);
}
25 changes: 25 additions & 0 deletions packages/react-openapi/src/getDisclosureLabel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use client';

import type { OpenAPIV3 } from '@gitbook/openapi-parser';
import type { OpenAPIClientContext } from './context';
import { tString } from './translate';

export function getDisclosureLabel(props: {
schema: OpenAPIV3.SchemaObject;
isExpanded: boolean;
context: OpenAPIClientContext;
}) {
const { schema, isExpanded, context } = props;
let label: string;
if (schema.type === 'array' && !!schema.items) {
if (schema.items.oneOf) {
label = tString(context.translation, 'available_items').toLowerCase();
} else {
label = tString(context.translation, 'properties').toLowerCase();
}
} else {
label = tString(context.translation, 'properties').toLowerCase();
}

return tString(context.translation, isExpanded ? 'hide' : 'show', label);
}
34 changes: 34 additions & 0 deletions packages/react-openapi/src/schemas/OpenAPISchemaItem.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use client';

import { SectionBody } from '../StaticSection';

import type { OpenAPIV3 } from '@gitbook/openapi-parser';
import { OpenAPIDisclosure } from '../OpenAPIDisclosure';
import { OpenAPIRootSchema } from '../OpenAPISchemaServer';
import { Section } from '../StaticSection';
import type { OpenAPIClientContext } from '../context';
import { getDisclosureLabel } from '../getDisclosureLabel';

export function OpenAPISchemaItem(props: {
name: string;
schema: OpenAPIV3.SchemaObject;
context: OpenAPIClientContext;
}) {
const { schema, context, name } = props;

return (
<OpenAPIDisclosure
className="openapi-schemas-disclosure"
key={name}
icon={context.icons.plus}
header={name}
label={(isExpanded) => getDisclosureLabel({ schema, isExpanded, context })}
>
<Section className="openapi-section-schemas">
<SectionBody>
<OpenAPIRootSchema schema={schema} context={context} />
</SectionBody>
</Section>
</OpenAPIDisclosure>
);
}
25 changes: 8 additions & 17 deletions packages/react-openapi/src/schemas/OpenAPISchemas.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import type { OpenAPISchema } from '@gitbook/openapi-parser';
import clsx from 'clsx';
import { OpenAPIDisclosure } from '../OpenAPIDisclosure';
import { OpenAPIExample } from '../OpenAPIExample';
import { OpenAPIRootSchema } from '../OpenAPISchemaServer';
import { Section, SectionBody, StaticSection } from '../StaticSection';
import { StaticSection } from '../StaticSection';
import {
type OpenAPIContextInput,
getOpenAPIClientContext,
resolveOpenAPIContext,
} from '../context';
import { t, tString } from '../translate';
import { t } from '../translate';
import { getExampleFromSchema } from '../util/example';
import { OpenAPISchemaItem } from './OpenAPISchemaItem';

/**
* OpenAPI Schemas component.
Expand Down Expand Up @@ -85,21 +85,12 @@ export function OpenAPISchemas(props: {
<div className={clsx('openapi-schemas', className)}>
{schemas.map(({ name, schema }) => {
return (
<OpenAPIDisclosure
className="openapi-schemas-disclosure"
<OpenAPISchemaItem
key={name}
icon={context.icons.chevronRight}
header={name}
label={(isExpanded) =>
tString(context.translation, isExpanded ? 'hide' : 'show')
}
>
<Section className="openapi-section-schemas">
<SectionBody>
<OpenAPIRootSchema schema={schema} context={clientContext} />
</SectionBody>
</Section>
</OpenAPIDisclosure>
name={name}
context={clientContext}
schema={schema}
/>
);
})}
</div>
Expand Down