Skip to content

Commit 5ef252c

Browse files
feat: support per-schema @ContentType JSDoc tags
1 parent b97760f commit 5ef252c

File tree

2 files changed

+426
-9
lines changed

2 files changed

+426
-9
lines changed

packages/openapi-generator/src/openapi.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -346,9 +346,20 @@ function routeToOpenAPI(route: Route): [string, string, OpenAPIV3.OperationObjec
346346
? {}
347347
: {
348348
requestBody: {
349-
content: {
350-
[contentType]: { schema: schemaToOpenAPI(route.body) },
351-
},
349+
content: (() => {
350+
const emptyBlock: Block = {
351+
description: '',
352+
tags: [],
353+
source: [],
354+
problems: [],
355+
};
356+
const bodyJsdoc = parseCommentBlock(route.body.comment ?? emptyBlock);
357+
const requestContentType = bodyJsdoc.tags?.contentType ?? contentType;
358+
359+
return {
360+
[requestContentType]: { schema: schemaToOpenAPI(route.body) },
361+
};
362+
})(),
352363
},
353364
};
354365

@@ -394,12 +405,21 @@ function routeToOpenAPI(route: Route): [string, string, OpenAPIV3.OperationObjec
394405
responses: Object.entries(route.response).reduce((acc, [code, response]) => {
395406
const description = STATUS_CODES[code] ?? '';
396407

408+
const emptyBlock: Block = {
409+
description: '',
410+
tags: [],
411+
source: [],
412+
problems: [],
413+
};
414+
const responseJsdoc = parseCommentBlock(response.comment ?? emptyBlock);
415+
const responseContentType = responseJsdoc.tags?.contentType ?? contentType;
416+
397417
return {
398418
...acc,
399419
[Number(code)]: {
400420
description,
401421
content: {
402-
[contentType]: {
422+
[responseContentType]: {
403423
schema: schemaToOpenAPI(response),
404424
...(example !== undefined ? { example } : undefined),
405425
},

0 commit comments

Comments
 (0)