Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 23 additions & 11 deletions packages/openapi-generator/src/openapi.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OpenAPIV3_1 } from 'openapi-types';
import { OpenAPIV3 } from 'openapi-types';

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

function schemaToOpenAPI(
schema: Schema,
): OpenAPIV3_1.SchemaObject | OpenAPIV3_1.ReferenceObject | undefined {
): OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject | undefined {
switch (schema.type) {
case 'primitive':
return { type: schema.value };
if (schema.value === 'integer') {
return { type: 'number' };
} else if (schema.value === 'null') {
// TODO: OpenAPI v3 does not have an explicit null type, is there a better way to represent this?
// Or should we just conflate explicit null and undefined properties?
return { nullable: true, enum: [] };
} else {
return { type: schema.value };
}
case 'literal':
return { type: schema.kind, enum: [schema.value] };
if (schema.kind === 'null') {
return { nullable: true, enum: [] };
} else {
return { type: schema.kind, enum: [schema.value] };
}
case 'ref':
return { $ref: `#/components/schemas/${schema.name}` };
case 'array':
Expand All @@ -34,7 +46,7 @@ function schemaToOpenAPI(

return { ...acc, [name]: innerSchema };
},
{} as Record<string, OpenAPIV3_1.SchemaObject | OpenAPIV3_1.ReferenceObject>,
{} as Record<string, OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject>,
),
required: schema.required,
};
Expand Down Expand Up @@ -76,7 +88,7 @@ function schemaToOpenAPI(
}
}

function routeToOpenAPI(route: Route): [string, string, OpenAPIV3_1.OperationObject] {
function routeToOpenAPI(route: Route): [string, string, OpenAPIV3.OperationObject] {
const jsdoc = route.comment !== undefined ? parseCommentBlock(route.comment) : {};
const operationId = jsdoc.tags?.operationId;
const tag = jsdoc.tags?.tag ?? '';
Expand Down Expand Up @@ -136,18 +148,18 @@ function routeToOpenAPI(route: Route): [string, string, OpenAPIV3_1.OperationObj
}

export function convertRoutesToOpenAPI(
info: OpenAPIV3_1.InfoObject,
info: OpenAPIV3.InfoObject,
routes: Route[],
schemas: Components,
): OpenAPIV3_1.Document {
): OpenAPIV3.Document {
const paths = routes.reduce(
(acc, route) => {
const [path, method, pathItem] = routeToOpenAPI(route);
let pathObject = acc[path] ?? {};
pathObject[method] = pathItem;
return { ...acc, [path]: pathObject };
},
{} as Record<string, Record<string, OpenAPIV3_1.PathItemObject>>,
{} as Record<string, Record<string, OpenAPIV3.PathItemObject>>,
);

const openapiSchemas = Object.entries(schemas).reduce(
Expand All @@ -159,11 +171,11 @@ export function convertRoutesToOpenAPI(
return { ...acc, [name]: openapiSchema };
}
},
{} as Record<string, OpenAPIV3_1.SchemaObject | OpenAPIV3_1.ReferenceObject>,
{} as Record<string, OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject>,
);

return {
openapi: '3.1.0',
openapi: '3.0.0',
info,
paths,
components: {
Expand Down
6 changes: 3 additions & 3 deletions packages/openapi-generator/test/openapi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export const internalRoute = h.httpRoute({
`;

testCase('simple route', SIMPLE, {
openapi: '3.1.0',
openapi: '3.0.0',
info: {
title: 'Test',
version: '1.0.0',
Expand Down Expand Up @@ -205,7 +205,7 @@ export const route = h.httpRoute({
`;

testCase('request body route', REQUEST_BODY, {
openapi: '3.1.0',
openapi: '3.0.0',
info: {
title: 'Test',
version: '1.0.0',
Expand Down Expand Up @@ -276,7 +276,7 @@ export const route = h.httpRoute({
`;

testCase('request union route', UNION, {
openapi: '3.1.0',
openapi: '3.0.0',
info: {
title: 'Test',
version: '1.0.0',
Expand Down