@@ -14,13 +14,27 @@ function schemaToOpenAPI(
1414 const createOpenAPIObject = (
1515 schema : Schema ,
1616 ) : OpenAPIV3 . SchemaObject | OpenAPIV3 . ReferenceObject | undefined => {
17+ const description = schema . comment ?. description ;
18+
19+ const defaultObject = {
20+ ...( description ? { description } : { } ) ,
21+ } ;
22+
1723 switch ( schema . type ) {
1824 case 'boolean' :
1925 case 'string' :
2026 case 'number' :
21- return { type : schema . type , ...( schema . enum ? { enum : schema . enum } : { } ) } ;
27+ return {
28+ type : schema . type ,
29+ ...( schema . enum ? { enum : schema . enum } : { } ) ,
30+ ...defaultObject ,
31+ } ;
2232 case 'integer' :
23- return { type : 'number' , ...( schema . enum ? { enum : schema . enum } : { } ) } ;
33+ return {
34+ type : 'number' ,
35+ ...( schema . enum ? { enum : schema . enum } : { } ) ,
36+ ...defaultObject ,
37+ } ;
2438 case 'null' :
2539 // TODO: OpenAPI v3 does not have an explicit null type, is there a better way to represent this?
2640 // Or should we just conflate explicit null and undefined properties?
@@ -32,7 +46,7 @@ function schemaToOpenAPI(
3246 if ( innerSchema === undefined ) {
3347 return undefined ;
3448 }
35- return { type : 'array' , items : innerSchema } ;
49+ return { type : 'array' , items : innerSchema , ... defaultObject } ;
3650 case 'object' :
3751 return {
3852 type : 'object' ,
@@ -146,13 +160,25 @@ function routeToOpenAPI(route: Route): [string, string, OpenAPIV3.OperationObjec
146160 { } ,
147161 ) ;
148162
163+ const stripTopLevelComment = ( schema : Schema ) => {
164+ const copy = { ...schema } ;
165+
166+ if ( copy . comment ?. description !== undefined && copy . comment ?. description !== '' ) {
167+ copy . comment . description = '' ;
168+ }
169+
170+ return copy ;
171+ } ;
172+
173+ const topLevelStripped = stripTopLevelComment ( route . body ! ) ;
174+
149175 const requestBody =
150176 route . body === undefined
151177 ? { }
152178 : {
153179 requestBody : {
154180 content : {
155- 'application/json' : { schema : schemaToOpenAPI ( route . body ) } ,
181+ 'application/json' : { schema : schemaToOpenAPI ( topLevelStripped ) } ,
156182 } ,
157183 } ,
158184 } ;
@@ -195,7 +221,7 @@ function routeToOpenAPI(route: Route): [string, string, OpenAPIV3.OperationObjec
195221 description,
196222 content : {
197223 'application/json' : {
198- schema : schemaToOpenAPI ( response ) ,
224+ schema : schemaToOpenAPI ( stripTopLevelComment ( response ) ) ,
199225 ...( example !== undefined ? { example } : undefined ) ,
200226 } ,
201227 } ,
0 commit comments