Skip to content

Commit b27ad85

Browse files
committed
fix: fix format of request bodies in openapi-generator
1 parent 3b9b3de commit b27ad85

File tree

2 files changed

+76
-3
lines changed

2 files changed

+76
-3
lines changed

packages/openapi-generator/src/openapi.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,17 @@ function routeToOpenAPI(route: Route): [string, string, OpenAPIV3_1.OperationObj
8282
const tag = jsdoc.tags?.tag ?? '';
8383
const isInternal = jsdoc.tags?.private !== undefined;
8484

85+
const requestBody =
86+
route.body === undefined
87+
? {}
88+
: {
89+
requestBody: {
90+
content: {
91+
'application/json': { schema: schemaToOpenAPI(route.body) },
92+
},
93+
},
94+
};
95+
8596
return [
8697
route.path,
8798
route.method.toLowerCase(),
@@ -106,9 +117,7 @@ function routeToOpenAPI(route: Route): [string, string, OpenAPIV3_1.OperationObj
106117
schema: schema as any, // TODO: Something to disallow arrays
107118
};
108119
}),
109-
...(route.body !== undefined
110-
? { requestBody: schemaToOpenAPI(route.body) as any }
111-
: {}),
120+
...requestBody,
112121
responses: Object.entries(route.response).reduce((acc, [code, response]) => {
113122
const description = response.comment?.description ?? STATUS_CODES[code] ?? '';
114123

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,70 @@ testCase('simple route', SIMPLE, {
185185
},
186186
});
187187

188+
const REQUEST_BODY = `
189+
import * as t from 'io-ts';
190+
import * as h from '@api-ts/io-ts-http';
191+
192+
export const route = h.httpRoute({
193+
path: '/foo',
194+
method: 'GET',
195+
request: h.httpRequest({
196+
body: {
197+
foo: t.string,
198+
},
199+
}),
200+
response: {
201+
/** foo response */
202+
200: t.string
203+
},
204+
});
205+
`;
206+
207+
testCase('request body route', REQUEST_BODY, {
208+
openapi: '3.1.0',
209+
info: {
210+
title: 'Test',
211+
version: '1.0.0',
212+
},
213+
paths: {
214+
'/foo': {
215+
get: {
216+
parameters: [],
217+
requestBody: {
218+
content: {
219+
'application/json': {
220+
schema: {
221+
type: 'object',
222+
properties: {
223+
foo: {
224+
type: 'string',
225+
},
226+
},
227+
required: ['foo'],
228+
},
229+
},
230+
},
231+
},
232+
responses: {
233+
200: {
234+
description: 'foo response',
235+
content: {
236+
'application/json': {
237+
schema: {
238+
type: 'string',
239+
},
240+
},
241+
},
242+
},
243+
},
244+
},
245+
},
246+
},
247+
components: {
248+
schemas: {},
249+
},
250+
});
251+
188252
const UNION = `
189253
import * as t from 'io-ts';
190254
import * as h from '@api-ts/io-ts-http';

0 commit comments

Comments
 (0)