1- import { OpenAPIV3_1 } from 'openapi-types' ;
1+ import { OpenAPIV3 } from 'openapi-types' ;
22
33import { STATUS_CODES } from 'http' ;
44import { parseCommentBlock } from './jsdoc' ;
@@ -8,12 +8,24 @@ import type { Schema } from './ir';
88
99function schemaToOpenAPI (
1010 schema : Schema ,
11- ) : OpenAPIV3_1 . SchemaObject | OpenAPIV3_1 . ReferenceObject | undefined {
11+ ) : OpenAPIV3 . SchemaObject | OpenAPIV3 . ReferenceObject | undefined {
1212 switch ( schema . type ) {
1313 case 'primitive' :
14- return { type : schema . value } ;
14+ if ( schema . value === 'integer' ) {
15+ return { type : 'number' } ;
16+ } else if ( schema . value === 'null' ) {
17+ // TODO: OpenAPI v3 does not have an explicit null type, is there a better way to represent this?
18+ // Or should we just conflate explicit null and undefined properties?
19+ return { nullable : true , enum : [ ] } ;
20+ } else {
21+ return { type : schema . value } ;
22+ }
1523 case 'literal' :
16- return { type : schema . kind , enum : [ schema . value ] } ;
24+ if ( schema . kind === 'null' ) {
25+ return { nullable : true , enum : [ ] } ;
26+ } else {
27+ return { type : schema . kind , enum : [ schema . value ] } ;
28+ }
1729 case 'ref' :
1830 return { $ref : `#/components/schemas/${ schema . name } ` } ;
1931 case 'array' :
@@ -34,7 +46,7 @@ function schemaToOpenAPI(
3446
3547 return { ...acc , [ name ] : innerSchema } ;
3648 } ,
37- { } as Record < string , OpenAPIV3_1 . SchemaObject | OpenAPIV3_1 . ReferenceObject > ,
49+ { } as Record < string , OpenAPIV3 . SchemaObject | OpenAPIV3 . ReferenceObject > ,
3850 ) ,
3951 required : schema . required ,
4052 } ;
@@ -76,7 +88,7 @@ function schemaToOpenAPI(
7688 }
7789}
7890
79- function routeToOpenAPI ( route : Route ) : [ string , string , OpenAPIV3_1 . OperationObject ] {
91+ function routeToOpenAPI ( route : Route ) : [ string , string , OpenAPIV3 . OperationObject ] {
8092 const jsdoc = route . comment !== undefined ? parseCommentBlock ( route . comment ) : { } ;
8193 const operationId = jsdoc . tags ?. operationId ;
8294 const tag = jsdoc . tags ?. tag ?? '' ;
@@ -136,18 +148,18 @@ function routeToOpenAPI(route: Route): [string, string, OpenAPIV3_1.OperationObj
136148}
137149
138150export function convertRoutesToOpenAPI (
139- info : OpenAPIV3_1 . InfoObject ,
151+ info : OpenAPIV3 . InfoObject ,
140152 routes : Route [ ] ,
141153 schemas : Components ,
142- ) : OpenAPIV3_1 . Document {
154+ ) : OpenAPIV3 . Document {
143155 const paths = routes . reduce (
144156 ( acc , route ) => {
145157 const [ path , method , pathItem ] = routeToOpenAPI ( route ) ;
146158 let pathObject = acc [ path ] ?? { } ;
147159 pathObject [ method ] = pathItem ;
148160 return { ...acc , [ path ] : pathObject } ;
149161 } ,
150- { } as Record < string , Record < string , OpenAPIV3_1 . PathItemObject > > ,
162+ { } as Record < string , Record < string , OpenAPIV3 . PathItemObject > > ,
151163 ) ;
152164
153165 const openapiSchemas = Object . entries ( schemas ) . reduce (
@@ -159,11 +171,11 @@ export function convertRoutesToOpenAPI(
159171 return { ...acc , [ name ] : openapiSchema } ;
160172 }
161173 } ,
162- { } as Record < string , OpenAPIV3_1 . SchemaObject | OpenAPIV3_1 . ReferenceObject > ,
174+ { } as Record < string , OpenAPIV3 . SchemaObject | OpenAPIV3 . ReferenceObject > ,
163175 ) ;
164176
165177 return {
166- openapi : '3.1 .0' ,
178+ openapi : '3.0 .0' ,
167179 info,
168180 paths,
169181 components : {
0 commit comments