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
15 changes: 12 additions & 3 deletions packages/openapi-generator/src/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,17 @@ function routeToOpenAPI(route: Route): [string, string, OpenAPIV3_1.OperationObj
const tag = jsdoc.tags?.tag ?? '';
const isInternal = jsdoc.tags?.private !== undefined;

const requestBody =
route.body === undefined
? {}
: {
requestBody: {
content: {
'application/json': { schema: schemaToOpenAPI(route.body) },
},
},
};

return [
route.path,
route.method.toLowerCase(),
Expand All @@ -106,9 +117,7 @@ function routeToOpenAPI(route: Route): [string, string, OpenAPIV3_1.OperationObj
schema: schema as any, // TODO: Something to disallow arrays
};
}),
...(route.body !== undefined
? { requestBody: schemaToOpenAPI(route.body) as any }
: {}),
...requestBody,
responses: Object.entries(route.response).reduce((acc, [code, response]) => {
const description = response.comment?.description ?? STATUS_CODES[code] ?? '';

Expand Down
64 changes: 64 additions & 0 deletions packages/openapi-generator/test/openapi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,70 @@ testCase('simple route', SIMPLE, {
},
});

const REQUEST_BODY = `
import * as t from 'io-ts';
import * as h from '@api-ts/io-ts-http';

export const route = h.httpRoute({
path: '/foo',
method: 'GET',
request: h.httpRequest({
body: {
foo: t.string,
},
}),
response: {
/** foo response */
200: t.string
},
});
`;

testCase('request body route', REQUEST_BODY, {
openapi: '3.1.0',
info: {
title: 'Test',
version: '1.0.0',
},
paths: {
'/foo': {
get: {
parameters: [],
requestBody: {
content: {
'application/json': {
schema: {
type: 'object',
properties: {
foo: {
type: 'string',
},
},
required: ['foo'],
},
},
},
},
responses: {
200: {
description: 'foo response',
content: {
'application/json': {
schema: {
type: 'string',
},
},
},
},
},
},
},
},
components: {
schemas: {},
},
});

const UNION = `
import * as t from 'io-ts';
import * as h from '@api-ts/io-ts-http';
Expand Down