@@ -11,6 +11,27 @@ import { createFullWidthTable } from "./createFullWidthTable";
11
11
import { getSchemaName } from "./schema" ;
12
12
import { create , guard } from "./utils" ;
13
13
14
+ function resolveAllOf ( allOf : SchemaObject [ ] ) {
15
+ // TODO: naive implementation (only supports objects, no directly nested allOf)
16
+ const properties = allOf . reduce ( ( acc , cur ) => {
17
+ if ( cur . properties !== undefined ) {
18
+ const next = { ...acc , ...cur . properties } ;
19
+ return next ;
20
+ }
21
+ return acc ;
22
+ } , { } ) ;
23
+
24
+ const required = allOf . reduce ( ( acc , cur ) => {
25
+ if ( Array . isArray ( cur . required ) ) {
26
+ const next = [ ...acc , ...cur . required ] ;
27
+ return next ;
28
+ }
29
+ return acc ;
30
+ } , [ ] as string [ ] ) ;
31
+
32
+ return { properties, required } ;
33
+ }
34
+
14
35
interface RowProps {
15
36
name : string ;
16
37
schema : SchemaObject ;
@@ -85,6 +106,26 @@ function createRows({ schema }: RowsProps): string | undefined {
85
106
} ) ;
86
107
}
87
108
109
+ // TODO: This can be a bit complicated types can be missmatched and there can be nested allOfs which need to be resolved before merging properties
110
+ if ( schema . allOf !== undefined ) {
111
+ const { properties, required } = resolveAllOf ( schema . allOf ) ;
112
+ return createFullWidthTable ( {
113
+ style : {
114
+ marginTop : "var(--ifm-table-cell-padding)" ,
115
+ marginBottom : "0px" ,
116
+ } ,
117
+ children : create ( "tbody" , {
118
+ children : Object . entries ( properties ) . map ( ( [ key , val ] ) =>
119
+ createRow ( {
120
+ name : key ,
121
+ schema : val ,
122
+ required : Array . isArray ( required ) ? required . includes ( key ) : false ,
123
+ } )
124
+ ) ,
125
+ } ) ,
126
+ } ) ;
127
+ }
128
+
88
129
// array
89
130
if ( schema . items !== undefined ) {
90
131
return createRows ( { schema : schema . items } ) ;
@@ -112,9 +153,31 @@ function createRowsRoot({ schema }: RowsRootProps) {
112
153
) ;
113
154
}
114
155
156
+ // TODO: This can be a bit complicated types can be missmatched and there can be nested allOfs which need to be resolved before merging properties
157
+ if ( schema . allOf !== undefined ) {
158
+ const { properties, required } = resolveAllOf ( schema . allOf ) ;
159
+ return Object . entries ( properties ) . map ( ( [ key , val ] ) =>
160
+ createRow ( {
161
+ name : key ,
162
+ schema : val ,
163
+ required : Array . isArray ( required ) ? required . includes ( key ) : false ,
164
+ } )
165
+ ) ;
166
+ }
167
+
115
168
// array
116
169
if ( schema . items !== undefined ) {
117
- return createRows ( { schema : schema . items } ) ;
170
+ return create ( "tr" , {
171
+ children : create ( "td" , {
172
+ children : [
173
+ create ( "span" , {
174
+ style : { opacity : "0.6" } ,
175
+ children : ` ${ getSchemaName ( schema , true ) } ` ,
176
+ } ) ,
177
+ createRows ( { schema : schema . items } ) ,
178
+ ] ,
179
+ } ) ,
180
+ } ) ;
118
181
}
119
182
120
183
// primitive
@@ -148,7 +211,7 @@ interface Props {
148
211
style ?: any ;
149
212
title : string ;
150
213
body : {
151
- content : {
214
+ content ? : {
152
215
[ key : string ] : MediaTypeObject ;
153
216
} ;
154
217
description ?: string ;
@@ -161,9 +224,9 @@ export function createSchemaTable({ title, body, ...rest }: Props) {
161
224
return undefined ;
162
225
}
163
226
227
+ // TODO:
164
228
// NOTE: We just pick a random content-type.
165
229
// How common is it to have multiple?
166
-
167
230
const randomFirstKey = Object . keys ( body . content ) [ 0 ] ;
168
231
169
232
const firstBody = body . content [ randomFirstKey ] . schema ;
@@ -173,8 +236,10 @@ export function createSchemaTable({ title, body, ...rest }: Props) {
173
236
}
174
237
175
238
// we don't show the table if there is no properties to show
176
- if ( Object . keys ( firstBody . properties ?? { } ) . length === 0 ) {
177
- return undefined ;
239
+ if ( firstBody . properties !== undefined ) {
240
+ if ( Object . keys ( firstBody . properties ) . length === 0 ) {
241
+ return undefined ;
242
+ }
178
243
}
179
244
180
245
return createFullWidthTable ( {
0 commit comments