@@ -49,20 +49,13 @@ export function OpenAPISchemaProperty(
49
49
50
50
if ( alternatives ?. [ 0 ] ?. length ) {
51
51
return (
52
- < InteractiveSection id = { id } className = { clsx ( 'openapi-schema' , className ) } >
53
- < OpenAPISchemaPresentation { ...props } />
54
- { alternatives [ 0 ] . map ( ( alternative , index ) => (
55
- < OpenAPISchemaAlternative
56
- key = { `alternative-${ index } ` }
57
- schema = { alternative }
58
- circularRefs = { circularRefs }
59
- context = { context }
60
- />
61
- ) ) }
62
- { parentCircularRef ? (
63
- < OpenAPISchemaCircularRef id = { parentCircularRef } schema = { schema } />
64
- ) : null }
65
- </ InteractiveSection >
52
+ < OpenAPISchemaAlternativesItem
53
+ { ...props }
54
+ circularRefs = { circularRefs }
55
+ context = { context }
56
+ alternatives = { alternatives }
57
+ parentCircularRef = { parentCircularRef }
58
+ />
66
59
) ;
67
60
}
68
61
@@ -173,6 +166,25 @@ function OpenAPISchemaAlternative(props: {
173
166
const id = useId ( ) ;
174
167
const subProperties = getSchemaProperties ( schema ) ;
175
168
const description = resolveDescription ( schema ) ;
169
+ const alternatives = getSchemaAlternatives ( schema , new Set ( circularRefs ?. keys ( ) ) ) ;
170
+
171
+ if ( alternatives ?. [ 0 ] ?. length && ! subProperties ?. length ) {
172
+ return (
173
+ < >
174
+ { description ? (
175
+ < Markdown source = { description } className = "openapi-schema-description" />
176
+ ) : null }
177
+ < OpenAPIDisclosure context = { context } label = { getDisclosureLabel ( schema ) } >
178
+ < OpenAPISchemaAlternativesItem
179
+ schema = { schema }
180
+ circularRefs = { circularRefs }
181
+ context = { context }
182
+ alternatives = { alternatives }
183
+ />
184
+ </ OpenAPIDisclosure >
185
+ </ >
186
+ ) ;
187
+ }
176
188
177
189
return (
178
190
< >
@@ -193,6 +205,35 @@ function OpenAPISchemaAlternative(props: {
193
205
) ;
194
206
}
195
207
208
+ function OpenAPISchemaAlternativesItem (
209
+ props : OpenAPISchemaPropertyEntry & {
210
+ circularRefs ?: CircularRefsIds ;
211
+ context : OpenAPIClientContext ;
212
+ alternatives : OpenAPISchemaAlternatives ;
213
+ parentCircularRef ?: string ;
214
+ }
215
+ ) {
216
+ const id = useId ( ) ;
217
+ const { schema, circularRefs, context, alternatives, parentCircularRef } = props ;
218
+
219
+ return (
220
+ < InteractiveSection id = { id } className = { clsx ( 'openapi-schema' ) } >
221
+ < OpenAPISchemaPresentation { ...props } />
222
+ { alternatives [ 0 ] . map ( ( alternative , index ) => (
223
+ < OpenAPISchemaAlternative
224
+ key = { `alternative-${ index } ` }
225
+ schema = { alternative }
226
+ circularRefs = { circularRefs }
227
+ context = { context }
228
+ />
229
+ ) ) }
230
+ { parentCircularRef ? (
231
+ < OpenAPISchemaCircularRef id = { parentCircularRef } schema = { schema } />
232
+ ) : null }
233
+ </ InteractiveSection >
234
+ ) ;
235
+ }
236
+
196
237
/**
197
238
* Render a circular reference to a schema.
198
239
*/
@@ -336,13 +377,18 @@ function getSchemaProperties(schema: OpenAPIV3.SchemaObject): null | OpenAPISche
336
377
return null ;
337
378
}
338
379
380
+ type OpenAPISchemaAlternatives = [
381
+ OpenAPIV3 . SchemaObject [ ] ,
382
+ OpenAPIV3 . DiscriminatorObject | undefined ,
383
+ ] ;
384
+
339
385
/**
340
386
* Get the alternatives to display for a schema.
341
387
*/
342
388
export function getSchemaAlternatives (
343
389
schema : OpenAPIV3 . SchemaObject ,
344
390
ancestors : Set < OpenAPIV3 . SchemaObject > = new Set ( )
345
- ) : null | [ OpenAPIV3 . SchemaObject [ ] , OpenAPIV3 . DiscriminatorObject | undefined ] {
391
+ ) : null | OpenAPISchemaAlternatives {
346
392
const downAncestors = new Set ( ancestors ) . add ( schema ) ;
347
393
348
394
if ( schema . anyOf ) {
@@ -408,7 +454,9 @@ export function getSchemaTitle(
408
454
if ( schema . format ) {
409
455
type += ` · ${ schema . format } ` ;
410
456
}
411
- } else if ( 'anyOf' in schema ) {
457
+ }
458
+
459
+ if ( 'anyOf' in schema ) {
412
460
type = 'any of' ;
413
461
} else if ( 'oneOf' in schema ) {
414
462
type = 'one of' ;
0 commit comments