diff --git a/packages/rtk-query-codegen-openapi/src/generate.ts b/packages/rtk-query-codegen-openapi/src/generate.ts index 9c5c00c158..37fd77af48 100644 --- a/packages/rtk-query-codegen-openapi/src/generate.ts +++ b/packages/rtk-query-codegen-openapi/src/generate.ts @@ -26,6 +26,9 @@ import ts from 'typescript'; const generatedApiName = 'injectedRtkApi'; function defaultIsDataResponse(code: string) { + if (code === "default") { + return true; + } const parsedCode = Number(code); return !Number.isNaN(parsedCode) && parsedCode >= 200 && parsedCode < 300; } @@ -234,6 +237,7 @@ export async function generateApi( ] as const ) .filter(([status, response]) => isDataResponse(status, apiGen.resolve(response), responses || {})) + .filter(([_1, _2, type]) => type !== keywordType.void) .map(([code, response, type]) => ts.addSyntheticLeadingComment( { ...type }, @@ -241,8 +245,7 @@ export async function generateApi( `* status ${code} ${response.description} `, false ) - ) - .filter((type) => type !== keywordType.void); + ); if (returnTypes.length > 0) { ResponseType = factory.createUnionTypeNode(returnTypes); } diff --git a/packages/rtk-query-codegen-openapi/test/__snapshots__/cli.test.ts.snap b/packages/rtk-query-codegen-openapi/test/__snapshots__/cli.test.ts.snap index c91144ad08..292f58714c 100644 --- a/packages/rtk-query-codegen-openapi/test/__snapshots__/cli.test.ts.snap +++ b/packages/rtk-query-codegen-openapi/test/__snapshots__/cli.test.ts.snap @@ -143,7 +143,7 @@ export type DeleteOrderApiArg = { /** ID of the order that needs to be deleted */ orderId: number; }; -export type CreateUserApiResponse = unknown; +export type CreateUserApiResponse = /** status default successful operation */ User; export type CreateUserApiArg = { /** Created user object */ user: User; @@ -363,7 +363,7 @@ export type DeleteOrderApiArg = { /** ID of the order that needs to be deleted */ orderId: number; }; -export type CreateUserApiResponse = unknown; +export type CreateUserApiResponse = /** status default successful operation */ User; export type CreateUserApiArg = { /** Created user object */ user: User; diff --git a/packages/rtk-query-codegen-openapi/test/__snapshots__/generateEndpoints.test.ts.snap b/packages/rtk-query-codegen-openapi/test/__snapshots__/generateEndpoints.test.ts.snap index a88a4786d1..9e4de46937 100644 --- a/packages/rtk-query-codegen-openapi/test/__snapshots__/generateEndpoints.test.ts.snap +++ b/packages/rtk-query-codegen-openapi/test/__snapshots__/generateEndpoints.test.ts.snap @@ -186,7 +186,7 @@ export type DeleteOrderApiArg = { /** ID of the order that needs to be deleted */ orderId: number; }; -export type CreateUserApiResponse = unknown; +export type CreateUserApiResponse = /** status default successful operation */ User; export type CreateUserApiArg = { /** Created user object */ user: User; @@ -1031,7 +1031,7 @@ export type DeleteOrderApiArg = { /** ID of the order that needs to be deleted */ orderId: number; }; -export type CreateUserApiResponse = unknown; +export type CreateUserApiResponse = /** status default successful operation */ User; export type CreateUserApiArg = { /** Created user object */ user: User; @@ -1389,7 +1389,7 @@ export type DeleteOrderApiArg = { /** ID of the order that needs to be deleted */ orderId: number; }; -export type CreateUserApiResponse = unknown; +export type CreateUserApiResponse = /** status default successful operation */ User; export type CreateUserApiArg = { /** Created user object */ user: User; diff --git a/packages/rtk-query-codegen-openapi/test/generateEndpoints.test.ts b/packages/rtk-query-codegen-openapi/test/generateEndpoints.test.ts index d922e7ff55..a904840c00 100644 --- a/packages/rtk-query-codegen-openapi/test/generateEndpoints.test.ts +++ b/packages/rtk-query-codegen-openapi/test/generateEndpoints.test.ts @@ -22,6 +22,15 @@ test('calling without `outputFile` returns the generated api', async () => { expect(api).toMatchSnapshot(); }); +test('should set response type for request with default response type', async () => { + const api = await generateEndpoints({ + apiFile: './fixtures/emptyApi.ts', + schemaFile: resolve(__dirname, 'fixtures/petstore.json'), + }); + // eslint-disable-next-line no-template-curly-in-string + expect(api).toMatch(/export type CreateUserApiResponse =[\s\S/*]+status default successful operation[\s/*]+User;/); +}); + test('endpoint filtering', async () => { const api = await generateEndpoints({ unionUndefined: true,