Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support default return schemas in openapi codegen #4152

Merged
merged 1 commit into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions packages/rtk-query-codegen-openapi/src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -234,15 +237,15 @@ 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 },
ts.SyntaxKind.MultiLineCommentTrivia,
`* status ${code} ${response.description} `,
false
)
)
.filter((type) => type !== keywordType.void);
);
if (returnTypes.length > 0) {
ResponseType = factory.createUnionTypeNode(returnTypes);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down