From bea930a571b3c7c2f58a6aaf3fb2ebd123c99690 Mon Sep 17 00:00:00 2001 From: Yuki Yokotani Date: Thu, 19 Dec 2024 23:58:21 +0900 Subject: [PATCH] Fix incorrect handling of query parameters with values 0 or empty strings in CodeGeneration output --- packages/rtk-query-codegen-openapi/src/generate.ts | 6 +++++- .../test/generateEndpoints.test.ts | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/rtk-query-codegen-openapi/src/generate.ts b/packages/rtk-query-codegen-openapi/src/generate.ts index d128d100b4..cc70b35b2d 100644 --- a/packages/rtk-query-codegen-openapi/src/generate.ts +++ b/packages/rtk-query-codegen-openapi/src/generate.ts @@ -448,7 +448,11 @@ export async function generateApi( const encodedValue = encodeQueryParams && param.param?.in === 'query' ? factory.createConditionalExpression( - value, + factory.createBinaryExpression( + value, + ts.SyntaxKind.ExclamationEqualsToken, + factory.createNull() + ), undefined, factory.createCallExpression(factory.createIdentifier('encodeURIComponent'), undefined, [ factory.createCallExpression(factory.createIdentifier('String'), undefined, [value]), diff --git a/packages/rtk-query-codegen-openapi/test/generateEndpoints.test.ts b/packages/rtk-query-codegen-openapi/test/generateEndpoints.test.ts index 93ed235849..50024e7210 100644 --- a/packages/rtk-query-codegen-openapi/test/generateEndpoints.test.ts +++ b/packages/rtk-query-codegen-openapi/test/generateEndpoints.test.ts @@ -242,7 +242,7 @@ describe('option encodeQueryParams', () => { }); expect(api).toMatch( - /params:\s*{\s*\n\s*status:\s*queryArg\.status\s*\?\s*encodeURIComponent\(\s*String\(queryArg\.status\)\s*\)\s*:\s*undefined\s*,?\s*\n\s*}/s + /params:\s*{\s*\n\s*status:\s*queryArg\.status\s*!=\s*null\s*\?\s*encodeURIComponent\(\s*String\(queryArg\.status\)\s*\)\s*:\s*undefined\s*,?\s*\n\s*}/s ); });