1
1
import clsx from 'clsx' ;
2
2
3
+ import type { OpenAPIV3 } from '@gitbook/openapi-parser' ;
3
4
import { OpenAPIDisclosureGroup } from '../OpenAPIDisclosureGroup' ;
4
5
import { OpenAPIRootSchema } from '../OpenAPISchema' ;
5
6
import { Section , SectionBody } from '../StaticSection' ;
@@ -22,29 +23,64 @@ export function OpenAPIModels(props: {
22
23
blockKey : context . blockKey ,
23
24
} ;
24
25
26
+ if ( ! models . length ) {
27
+ return null ;
28
+ }
29
+
25
30
return (
26
31
< div className = { clsx ( 'openapi-models' , className ) } >
27
- { models . length ? (
28
- < OpenAPIDisclosureGroup
29
- allowsMultipleExpanded
30
- icon = { context . icons . chevronRight }
31
- groups = { models . map ( ( [ name , schema ] ) => ( {
32
- id : name ,
33
- label : (
34
- < div className = "openapi-response-tab-content" key = { `model-${ name } ` } >
35
- < span className = "openapi-response-statuscode" > { name } </ span >
36
- </ div >
37
- ) ,
38
- body : (
39
- < Section className = "openapi-section-models" >
40
- < SectionBody >
41
- < OpenAPIRootSchema schema = { schema } context = { clientContext } />
42
- </ SectionBody >
43
- </ Section >
44
- ) ,
45
- } ) ) }
46
- />
47
- ) : null }
32
+ < OpenAPIRootModelsSchema models = { models } context = { clientContext } />
48
33
</ div >
49
34
) ;
50
35
}
36
+
37
+ /**
38
+ * Root schema for OpenAPI models.
39
+ * It displays a single model or a disclosure group for multiple models.
40
+ */
41
+ function OpenAPIRootModelsSchema ( props : {
42
+ models : [ string , OpenAPIV3 . SchemaObject ] [ ] ;
43
+ context : OpenAPIClientContext ;
44
+ } ) {
45
+ const { models, context } = props ;
46
+
47
+ // If there is only one model, we show it directly.
48
+ if ( models . length === 1 ) {
49
+ const schema = models ?. [ 0 ] ?. [ 1 ] ;
50
+
51
+ if ( ! schema ) {
52
+ return null ;
53
+ }
54
+
55
+ return (
56
+ < Section >
57
+ < SectionBody >
58
+ < OpenAPIRootSchema schema = { schema } context = { context } />
59
+ </ SectionBody >
60
+ </ Section >
61
+ ) ;
62
+ }
63
+
64
+ // If there are multiple models, we use a disclosure group to show them all.
65
+ return (
66
+ < OpenAPIDisclosureGroup
67
+ allowsMultipleExpanded
68
+ icon = { context . icons . chevronRight }
69
+ groups = { models . map ( ( [ name , schema ] ) => ( {
70
+ id : name ,
71
+ label : (
72
+ < div className = "openapi-response-tab-content" key = { `model-${ name } ` } >
73
+ < span className = "openapi-response-statuscode" > { name } </ span >
74
+ </ div >
75
+ ) ,
76
+ body : (
77
+ < Section className = "openapi-section-models" >
78
+ < SectionBody >
79
+ < OpenAPIRootSchema schema = { schema } context = { context } />
80
+ </ SectionBody >
81
+ </ Section >
82
+ ) ,
83
+ } ) ) }
84
+ />
85
+ ) ;
86
+ }
0 commit comments