Skip to content

Commit 259074b

Browse files
authored
Fix OpenAPI spec recursion (#2920)
1 parent 9a2f5b9 commit 259074b

File tree

7 files changed

+233
-328
lines changed

7 files changed

+233
-328
lines changed

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111
"prettier.enable": false,
1212
"editor.defaultFormatter": "biomejs.biome",
1313
"editor.codeActionsOnSave": {
14-
"source.organizeImports": "explicit"
14+
"source.organizeImports.biome": "explicit"
1515
}
1616
}

packages/gitbook/src/components/DocumentView/Blocks.tsx

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -52,33 +52,29 @@ export function UnwrappedBlocks<TBlock extends DocumentBlock>(props: UnwrappedBl
5252

5353
let isOffscreen = false;
5454

55-
return (
56-
<>
57-
{nodes.map((node) => {
58-
isOffscreen =
59-
isOffscreen ||
60-
isBlockOffscreen({
61-
document: props.document,
62-
block: node,
63-
ancestorBlocks: props.ancestorBlocks,
64-
});
55+
return nodes.map((node) => {
56+
isOffscreen =
57+
isOffscreen ||
58+
isBlockOffscreen({
59+
document: props.document,
60+
block: node,
61+
ancestorBlocks: props.ancestorBlocks,
62+
});
6563

66-
return (
67-
<Block
68-
key={node.key}
69-
block={node}
70-
style={[
71-
'mx-auto w-full decoration-primary/6',
72-
node.data && 'fullWidth' in node.data && node.data.fullWidth
73-
? 'max-w-screen-xl'
74-
: 'max-w-3xl',
75-
blockStyle,
76-
]}
77-
isEstimatedOffscreen={isOffscreen}
78-
{...contextProps}
79-
/>
80-
);
81-
})}
82-
</>
83-
);
64+
return (
65+
<Block
66+
key={node.key}
67+
block={node}
68+
style={[
69+
'mx-auto w-full decoration-primary/6',
70+
node.data && 'fullWidth' in node.data && node.data.fullWidth
71+
? 'max-w-screen-xl'
72+
: 'max-w-3xl',
73+
blockStyle,
74+
]}
75+
isEstimatedOffscreen={isOffscreen}
76+
{...contextProps}
77+
/>
78+
);
79+
});
8480
}

packages/gitbook/src/components/DocumentView/Inlines.tsx

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,19 @@ export function Inlines<T extends DocumentInline | DocumentText>(
2424
) {
2525
const { nodes, document, ancestorInlines, ...contextProps } = props;
2626

27-
return (
28-
<>
29-
{nodes.map((node) => {
30-
if (node.object === 'text') {
31-
return <Text key={node.key} text={node} />;
32-
}
27+
return nodes.map((node) => {
28+
if (node.object === 'text') {
29+
return <Text key={node.key} text={node} />;
30+
}
3331

34-
return (
35-
<Inline
36-
key={node.key}
37-
inline={node}
38-
document={document}
39-
ancestorInlines={ancestorInlines}
40-
{...contextProps}
41-
/>
42-
);
43-
})}
44-
</>
45-
);
32+
return (
33+
<Inline
34+
key={node.key}
35+
inline={node}
36+
document={document}
37+
ancestorInlines={ancestorInlines}
38+
{...contextProps}
39+
/>
40+
);
41+
});
4642
}

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -134,26 +134,23 @@
134134
@apply flex flex-col;
135135
}
136136

137-
.openapi-schema-body {
137+
.openapi-schema {
138138
@apply py-2.5 flex flex-col gap-2;
139139
}
140140

141141
.openapi-section-body .openapi-schema-properties {
142142
@apply divide-y divide-tint-subtle;
143143
}
144144

145-
.openapi-disclosure-group-panel
146-
> .openapi-schema-properties
147-
> *:first-child
148-
> .openapi-schema-body {
145+
.openapi-disclosure-group-panel > .openapi-schema-properties > *:first-child > .openapi-schema {
149146
@apply pt-0;
150147
}
151148

152-
.openapi-responsebody > .openapi-schema-properties > *:last-child > .openapi-schema-body {
149+
.openapi-responsebody > .openapi-schema-properties > .openapi-schema:last-child {
153150
@apply pb-0;
154151
}
155152

156-
.openapi-responsebody > .openapi-schema-properties > *:only-child > .openapi-schema-body {
153+
.openapi-responsebody > .openapi-schema-properties > .openapi-schema:only-child {
157154
@apply py-0;
158155
}
159156

@@ -602,7 +599,7 @@
602599
@apply border-b border-x border-tint-subtle rounded-b-lg;
603600
}
604601

605-
.openapi-disclosure-panel .openapi-schema-body {
602+
.openapi-disclosure-panel .openapi-schema {
606603
@apply p-2.5;
607604
}
608605

@@ -615,6 +612,6 @@
615612
@apply pt-0 mt-0;
616613
}
617614

618-
.openapi-section-body.openapi-schema.openapi-schema-root-body {
615+
.openapi-section-body.openapi-schema.openapi-schema-root {
619616
@apply space-y-2.5;
620617
}
Lines changed: 34 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,44 @@
11
'use client';
2-
import type React from 'react';
3-
import { useRef } from 'react';
4-
import { mergeProps, useButton, useDisclosure, useFocusRing } from 'react-aria';
5-
import { useDisclosureState } from 'react-stately';
2+
import { useState } from 'react';
3+
import { Button, Disclosure, DisclosurePanel, Heading } from 'react-aria-components';
64
import type { OpenAPIClientContext } from './types';
75

8-
interface Props {
9-
context: OpenAPIClientContext;
10-
children: React.ReactNode;
11-
label?: string;
12-
}
13-
146
/**
157
* Display an interactive OpenAPI disclosure.
16-
* The label is optional and defaults to "child attributes".
178
*/
18-
export function OpenAPIDisclosure({ context, children, label }: Props): React.JSX.Element {
19-
const state = useDisclosureState({});
20-
const panelRef = useRef<HTMLDivElement | null>(null);
21-
const triggerRef = useRef<HTMLButtonElement | null>(null);
22-
const { buttonProps: triggerProps, panelProps } = useDisclosure({}, state, panelRef);
23-
const { buttonProps } = useButton(triggerProps, triggerRef);
24-
const { isFocusVisible, focusProps } = useFocusRing();
9+
export function OpenAPIDisclosure(props: {
10+
context: OpenAPIClientContext;
11+
children: React.ReactNode;
12+
label: string;
13+
}): React.JSX.Element {
14+
const { context, children, label } = props;
15+
const [isExpanded, setIsExpanded] = useState(false);
2516

2617
return (
27-
<div className="openapi-disclosure">
28-
<button
29-
ref={triggerRef}
30-
{...mergeProps(buttonProps, focusProps)}
31-
slot="trigger"
32-
className="openapi-disclosure-trigger"
33-
style={{
34-
outline: isFocusVisible
35-
? '2px solid rgb(var(--primary-color-500) / 0.4)'
36-
: 'none',
37-
}}
38-
>
39-
{context.icons.plus}
40-
<span>
41-
{`${state.isExpanded ? 'Hide' : 'Show'} ${label ? label : 'child attributes'}`}
42-
</span>
43-
</button>
44-
45-
{state.isExpanded && (
46-
<div ref={panelRef} {...panelProps} className="openapi-disclosure-panel">
47-
{children}
48-
</div>
49-
)}
50-
</div>
18+
<Disclosure
19+
className="openapi-disclosure"
20+
isExpanded={isExpanded}
21+
onExpandedChange={setIsExpanded}
22+
>
23+
<Heading>
24+
<Button
25+
slot="trigger"
26+
className="openapi-disclosure-trigger"
27+
style={({ isFocusVisible }) => ({
28+
outline: isFocusVisible
29+
? '2px solid rgb(var(--primary-color-500) / 0.4)'
30+
: 'none',
31+
})}
32+
>
33+
{context.icons.plus}
34+
<span>
35+
{isExpanded ? 'Hide' : 'Show'} {label}
36+
</span>
37+
</Button>
38+
</Heading>
39+
<DisclosurePanel className="openapi-disclosure-panel">
40+
{isExpanded ? children : null}
41+
</DisclosurePanel>
42+
</Disclosure>
5143
);
5244
}

packages/react-openapi/src/OpenAPISchema.test.ts

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,15 @@ describe('getSchemaAlternatives', () => {
2323
],
2424
})
2525
).toEqual([
26-
[
27-
{
28-
type: 'number',
29-
},
30-
{
31-
type: 'boolean',
32-
},
33-
{
34-
type: 'string',
35-
},
36-
],
37-
undefined,
26+
{
27+
type: 'number',
28+
},
29+
{
30+
type: 'boolean',
31+
},
32+
{
33+
type: 'string',
34+
},
3835
]);
3936
});
4037

@@ -58,22 +55,19 @@ describe('getSchemaAlternatives', () => {
5855
],
5956
})
6057
).toEqual([
61-
[
62-
{
63-
allOf: [
64-
{
65-
type: 'number',
66-
},
67-
{
68-
type: 'boolean',
69-
},
70-
],
71-
},
72-
{
73-
type: 'string',
74-
},
75-
],
76-
undefined,
58+
{
59+
allOf: [
60+
{
61+
type: 'number',
62+
},
63+
{
64+
type: 'boolean',
65+
},
66+
],
67+
},
68+
{
69+
type: 'string',
70+
},
7771
]);
7872
});
7973

@@ -89,13 +83,10 @@ describe('getSchemaAlternatives', () => {
8983
a.anyOf?.push(a);
9084

9185
expect(getSchemaAlternatives(a)).toEqual([
92-
[
93-
{
94-
type: 'string',
95-
},
96-
a,
97-
],
98-
undefined,
86+
{
87+
type: 'string',
88+
},
89+
a,
9990
]);
10091
});
10192
});

0 commit comments

Comments
 (0)