Skip to content

Commit fe2967e

Browse files
Merge pull request #536 from bitgopatmcl/use-openapi-v3
feat: output OpenAPI v3 instead of v3.1
2 parents 41b8617 + 0566561 commit fe2967e

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

packages/openapi-generator/src/openapi.ts

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { OpenAPIV3_1 } from 'openapi-types';
1+
import { OpenAPIV3 } from 'openapi-types';
22

33
import { STATUS_CODES } from 'http';
44
import { parseCommentBlock } from './jsdoc';
@@ -8,12 +8,24 @@ import type { Schema } from './ir';
88

99
function 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

138150
export 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: {

packages/openapi-generator/test/openapi.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export const internalRoute = h.httpRoute({
107107
`;
108108

109109
testCase('simple route', SIMPLE, {
110-
openapi: '3.1.0',
110+
openapi: '3.0.0',
111111
info: {
112112
title: 'Test',
113113
version: '1.0.0',
@@ -205,7 +205,7 @@ export const route = h.httpRoute({
205205
`;
206206

207207
testCase('request body route', REQUEST_BODY, {
208-
openapi: '3.1.0',
208+
openapi: '3.0.0',
209209
info: {
210210
title: 'Test',
211211
version: '1.0.0',
@@ -276,7 +276,7 @@ export const route = h.httpRoute({
276276
`;
277277

278278
testCase('request union route', UNION, {
279-
openapi: '3.1.0',
279+
openapi: '3.0.0',
280280
info: {
281281
title: 'Test',
282282
version: '1.0.0',

0 commit comments

Comments
 (0)