mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-16 15:45:13 +00:00
Fix OpenAPI spec rendering (#3037)
This commit is contained in:
@@ -0,0 +1,6 @@
|
|||||||
|
---
|
||||||
|
"@gitbook/react-openapi": patch
|
||||||
|
"gitbook": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fix spec properties rendering and missing keys
|
||||||
@@ -241,6 +241,7 @@
|
|||||||
"@scalar/oas-utils": "^0.2.120",
|
"@scalar/oas-utils": "^0.2.120",
|
||||||
"clsx": "^2.1.1",
|
"clsx": "^2.1.1",
|
||||||
"flatted": "^3.2.9",
|
"flatted": "^3.2.9",
|
||||||
|
"json-decycle": "^4.0.0",
|
||||||
"json-xml-parse": "^1.3.0",
|
"json-xml-parse": "^1.3.0",
|
||||||
"react-aria": "^3.37.0",
|
"react-aria": "^3.37.0",
|
||||||
"react-aria-components": "^1.6.0",
|
"react-aria-components": "^1.6.0",
|
||||||
@@ -2069,6 +2070,8 @@
|
|||||||
|
|
||||||
"json-buffer": ["json-buffer@3.0.0", "", {}, "sha512-CuUqjv0FUZIdXkHPI8MezCnFCdaTAacej1TZYulLoAg1h/PhwkdXFN4V/gzY4g+fMBCOV2xF+rp7t2XD2ns/NQ=="],
|
"json-buffer": ["json-buffer@3.0.0", "", {}, "sha512-CuUqjv0FUZIdXkHPI8MezCnFCdaTAacej1TZYulLoAg1h/PhwkdXFN4V/gzY4g+fMBCOV2xF+rp7t2XD2ns/NQ=="],
|
||||||
|
|
||||||
|
"json-decycle": ["json-decycle@4.0.0", "", {}, "sha512-3GFL/vWazCbMu1kw+NdIfAHh6Ugq5pxkKcSUnK1f/Fw1nDtt1i+BiBfRJs0iPEKscYAz4k4+osvgjY95hmuJXQ=="],
|
||||||
|
|
||||||
"json-parse-even-better-errors": ["json-parse-even-better-errors@2.3.1", "", {}, "sha512-xyFwyhro/JEof6Ghe2iz2NcXoj2sloNsWr/XsERDK/oiPCfaNhl5ONfp+jQdAZRQQ0IJWNzH9zIZF7li91kh2w=="],
|
"json-parse-even-better-errors": ["json-parse-even-better-errors@2.3.1", "", {}, "sha512-xyFwyhro/JEof6Ghe2iz2NcXoj2sloNsWr/XsERDK/oiPCfaNhl5ONfp+jQdAZRQQ0IJWNzH9zIZF7li91kh2w=="],
|
||||||
|
|
||||||
"json-schema": ["json-schema@0.4.0", "", {}, "sha512-es94M3nTIfsEPisRafak+HDLfHXnKBhV3vU5eqPcS3flIWqcxJWgXHXiey3YrpaNsanY5ei1VoYEbOzijuq9BA=="],
|
"json-schema": ["json-schema@0.4.0", "", {}, "sha512-es94M3nTIfsEPisRafak+HDLfHXnKBhV3vU5eqPcS3flIWqcxJWgXHXiey3YrpaNsanY5ei1VoYEbOzijuq9BA=="],
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ export function UnwrappedBlocks<TBlock extends DocumentBlock>(props: UnwrappedBl
|
|||||||
const { nodes, blockStyle, isOffscreen: defaultIsOffscreen = false, ...contextProps } = props;
|
const { nodes, blockStyle, isOffscreen: defaultIsOffscreen = false, ...contextProps } = props;
|
||||||
|
|
||||||
let isOffscreen = defaultIsOffscreen;
|
let isOffscreen = defaultIsOffscreen;
|
||||||
return nodes.map((node) => {
|
return nodes.map((node, index) => {
|
||||||
isOffscreen =
|
isOffscreen =
|
||||||
isOffscreen ||
|
isOffscreen ||
|
||||||
isBlockOffscreen({
|
isBlockOffscreen({
|
||||||
@@ -65,7 +65,7 @@ export function UnwrappedBlocks<TBlock extends DocumentBlock>(props: UnwrappedBl
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<Block
|
<Block
|
||||||
key={node.key}
|
key={node.key || `${node.type}-${index}`}
|
||||||
block={node}
|
block={node}
|
||||||
style={[
|
style={[
|
||||||
'mx-auto w-full decoration-primary/6',
|
'mx-auto w-full decoration-primary/6',
|
||||||
|
|||||||
@@ -24,14 +24,16 @@ export function Inlines<T extends DocumentInline | DocumentText>(
|
|||||||
) {
|
) {
|
||||||
const { nodes, document, ancestorInlines, ...contextProps } = props;
|
const { nodes, document, ancestorInlines, ...contextProps } = props;
|
||||||
|
|
||||||
return nodes.map((node) => {
|
return nodes.map((node, index) => {
|
||||||
|
const key = node.key || `key-${index}`;
|
||||||
|
|
||||||
if (node.object === 'text') {
|
if (node.object === 'text') {
|
||||||
return <Text key={node.key} text={node} />;
|
return <Text key={key} text={node} />;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Inline
|
<Inline
|
||||||
key={node.key}
|
key={key}
|
||||||
inline={node}
|
inline={node}
|
||||||
document={document}
|
document={document}
|
||||||
ancestorInlines={ancestorInlines}
|
ancestorInlines={ancestorInlines}
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
"@scalar/oas-utils": "^0.2.120",
|
"@scalar/oas-utils": "^0.2.120",
|
||||||
"clsx": "^2.1.1",
|
"clsx": "^2.1.1",
|
||||||
"flatted": "^3.2.9",
|
"flatted": "^3.2.9",
|
||||||
|
"json-decycle": "^4.0.0",
|
||||||
"json-xml-parse": "^1.3.0",
|
"json-xml-parse": "^1.3.0",
|
||||||
"react-aria-components": "^1.6.0",
|
"react-aria-components": "^1.6.0",
|
||||||
"react-aria": "^3.37.0",
|
"react-aria": "^3.37.0",
|
||||||
|
|||||||
@@ -32,8 +32,6 @@ export function InteractiveSection(props: {
|
|||||||
defaultTab?: string;
|
defaultTab?: string;
|
||||||
/** Content of the header */
|
/** Content of the header */
|
||||||
header?: React.ReactNode;
|
header?: React.ReactNode;
|
||||||
/** Body of the section */
|
|
||||||
children?: React.ReactNode;
|
|
||||||
/** Children to display within the container */
|
/** Children to display within the container */
|
||||||
overlay?: React.ReactNode;
|
overlay?: React.ReactNode;
|
||||||
}) {
|
}) {
|
||||||
@@ -45,7 +43,6 @@ export function InteractiveSection(props: {
|
|||||||
tabs = [],
|
tabs = [],
|
||||||
defaultTab = tabs[0]?.key,
|
defaultTab = tabs[0]?.key,
|
||||||
header,
|
header,
|
||||||
children,
|
|
||||||
overlay,
|
overlay,
|
||||||
toggleIcon = '▶',
|
toggleIcon = '▶',
|
||||||
} = props;
|
} = props;
|
||||||
@@ -83,7 +80,7 @@ export function InteractiveSection(props: {
|
|||||||
className={className}
|
className={className}
|
||||||
>
|
>
|
||||||
<SectionHeaderContent className={className}>
|
<SectionHeaderContent className={className}>
|
||||||
{(children || selectedTab?.body) && toggeable ? (
|
{selectedTab?.body && toggeable ? (
|
||||||
<button
|
<button
|
||||||
{...mergeProps(buttonProps, focusProps)}
|
{...mergeProps(buttonProps, focusProps)}
|
||||||
ref={triggerRef}
|
ref={triggerRef}
|
||||||
@@ -131,9 +128,8 @@ export function InteractiveSection(props: {
|
|||||||
</div>
|
</div>
|
||||||
</SectionHeader>
|
</SectionHeader>
|
||||||
) : null}
|
) : null}
|
||||||
{(!toggeable || state.isExpanded) && (children || selectedTab?.body) ? (
|
{(!toggeable || state.isExpanded) && selectedTab?.body ? (
|
||||||
<SectionBody ref={panelRef} {...panelProps} className={className}>
|
<SectionBody ref={panelRef} {...panelProps} className={className}>
|
||||||
{children}
|
|
||||||
{selectedTab?.body}
|
{selectedTab?.body}
|
||||||
</SectionBody>
|
</SectionBody>
|
||||||
) : null}
|
) : null}
|
||||||
|
|||||||
@@ -148,7 +148,13 @@ function generateCodeSamples(props: {
|
|||||||
return {
|
return {
|
||||||
key: `default-${generator.id}`,
|
key: `default-${generator.id}`,
|
||||||
label: generator.label,
|
label: generator.label,
|
||||||
body: <OpenAPIMediaTypeExamplesBody data={data} renderers={renderers} />,
|
body: (
|
||||||
|
<OpenAPIMediaTypeExamplesBody
|
||||||
|
method={data.method}
|
||||||
|
path={data.path}
|
||||||
|
renderers={renderers}
|
||||||
|
/>
|
||||||
|
),
|
||||||
footer: (
|
footer: (
|
||||||
<OpenAPICodeSampleFooter renderers={renderers} data={data} context={context} />
|
<OpenAPICodeSampleFooter renderers={renderers} data={data} context={context} />
|
||||||
),
|
),
|
||||||
@@ -189,9 +195,9 @@ function OpenAPICodeSampleFooter(props: {
|
|||||||
const { method, path } = data;
|
const { method, path } = data;
|
||||||
const { specUrl } = context;
|
const { specUrl } = context;
|
||||||
const hideTryItPanel = data['x-hideTryItPanel'] || data.operation['x-hideTryItPanel'];
|
const hideTryItPanel = data['x-hideTryItPanel'] || data.operation['x-hideTryItPanel'];
|
||||||
const hasMediaTypes = renderers.length > 0;
|
const hasMultipleMediaTypes = renderers.length > 1;
|
||||||
|
|
||||||
if (hideTryItPanel && !hasMediaTypes) {
|
if (hideTryItPanel && !hasMultipleMediaTypes) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -201,8 +207,12 @@ function OpenAPICodeSampleFooter(props: {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="openapi-codesample-footer">
|
<div className="openapi-codesample-footer">
|
||||||
{hasMediaTypes ? (
|
{hasMultipleMediaTypes ? (
|
||||||
<OpenAPIMediaTypeExamplesSelector data={data} renderers={renderers} />
|
<OpenAPIMediaTypeExamplesSelector
|
||||||
|
method={data.method}
|
||||||
|
path={data.path}
|
||||||
|
renderers={renderers}
|
||||||
|
/>
|
||||||
) : (
|
) : (
|
||||||
<span />
|
<span />
|
||||||
)}
|
)}
|
||||||
|
|||||||
@@ -3,10 +3,9 @@ import clsx from 'clsx';
|
|||||||
import { useCallback } from 'react';
|
import { useCallback } from 'react';
|
||||||
import { useStore } from 'zustand';
|
import { useStore } from 'zustand';
|
||||||
import type { MediaTypeRenderer } from './OpenAPICodeSample';
|
import type { MediaTypeRenderer } from './OpenAPICodeSample';
|
||||||
import type { OpenAPIOperationData } from './types';
|
|
||||||
import { getOrCreateTabStoreByKey } from './useSyncedTabsGlobalState';
|
import { getOrCreateTabStoreByKey } from './useSyncedTabsGlobalState';
|
||||||
|
|
||||||
function useMediaTypeState(data: OpenAPIOperationData, defaultKey: string) {
|
function useMediaTypeState(data: { method: string; path: string }, defaultKey: string) {
|
||||||
const { method, path } = data;
|
const { method, path } = data;
|
||||||
const store = useStore(getOrCreateTabStoreByKey(`media-type-${method}-${path}`, defaultKey));
|
const store = useStore(getOrCreateTabStoreByKey(`media-type-${method}-${path}`, defaultKey));
|
||||||
if (typeof store.tabKey !== 'string') {
|
if (typeof store.tabKey !== 'string') {
|
||||||
@@ -18,7 +17,7 @@ function useMediaTypeState(data: OpenAPIOperationData, defaultKey: string) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function useMediaTypeSampleIndexState(data: OpenAPIOperationData, mediaType: string) {
|
function useMediaTypeSampleIndexState(data: { method: string; path: string }, mediaType: string) {
|
||||||
const { method, path } = data;
|
const { method, path } = data;
|
||||||
const store = useStore(
|
const store = useStore(
|
||||||
getOrCreateTabStoreByKey(`media-type-sample-${mediaType}-${method}-${path}`, 0)
|
getOrCreateTabStoreByKey(`media-type-sample-${mediaType}-${method}-${path}`, 0)
|
||||||
@@ -33,14 +32,15 @@ function useMediaTypeSampleIndexState(data: OpenAPIOperationData, mediaType: str
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function OpenAPIMediaTypeExamplesSelector(props: {
|
export function OpenAPIMediaTypeExamplesSelector(props: {
|
||||||
data: OpenAPIOperationData;
|
method: string;
|
||||||
|
path: string;
|
||||||
renderers: MediaTypeRenderer[];
|
renderers: MediaTypeRenderer[];
|
||||||
}) {
|
}) {
|
||||||
const { data, renderers } = props;
|
const { method, path, renderers } = props;
|
||||||
if (!renderers[0]) {
|
if (!renderers[0]) {
|
||||||
throw new Error('No renderers provided');
|
throw new Error('No renderers provided');
|
||||||
}
|
}
|
||||||
const state = useMediaTypeState(data, renderers[0].mediaType);
|
const state = useMediaTypeState({ method, path }, renderers[0].mediaType);
|
||||||
const selected = renderers.find((r) => r.mediaType === state.mediaType) || renderers[0];
|
const selected = renderers.find((r) => r.mediaType === state.mediaType) || renderers[0];
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -56,17 +56,18 @@ export function OpenAPIMediaTypeExamplesSelector(props: {
|
|||||||
</option>
|
</option>
|
||||||
))}
|
))}
|
||||||
</select>
|
</select>
|
||||||
<ExamplesSelector data={data} renderer={selected} />
|
<ExamplesSelector method={method} path={path} renderer={selected} />
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function ExamplesSelector(props: {
|
function ExamplesSelector(props: {
|
||||||
data: OpenAPIOperationData;
|
method: string;
|
||||||
|
path: string;
|
||||||
renderer: MediaTypeRenderer;
|
renderer: MediaTypeRenderer;
|
||||||
}) {
|
}) {
|
||||||
const { data, renderer } = props;
|
const { method, path, renderer } = props;
|
||||||
const state = useMediaTypeSampleIndexState(data, renderer.mediaType);
|
const state = useMediaTypeSampleIndexState({ method, path }, renderer.mediaType);
|
||||||
if (renderer.examples.length < 2) {
|
if (renderer.examples.length < 2) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -87,25 +88,26 @@ function ExamplesSelector(props: {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function OpenAPIMediaTypeExamplesBody(props: {
|
export function OpenAPIMediaTypeExamplesBody(props: {
|
||||||
data: OpenAPIOperationData;
|
method: string;
|
||||||
|
path: string;
|
||||||
renderers: MediaTypeRenderer[];
|
renderers: MediaTypeRenderer[];
|
||||||
}) {
|
}) {
|
||||||
const { renderers, data } = props;
|
const { renderers, method, path } = props;
|
||||||
if (!renderers[0]) {
|
if (!renderers[0]) {
|
||||||
throw new Error('No renderers provided');
|
throw new Error('No renderers provided');
|
||||||
}
|
}
|
||||||
const mediaTypeState = useMediaTypeState(data, renderers[0].mediaType);
|
const mediaTypeState = useMediaTypeState({ method, path }, renderers[0].mediaType);
|
||||||
const selected =
|
const selected =
|
||||||
renderers.find((r) => r.mediaType === mediaTypeState.mediaType) ?? renderers[0];
|
renderers.find((r) => r.mediaType === mediaTypeState.mediaType) ?? renderers[0];
|
||||||
if (selected.examples.length === 0) {
|
if (selected.examples.length === 0) {
|
||||||
return selected.element;
|
return selected.element;
|
||||||
}
|
}
|
||||||
return <ExamplesBody data={data} renderer={selected} />;
|
return <ExamplesBody method={method} path={path} renderer={selected} />;
|
||||||
}
|
}
|
||||||
|
|
||||||
function ExamplesBody(props: { data: OpenAPIOperationData; renderer: MediaTypeRenderer }) {
|
function ExamplesBody(props: { method: string; path: string; renderer: MediaTypeRenderer }) {
|
||||||
const { data, renderer } = props;
|
const { method, path, renderer } = props;
|
||||||
const exampleState = useMediaTypeSampleIndexState(data, renderer.mediaType);
|
const exampleState = useMediaTypeSampleIndexState({ method, path }, renderer.mediaType);
|
||||||
const example = renderer.examples[exampleState.index] ?? renderer.examples[0];
|
const example = renderer.examples[exampleState.index] ?? renderer.examples[0];
|
||||||
if (!example) {
|
if (!example) {
|
||||||
throw new Error(`No example found for index ${exampleState.index}`);
|
throw new Error(`No example found for index ${exampleState.index}`);
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ function formatPath(path: string) {
|
|||||||
parts.push(path.slice(lastIndex, offset));
|
parts.push(path.slice(lastIndex, offset));
|
||||||
}
|
}
|
||||||
parts.push(
|
parts.push(
|
||||||
<span key={offset} className="openapi-path-variable">
|
<span key={`offset-${offset}`} className="openapi-path-variable">
|
||||||
{match}
|
{match}
|
||||||
</span>
|
</span>
|
||||||
);
|
);
|
||||||
@@ -61,7 +61,7 @@ function formatPath(path: string) {
|
|||||||
|
|
||||||
const formattedPath = parts.map((part, index) => {
|
const formattedPath = parts.map((part, index) => {
|
||||||
if (typeof part === 'string') {
|
if (typeof part === 'string') {
|
||||||
return <span key={index}>{part}</span>;
|
return <span key={`part-${index}`}>{part}</span>;
|
||||||
}
|
}
|
||||||
return part;
|
return part;
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import type { OpenAPIV3 } from '@gitbook/openapi-parser';
|
import type { OpenAPIV3 } from '@gitbook/openapi-parser';
|
||||||
import { InteractiveSection } from './InteractiveSection';
|
import { InteractiveSection } from './InteractiveSection';
|
||||||
import { OpenAPIRootSchema } from './OpenAPISchema';
|
import { OpenAPIRootSchema } from './OpenAPISchemaServer';
|
||||||
import type { OpenAPIClientContext } from './types';
|
import type { OpenAPIClientContext } from './types';
|
||||||
import { checkIsReference } from './utils';
|
import { checkIsReference } from './utils';
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import type { OpenAPIV3 } from '@gitbook/openapi-parser';
|
import type { OpenAPIV3 } from '@gitbook/openapi-parser';
|
||||||
import { OpenAPIDisclosure } from './OpenAPIDisclosure';
|
import { OpenAPIDisclosure } from './OpenAPIDisclosure';
|
||||||
import { OpenAPISchemaProperties } from './OpenAPISchema';
|
import { OpenAPISchemaProperties } from './OpenAPISchemaServer';
|
||||||
import type { OpenAPIClientContext } from './types';
|
import type { OpenAPIClientContext } from './types';
|
||||||
import { parameterToProperty, resolveDescription } from './utils';
|
import { parameterToProperty, resolveDescription } from './utils';
|
||||||
|
|
||||||
@@ -29,9 +29,9 @@ export function OpenAPIResponse(props: {
|
|||||||
{headers.length > 0 ? (
|
{headers.length > 0 ? (
|
||||||
<OpenAPIDisclosure context={context} label="Headers">
|
<OpenAPIDisclosure context={context} label="Headers">
|
||||||
<OpenAPISchemaProperties
|
<OpenAPISchemaProperties
|
||||||
properties={headers.map(([name, header]) => {
|
properties={headers.map(([name, header]) =>
|
||||||
return parameterToProperty({ name, ...header });
|
parameterToProperty({ name, ...header })
|
||||||
})}
|
)}
|
||||||
context={context}
|
context={context}
|
||||||
/>
|
/>
|
||||||
</OpenAPIDisclosure>
|
</OpenAPIDisclosure>
|
||||||
|
|||||||
@@ -27,10 +27,7 @@ export function OpenAPIResponses(props: {
|
|||||||
return {
|
return {
|
||||||
id: statusCode,
|
id: statusCode,
|
||||||
label: (
|
label: (
|
||||||
<div
|
<div className="openapi-response-tab-content">
|
||||||
className="openapi-response-tab-content"
|
|
||||||
key={`response-${statusCode}`}
|
|
||||||
>
|
|
||||||
<span className="openapi-response-statuscode">
|
<span className="openapi-response-statuscode">
|
||||||
{statusCode}
|
{statusCode}
|
||||||
</span>
|
</span>
|
||||||
@@ -47,7 +44,6 @@ export function OpenAPIResponses(props: {
|
|||||||
label: contentType,
|
label: contentType,
|
||||||
body: (
|
body: (
|
||||||
<OpenAPIResponse
|
<OpenAPIResponse
|
||||||
key={`$response-${statusCode}-${contentType}`}
|
|
||||||
response={response}
|
response={response}
|
||||||
mediaType={mediaType}
|
mediaType={mediaType}
|
||||||
context={context}
|
context={context}
|
||||||
|
|||||||
@@ -1,7 +1,12 @@
|
|||||||
|
'use client';
|
||||||
|
// This component does not use any client feature but we don't want to
|
||||||
|
// render it server-side because it has recursion.
|
||||||
|
|
||||||
import type { OpenAPIV3 } from '@gitbook/openapi-parser';
|
import type { OpenAPIV3 } from '@gitbook/openapi-parser';
|
||||||
import { useId } from 'react';
|
import { useId } from 'react';
|
||||||
|
|
||||||
import clsx from 'clsx';
|
import clsx from 'clsx';
|
||||||
|
import { retrocycle } from 'json-decycle';
|
||||||
import { Markdown } from './Markdown';
|
import { Markdown } from './Markdown';
|
||||||
import { OpenAPIDisclosure } from './OpenAPIDisclosure';
|
import { OpenAPIDisclosure } from './OpenAPIDisclosure';
|
||||||
import { OpenAPISchemaName } from './OpenAPISchemaName';
|
import { OpenAPISchemaName } from './OpenAPISchemaName';
|
||||||
@@ -10,7 +15,7 @@ import { checkIsReference, resolveDescription, resolveFirstExample } from './uti
|
|||||||
|
|
||||||
type CircularRefsIds = Map<OpenAPIV3.SchemaObject, string>;
|
type CircularRefsIds = Map<OpenAPIV3.SchemaObject, string>;
|
||||||
|
|
||||||
interface OpenAPISchemaPropertyEntry {
|
export interface OpenAPISchemaPropertyEntry {
|
||||||
propertyName?: string | undefined;
|
propertyName?: string | undefined;
|
||||||
required?: boolean | undefined;
|
required?: boolean | undefined;
|
||||||
schema: OpenAPIV3.SchemaObject;
|
schema: OpenAPIV3.SchemaObject;
|
||||||
@@ -22,15 +27,10 @@ interface OpenAPISchemaPropertyEntry {
|
|||||||
function OpenAPISchemaProperty(props: {
|
function OpenAPISchemaProperty(props: {
|
||||||
property: OpenAPISchemaPropertyEntry;
|
property: OpenAPISchemaPropertyEntry;
|
||||||
context: OpenAPIClientContext;
|
context: OpenAPIClientContext;
|
||||||
circularRefs?: CircularRefsIds;
|
circularRefs: CircularRefsIds;
|
||||||
className?: string;
|
className?: string;
|
||||||
}) {
|
}) {
|
||||||
const {
|
const { circularRefs: parentCircularRefs, context, className, property } = props;
|
||||||
property,
|
|
||||||
circularRefs: parentCircularRefs = new Map<OpenAPIV3.SchemaObject, string>(),
|
|
||||||
context,
|
|
||||||
className,
|
|
||||||
} = props;
|
|
||||||
|
|
||||||
const { schema } = property;
|
const { schema } = property;
|
||||||
|
|
||||||
@@ -40,37 +40,43 @@ function OpenAPISchemaProperty(props: {
|
|||||||
<div id={id} className={clsx('openapi-schema', className)}>
|
<div id={id} className={clsx('openapi-schema', className)}>
|
||||||
<OpenAPISchemaPresentation property={property} />
|
<OpenAPISchemaPresentation property={property} />
|
||||||
{(() => {
|
{(() => {
|
||||||
const parentCircularRef = parentCircularRefs.get(schema);
|
const circularRefId = parentCircularRefs.get(schema);
|
||||||
|
|
||||||
// Avoid recursing infinitely, and instead render a link to the parent schema
|
// Avoid recursing infinitely, and instead render a link to the parent schema
|
||||||
if (parentCircularRef) {
|
if (circularRefId) {
|
||||||
return <OpenAPISchemaCircularRef id={parentCircularRef} schema={schema} />;
|
return <OpenAPISchemaCircularRef id={circularRefId} schema={schema} />;
|
||||||
}
|
}
|
||||||
|
|
||||||
const circularRefs = parentCircularRefs.set(schema, id);
|
const circularRefs = new Map(parentCircularRefs);
|
||||||
|
circularRefs.set(schema, id);
|
||||||
|
|
||||||
const properties = getSchemaProperties(schema);
|
const properties = getSchemaProperties(schema);
|
||||||
const alternatives = getSchemaAlternatives(schema, new Set(circularRefs.keys()));
|
if (properties) {
|
||||||
return (
|
return (
|
||||||
<>
|
<OpenAPIDisclosure context={context} label={getDisclosureLabel(schema)}>
|
||||||
{alternatives?.map((schema, index) => (
|
<OpenAPISchemaProperties
|
||||||
<OpenAPISchemaAlternative
|
properties={properties}
|
||||||
key={index}
|
|
||||||
schema={schema}
|
|
||||||
circularRefs={circularRefs}
|
circularRefs={circularRefs}
|
||||||
context={context}
|
context={context}
|
||||||
/>
|
/>
|
||||||
))}
|
</OpenAPIDisclosure>
|
||||||
{properties?.length ? (
|
);
|
||||||
<OpenAPIDisclosure context={context} label={getDisclosureLabel(schema)}>
|
}
|
||||||
<OpenAPISchemaProperties
|
|
||||||
properties={properties}
|
const ancestors = new Set(circularRefs.keys());
|
||||||
circularRefs={circularRefs}
|
const alternatives = getSchemaAlternatives(schema, ancestors);
|
||||||
context={context}
|
|
||||||
/>
|
if (alternatives) {
|
||||||
</OpenAPIDisclosure>
|
return alternatives.map((schema, index) => (
|
||||||
) : null}
|
<OpenAPISchemaAlternative
|
||||||
</>
|
key={index}
|
||||||
);
|
schema={schema}
|
||||||
|
circularRefs={circularRefs}
|
||||||
|
context={context}
|
||||||
|
/>
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
})()}
|
})()}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
@@ -79,41 +85,77 @@ function OpenAPISchemaProperty(props: {
|
|||||||
/**
|
/**
|
||||||
* Render a set of properties of an OpenAPI schema.
|
* Render a set of properties of an OpenAPI schema.
|
||||||
*/
|
*/
|
||||||
export function OpenAPISchemaProperties(props: {
|
function OpenAPISchemaProperties(props: {
|
||||||
id?: string;
|
id?: string;
|
||||||
properties: OpenAPISchemaPropertyEntry[];
|
properties: OpenAPISchemaPropertyEntry[];
|
||||||
circularRefs?: CircularRefsIds;
|
circularRefs?: CircularRefsIds;
|
||||||
context: OpenAPIClientContext;
|
context: OpenAPIClientContext;
|
||||||
}) {
|
}) {
|
||||||
const { id, properties, circularRefs, context } = props;
|
const {
|
||||||
|
id,
|
||||||
|
properties,
|
||||||
|
circularRefs = new Map<OpenAPIV3.SchemaObject, string>(),
|
||||||
|
context,
|
||||||
|
} = props;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div id={id} className="openapi-schema-properties">
|
<div id={id} className="openapi-schema-properties">
|
||||||
{properties.map((property, index) => (
|
{properties.map((property, index) => {
|
||||||
<OpenAPISchemaProperty
|
return (
|
||||||
key={index}
|
<OpenAPISchemaProperty
|
||||||
circularRefs={circularRefs}
|
key={index}
|
||||||
property={property}
|
circularRefs={circularRefs}
|
||||||
context={context}
|
property={property}
|
||||||
/>
|
context={context}
|
||||||
))}
|
/>
|
||||||
|
);
|
||||||
|
})}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function OpenAPISchemaPropertiesFromServer(props: {
|
||||||
|
id?: string;
|
||||||
|
properties: string;
|
||||||
|
context: OpenAPIClientContext;
|
||||||
|
}) {
|
||||||
|
return (
|
||||||
|
<OpenAPISchemaProperties
|
||||||
|
id={props.id}
|
||||||
|
properties={JSON.parse(props.properties, retrocycle())}
|
||||||
|
context={props.context}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Render a root schema (such as the request body or response body).
|
* Render a root schema (such as the request body or response body).
|
||||||
*/
|
*/
|
||||||
export function OpenAPIRootSchema(props: {
|
function OpenAPIRootSchema(props: {
|
||||||
schema: OpenAPIV3.SchemaObject;
|
schema: OpenAPIV3.SchemaObject;
|
||||||
context: OpenAPIClientContext;
|
context: OpenAPIClientContext;
|
||||||
|
circularRefs?: CircularRefsIds;
|
||||||
}) {
|
}) {
|
||||||
const { schema, context } = props;
|
const {
|
||||||
|
schema,
|
||||||
|
context,
|
||||||
|
circularRefs: parentCircularRefs = new Map<OpenAPIV3.SchemaObject, string>(),
|
||||||
|
} = props;
|
||||||
|
|
||||||
|
const id = useId();
|
||||||
const properties = getSchemaProperties(schema);
|
const properties = getSchemaProperties(schema);
|
||||||
|
|
||||||
if (properties?.length) {
|
if (properties?.length) {
|
||||||
return <OpenAPISchemaProperties properties={properties} context={context} />;
|
const circularRefs = new Map(parentCircularRefs);
|
||||||
|
circularRefs.set(schema, id);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<OpenAPISchemaProperties
|
||||||
|
properties={properties}
|
||||||
|
circularRefs={circularRefs}
|
||||||
|
context={context}
|
||||||
|
/>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -121,6 +163,19 @@ export function OpenAPIRootSchema(props: {
|
|||||||
className="openapi-schema-root"
|
className="openapi-schema-root"
|
||||||
property={{ schema }}
|
property={{ schema }}
|
||||||
context={context}
|
context={context}
|
||||||
|
circularRefs={parentCircularRefs}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function OpenAPIRootSchemaFromServer(props: {
|
||||||
|
schema: string;
|
||||||
|
context: OpenAPIClientContext;
|
||||||
|
}) {
|
||||||
|
return (
|
||||||
|
<OpenAPIRootSchema
|
||||||
|
schema={JSON.parse(props.schema, retrocycle())}
|
||||||
|
context={props.context}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -136,6 +191,7 @@ function OpenAPISchemaAlternative(props: {
|
|||||||
context: OpenAPIClientContext;
|
context: OpenAPIClientContext;
|
||||||
}) {
|
}) {
|
||||||
const { schema, circularRefs, context } = props;
|
const { schema, circularRefs, context } = props;
|
||||||
|
|
||||||
const description = resolveDescription(schema);
|
const description = resolveDescription(schema);
|
||||||
const properties = getSchemaProperties(schema);
|
const properties = getSchemaProperties(schema);
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
import type { OpenAPIV3 } from '@gitbook/openapi-parser';
|
||||||
|
import { decycle } from 'json-decycle';
|
||||||
|
import {
|
||||||
|
OpenAPIRootSchemaFromServer,
|
||||||
|
OpenAPISchemaPropertiesFromServer,
|
||||||
|
type OpenAPISchemaPropertyEntry,
|
||||||
|
} from './OpenAPISchema';
|
||||||
|
import type { OpenAPIClientContext } from './types';
|
||||||
|
|
||||||
|
export function OpenAPISchemaProperties(props: {
|
||||||
|
id?: string;
|
||||||
|
properties: OpenAPISchemaPropertyEntry[];
|
||||||
|
context: OpenAPIClientContext;
|
||||||
|
}) {
|
||||||
|
return (
|
||||||
|
<OpenAPISchemaPropertiesFromServer
|
||||||
|
id={props.id}
|
||||||
|
properties={JSON.stringify(props.properties, decycle())}
|
||||||
|
context={props.context}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function OpenAPIRootSchema(props: {
|
||||||
|
schema: OpenAPIV3.SchemaObject;
|
||||||
|
context: OpenAPIClientContext;
|
||||||
|
}) {
|
||||||
|
return (
|
||||||
|
<OpenAPIRootSchemaFromServer
|
||||||
|
schema={JSON.stringify(props.schema, decycle())}
|
||||||
|
context={props.context}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -2,18 +2,12 @@ import type { OpenAPI } from '@gitbook/openapi-parser';
|
|||||||
|
|
||||||
import { OpenAPIRequestBody } from './OpenAPIRequestBody';
|
import { OpenAPIRequestBody } from './OpenAPIRequestBody';
|
||||||
import { OpenAPIResponses } from './OpenAPIResponses';
|
import { OpenAPIResponses } from './OpenAPIResponses';
|
||||||
import { OpenAPISchemaProperties } from './OpenAPISchema';
|
import { OpenAPISchemaProperties } from './OpenAPISchemaServer';
|
||||||
import { OpenAPISecurities } from './OpenAPISecurities';
|
import { OpenAPISecurities } from './OpenAPISecurities';
|
||||||
import { StaticSection } from './StaticSection';
|
import { StaticSection } from './StaticSection';
|
||||||
import type { OpenAPIClientContext, OpenAPIOperationData } from './types';
|
import type { OpenAPIClientContext, OpenAPIOperationData } from './types';
|
||||||
import { parameterToProperty } from './utils';
|
import { parameterToProperty } from './utils';
|
||||||
|
|
||||||
/**
|
|
||||||
* Client component to render the spec for the request and response.
|
|
||||||
*
|
|
||||||
* We use a client component as rendering recursive JSON schema in the server is expensive
|
|
||||||
* (the entire schema is rendered at once, while the client component only renders the visible part)
|
|
||||||
*/
|
|
||||||
export function OpenAPISpec(props: { data: OpenAPIOperationData; context: OpenAPIClientContext }) {
|
export function OpenAPISpec(props: { data: OpenAPIOperationData; context: OpenAPIClientContext }) {
|
||||||
const { data, context } = props;
|
const { data, context } = props;
|
||||||
|
|
||||||
@@ -25,13 +19,13 @@ export function OpenAPISpec(props: { data: OpenAPIOperationData; context: OpenAP
|
|||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{securities.length > 0 ? (
|
{securities.length > 0 ? (
|
||||||
<OpenAPISecurities securities={securities} context={context} />
|
<OpenAPISecurities key="securities" securities={securities} context={context} />
|
||||||
) : null}
|
) : null}
|
||||||
|
|
||||||
{parameterGroups.map((group) => {
|
{parameterGroups.map((group) => {
|
||||||
return (
|
return (
|
||||||
<StaticSection
|
<StaticSection
|
||||||
key={group.key}
|
key={`parameter-${group.key}`}
|
||||||
className="openapi-parameters"
|
className="openapi-parameters"
|
||||||
header={group.label}
|
header={group.label}
|
||||||
>
|
>
|
||||||
@@ -44,10 +38,18 @@ export function OpenAPISpec(props: { data: OpenAPIOperationData; context: OpenAP
|
|||||||
})}
|
})}
|
||||||
|
|
||||||
{operation.requestBody ? (
|
{operation.requestBody ? (
|
||||||
<OpenAPIRequestBody requestBody={operation.requestBody} context={context} />
|
<OpenAPIRequestBody
|
||||||
|
key="body"
|
||||||
|
requestBody={operation.requestBody}
|
||||||
|
context={context}
|
||||||
|
/>
|
||||||
) : null}
|
) : null}
|
||||||
{operation.responses ? (
|
{operation.responses ? (
|
||||||
<OpenAPIResponses responses={operation.responses} context={context} />
|
<OpenAPIResponses
|
||||||
|
key="responses"
|
||||||
|
responses={operation.responses}
|
||||||
|
context={context}
|
||||||
|
/>
|
||||||
) : null}
|
) : null}
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -137,21 +137,11 @@ export function OpenAPITabsPanels() {
|
|||||||
const key = selectedTab.key.toString();
|
const key = selectedTab.key.toString();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<TabPanel key={key} id={key} className="openapi-tabs-panel">
|
<TabPanel id={key} className="openapi-tabs-panel">
|
||||||
{selectedTab.body}
|
<div className="openapi-tabs-body">{selectedTab.body}</div>
|
||||||
{selectedTab.footer ? (
|
{selectedTab.footer ? (
|
||||||
<OpenAPITabsPanelFooter>{selectedTab.footer}</OpenAPITabsPanelFooter>
|
<div className="openapi-tabs-footer">{selectedTab.footer}</div>
|
||||||
) : null}
|
) : null}
|
||||||
</TabPanel>
|
</TabPanel>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* The OpenAPI Tabs panel footer component.
|
|
||||||
* This component should be used as a child of the OpenAPITabs component.
|
|
||||||
*/
|
|
||||||
function OpenAPITabsPanelFooter(props: { children: React.ReactNode }) {
|
|
||||||
const { children } = props;
|
|
||||||
|
|
||||||
return <div className="openapi-tabs-footer">{children}</div>;
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import clsx from 'clsx';
|
import clsx from 'clsx';
|
||||||
import { OpenAPIDisclosureGroup } from '../OpenAPIDisclosureGroup';
|
import { OpenAPIDisclosureGroup } from '../OpenAPIDisclosureGroup';
|
||||||
import { OpenAPIRootSchema } from '../OpenAPISchema';
|
import { OpenAPIRootSchema } from '../OpenAPISchemaServer';
|
||||||
import { Section, SectionBody } from '../StaticSection';
|
import { Section, SectionBody } from '../StaticSection';
|
||||||
import type { OpenAPIClientContext, OpenAPIContextProps, OpenAPISchemasData } from '../types';
|
import type { OpenAPIClientContext, OpenAPIContextProps, OpenAPISchemasData } from '../types';
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user