Skip to content

Commit fa5b012

Browse files
committed
Add "includeDefault" option to rtkq-codegen-openapi
1 parent 8b32304 commit fa5b012

File tree

5 files changed

+28
-26
lines changed

5 files changed

+28
-26
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ import { factory } from './utils/factory';
3030
const generatedApiName = 'injectedRtkApi';
3131
const v3DocCache: Record<string, OpenAPIV3.Document> = {};
3232

33-
function defaultIsDataResponse(code: string) {
34-
if (code === 'default') {
33+
function defaultIsDataResponse(code: string, includeDefault: boolean) {
34+
if (includeDefault && code === 'default') {
3535
return true;
3636
}
3737
const parsedCode = Number(code);
@@ -112,6 +112,7 @@ export async function generateApi(
112112
unionUndefined,
113113
encodeParams = false,
114114
flattenArg = false,
115+
includeDefault = false,
115116
useEnumType = false,
116117
mergeReadWriteOnly = false,
117118
httpResolverOptions,
@@ -253,7 +254,9 @@ export async function generateApi(
253254
factory.createKeywordTypeNode(ts.SyntaxKind.UndefinedKeyword),
254255
] as const
255256
)
256-
.filter(([status, response]) => isDataResponse(status, apiGen.resolve(response), responses || {}))
257+
.filter(([status, response]) =>
258+
isDataResponse(status, includeDefault, apiGen.resolve(response), responses || {})
259+
)
257260
.filter(([_1, _2, type]) => type !== keywordType.void)
258261
.map(([code, response, type]) =>
259262
ts.addSyntheticLeadingComment(

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export type GenerationOptions = Id<
2424
Optional<OutputFileOptions, 'outputFile'> & {
2525
isDataResponse?(
2626
code: string,
27+
includeDefault: boolean,
2728
response: OpenAPIV3.ResponseObject,
2829
allResponses: OpenAPIV3.ResponsesObject
2930
): boolean;
@@ -82,6 +83,12 @@ export interface CommonOptions {
8283
* `true` will "flatten" the arg so that you can do things like `useGetEntityById(1)` instead of `useGetEntityById({ entityId: 1 })`
8384
*/
8485
flattenArg?: boolean;
86+
/**
87+
* default to false
88+
* If set to `true`, the default response type will be included in the generated code for all endpoints.
89+
* @see https://swagger.io/docs/specification/describing-responses/#default
90+
*/
91+
includeDefault?: boolean;
8592
/**
8693
* default to false
8794
* `true` will not generate separate types for read-only and write-only properties.

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 = /** status default successful operation */ User;
170+
export type CreateUserApiResponse = unknown;
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 = /** status default successful operation */ User;
417+
export type CreateUserApiResponse = unknown;
418418
export type CreateUserApiArg = {
419419
/** Created user object */
420420
user: User;

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

Lines changed: 11 additions & 20 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 = /** status default successful operation */ User;
170+
export type CreateUserApiResponse = unknown;
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 = /** status default successful operation */ User;
417+
export type CreateUserApiResponse = unknown;
418418
export type CreateUserApiArg = {
419419
/** Created user object */
420420
user: User;
@@ -714,8 +714,7 @@ export type DeleteOrderApiArg = {
714714
/** ID of the order that needs to be deleted */
715715
orderId: number;
716716
};
717-
export type CreateUserApiResponse =
718-
/** status default successful operation */ User;
717+
export type CreateUserApiResponse = unknown;
719718
export type CreateUserApiArg = {
720719
/** Created user object */
721720
user: User;
@@ -1125,8 +1124,7 @@ export type DeleteOrderApiArg = {
11251124
/** ID of the order that needs to be deleted */
11261125
orderId: number;
11271126
};
1128-
export type CreateUserApiResponse =
1129-
/** status default successful operation */ User;
1127+
export type CreateUserApiResponse = unknown;
11301128
export type CreateUserApiArg = {
11311129
/** Created user object */
11321130
user: User;
@@ -1404,8 +1402,7 @@ export type DeleteOrderApiArg = {
14041402
/** ID of the order that needs to be deleted */
14051403
orderId: number;
14061404
};
1407-
export type CreateUserApiResponse =
1408-
/** status default successful operation */ User;
1405+
export type CreateUserApiResponse = unknown;
14091406
export type CreateUserApiArg = {
14101407
/** Created user object */
14111408
user: User;
@@ -1702,8 +1699,7 @@ export type DeleteOrderApiArg = {
17021699
/** ID of the order that needs to be deleted */
17031700
orderId: number;
17041701
};
1705-
export type CreateUserApiResponse =
1706-
/** status default successful operation */ User;
1702+
export type CreateUserApiResponse = unknown;
17071703
export type CreateUserApiArg = {
17081704
/** Created user object */
17091705
user: User;
@@ -1986,8 +1982,7 @@ export type DeleteOrderApiArg = {
19861982
/** ID of the order that needs to be deleted */
19871983
orderId: number;
19881984
};
1989-
export type CreateUserApiResponse =
1990-
/** status default successful operation */ User;
1985+
export type CreateUserApiResponse = unknown;
19911986
export type CreateUserApiArg = {
19921987
/** Created user object */
19931988
user: User;
@@ -2262,8 +2257,7 @@ export type DeleteOrderApiArg = {
22622257
/** ID of the order that needs to be deleted */
22632258
orderId: number;
22642259
};
2265-
export type CreateUserApiResponse =
2266-
/** status default successful operation */ User;
2260+
export type CreateUserApiResponse = unknown;
22672261
export type CreateUserApiArg = {
22682262
/** Created user object */
22692263
user: User;
@@ -2757,8 +2751,7 @@ export type DeleteOrderV2ApiArg = {
27572751
/** ID of the order that needs to be deleted */
27582752
orderId: number;
27592753
};
2760-
export type CreateUserV2ApiResponse =
2761-
/** status default successful operation */ User;
2754+
export type CreateUserV2ApiResponse = unknown;
27622755
export type CreateUserV2ApiArg = {
27632756
/** Created user object */
27642757
user: User;
@@ -3385,8 +3378,7 @@ export type DeleteOrderApiArg = {
33853378
/** ID of the order that needs to be deleted */
33863379
orderId: number;
33873380
};
3388-
export type CreateUserApiResponse =
3389-
/** status default successful operation */ User;
3381+
export type CreateUserApiResponse = unknown;
33903382
export type CreateUserApiArg = {
33913383
/** Created user object */
33923384
user: User;
@@ -3787,8 +3779,7 @@ export type DeleteOrderApiArg = {
37873779
/** ID of the order that needs to be deleted */
37883780
orderId: number;
37893781
};
3790-
export type CreateUserApiResponse =
3791-
/** status default successful operation */ User;
3782+
export type CreateUserApiResponse = unknown;
37923783
export type CreateUserApiArg = {
37933784
/** Created user object */
37943785
user: User;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ 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 () => {
29+
test('should include default response type in request when includeDefault is set to true', async () => {
3030
const api = await generateEndpoints({
3131
apiFile: './fixtures/emptyApi.ts',
3232
schemaFile: resolve(__dirname, 'fixtures/petstore.json'),
33+
includeDefault: true,
3334
});
3435
// eslint-disable-next-line no-template-curly-in-string
3536
expect(api).toMatch(/export type CreateUserApiResponse =[\s\S/*]+status default successful operation[\s/*]+User;/);

0 commit comments

Comments
 (0)