Skip to content

Commit c6371df

Browse files
authored
Merge pull request #4152 from barp/master
2 parents 586184e + 7d5fa42 commit c6371df

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

packages/rtk-query-codegen-openapi/src/generate.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ import { factory } from './utils/factory';
2222
const generatedApiName = 'injectedRtkApi';
2323

2424
function defaultIsDataResponse(code: string) {
25+
if (code === "default") {
26+
return true;
27+
}
2528
const parsedCode = Number(code);
2629
return !Number.isNaN(parsedCode) && parsedCode >= 200 && parsedCode < 300;
2730
}
@@ -232,15 +235,15 @@ export async function generateApi(
232235
] as const
233236
)
234237
.filter(([status, response]) => isDataResponse(status, apiGen.resolve(response), responses || {}))
238+
.filter(([_1, _2, type]) => type !== keywordType.void)
235239
.map(([code, response, type]) =>
236240
ts.addSyntheticLeadingComment(
237241
{ ...type },
238242
ts.SyntaxKind.MultiLineCommentTrivia,
239243
`* status ${code} ${response.description} `,
240244
false
241245
)
242-
)
243-
.filter((type) => type !== keywordType.void);
246+
);
244247
if (returnTypes.length > 0) {
245248
ResponseType = factory.createUnionTypeNode(returnTypes);
246249
}

packages/rtk-query-codegen-openapi/test/__snapshots__/cli.test.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ export type DeleteOrderApiArg = {
167167
/** ID of the order that needs to be deleted */
168168
orderId: number;
169169
};
170-
export type CreateUserApiResponse = unknown;
170+
export type CreateUserApiResponse = /** status default successful operation */ User;
171171
export type CreateUserApiArg = {
172172
/** Created user object */
173173
user: User;
@@ -414,7 +414,7 @@ export type DeleteOrderApiArg = {
414414
/** ID of the order that needs to be deleted */
415415
orderId: number;
416416
};
417-
export type CreateUserApiResponse = unknown;
417+
export type CreateUserApiResponse = /** status default successful operation */ User;
418418
export type CreateUserApiArg = {
419419
/** Created user object */
420420
user: User;

packages/rtk-query-codegen-openapi/test/__snapshots__/generateEndpoints.test.ts.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ export type DeleteOrderApiArg = {
167167
/** ID of the order that needs to be deleted */
168168
orderId: number;
169169
};
170-
export type CreateUserApiResponse = unknown;
170+
export type CreateUserApiResponse = /** status default successful operation */ User;
171171
export type CreateUserApiArg = {
172172
/** Created user object */
173173
user: User;
@@ -1647,7 +1647,7 @@ export type DeleteOrderApiArg = {
16471647
/** ID of the order that needs to be deleted */
16481648
orderId: number;
16491649
};
1650-
export type CreateUserApiResponse = unknown;
1650+
export type CreateUserApiResponse = /** status default successful operation */ User;
16511651
export type CreateUserApiArg = {
16521652
/** Created user object */
16531653
user: User;
@@ -2048,7 +2048,7 @@ export type DeleteOrderApiArg = {
20482048
/** ID of the order that needs to be deleted */
20492049
orderId: number;
20502050
};
2051-
export type CreateUserApiResponse = unknown;
2051+
export type CreateUserApiResponse = /** status default successful operation */ User;
20522052
export type CreateUserApiArg = {
20532053
/** Created user object */
20542054
user: User;

packages/rtk-query-codegen-openapi/test/generateEndpoints.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ test('calling without `outputFile` returns the generated api', async () => {
2626
expect(api).toMatchSnapshot();
2727
});
2828

29+
test('should set response type for request with default response type', async () => {
30+
const api = await generateEndpoints({
31+
apiFile: './fixtures/emptyApi.ts',
32+
schemaFile: resolve(__dirname, 'fixtures/petstore.json'),
33+
});
34+
// eslint-disable-next-line no-template-curly-in-string
35+
expect(api).toMatch(/export type CreateUserApiResponse =[\s\S/*]+status default successful operation[\s/*]+User;/);
36+
});
37+
2938
test('endpoint filtering', async () => {
3039
const api = await generateEndpoints({
3140
unionUndefined: true,

0 commit comments

Comments
 (0)