Skip to content

Commit 20adce9

Browse files
committed
fix(enum-values): handle enums in request body
1 parent 38b73b0 commit 20adce9

File tree

2 files changed

+95
-1
lines changed

2 files changed

+95
-1
lines changed

packages/openapi-typescript/src/transform/request-body-object.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export default function transformRequestBodyObject(
1515
): ts.TypeNode {
1616
const type: ts.TypeElement[] = [];
1717
for (const [contentType, mediaTypeObject] of getEntries(requestBodyObject.content ?? {}, options.ctx)) {
18-
const nextPath = createRef([options.path, contentType]);
18+
const nextPath = createRef([options.path, "content", contentType]);
1919
const mediaType =
2020
"$ref" in mediaTypeObject
2121
? transformSchemaObject(mediaTypeObject, {

packages/openapi-typescript/test/node-api.test.ts

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,6 +1210,99 @@ export type operations = Record<string, never>;`,
12101210
ci: { timeout: 30_000 },
12111211
},
12121212
],
1213+
[
1214+
"options > enumValues with request body enum",
1215+
{
1216+
given: {
1217+
openapi: "3.1",
1218+
info: { title: "Test", version: "1.0" },
1219+
paths: {
1220+
"/test": {
1221+
get: {
1222+
requestBody: {
1223+
content: {
1224+
"application/json": {
1225+
schema: {
1226+
properties: {
1227+
status: {
1228+
type: "string",
1229+
enum: ["active", "inactive"],
1230+
},
1231+
},
1232+
},
1233+
},
1234+
},
1235+
},
1236+
responses: { 200: { description: "OK" } },
1237+
},
1238+
},
1239+
},
1240+
},
1241+
want: `export interface paths {
1242+
"/test": {
1243+
parameters: {
1244+
query?: never;
1245+
header?: never;
1246+
path?: never;
1247+
cookie?: never;
1248+
};
1249+
get: {
1250+
parameters: {
1251+
query?: never;
1252+
header?: never;
1253+
path?: never;
1254+
cookie?: never;
1255+
};
1256+
requestBody?: {
1257+
content: {
1258+
"application/json": {
1259+
/** @enum {string} */
1260+
status?: "active" | "inactive";
1261+
};
1262+
};
1263+
};
1264+
responses: {
1265+
/** @description OK */
1266+
200: {
1267+
headers: {
1268+
[name: string]: unknown;
1269+
};
1270+
content?: never;
1271+
};
1272+
};
1273+
};
1274+
put?: never;
1275+
post?: never;
1276+
delete?: never;
1277+
options?: never;
1278+
head?: never;
1279+
patch?: never;
1280+
trace?: never;
1281+
};
1282+
}
1283+
export type webhooks = Record<string, never>;
1284+
export interface components {
1285+
schemas: never;
1286+
responses: never;
1287+
parameters: never;
1288+
requestBodies: never;
1289+
headers: never;
1290+
pathItems: never;
1291+
}
1292+
export type $defs = Record<string, never>;
1293+
type FlattenedDeepRequired<T> = {
1294+
[K in keyof T]-?: FlattenedDeepRequired<T[K] extends unknown[] | undefined | null ? Extract<T[K], unknown[]>[number] : T[K]>;
1295+
};
1296+
type ReadonlyArray<T> = [
1297+
Exclude<T, undefined>
1298+
] extends [
1299+
unknown[]
1300+
] ? Readonly<Exclude<T, undefined>> : Readonly<Exclude<T, undefined>[]>;
1301+
export const pathsTestGetRequestBodyContentApplicationJsonStatusValues: ReadonlyArray<FlattenedDeepRequired<paths>["/test"]["get"]["requestBody"]["content"]["application/json"]["status"]> = ["active", "inactive"];
1302+
export type operations = Record<string, never>;`,
1303+
options: { enumValues: true },
1304+
},
1305+
],
12131306
];
12141307

12151308
for (const [testName, { given, want, options, ci }] of tests) {
@@ -1227,3 +1320,4 @@ export type operations = Record<string, never>;`,
12271320
);
12281321
}
12291322
});
1323+

0 commit comments

Comments
 (0)