Skip to content

Commit 20bcc26

Browse files
bayasdevmarkerikson
authored andcommitted
Cast parameters to string
1 parent 52b0b0c commit 20bcc26

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,9 @@ export async function generateApi(
396396
return createPropertyAssignment(
397397
param.originalName,
398398
encodeParams && param.param?.in === 'query'
399-
? factory.createCallExpression(factory.createIdentifier('encodeURIComponent'), undefined, [value])
399+
? factory.createCallExpression(factory.createIdentifier('encodeURIComponent'), undefined, [
400+
factory.createCallExpression(factory.createIdentifier('String'), undefined, [value]),
401+
])
400402
: value
401403
);
402404
});
@@ -487,7 +489,9 @@ function generatePathExpression(
487489
expressions.map(([prop, literal], index) => {
488490
const value = isFlatArg ? rootObject : accessProperty(rootObject, prop);
489491
const encodedValue = encodeParams
490-
? factory.createCallExpression(factory.createIdentifier('encodeURIComponent'), undefined, [value])
492+
? factory.createCallExpression(factory.createIdentifier('encodeURIComponent'), undefined, [
493+
factory.createCallExpression(factory.createIdentifier('String'), undefined, [value]),
494+
])
491495
: value;
492496
return factory.createTemplateSpan(
493497
encodedValue,

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ describe('option encodeParams', () => {
8888
...config,
8989
filterEndpoints: ['findPetsByStatus'],
9090
});
91-
expect(api).toContain('status: encodeURIComponent(queryArg.status)');
91+
expect(api).toContain('status: encodeURIComponent(String(queryArg.status))');
9292
});
9393

9494
it('should encode path parameters', async () => {
@@ -97,7 +97,7 @@ describe('option encodeParams', () => {
9797
filterEndpoints: ['getOrderById'],
9898
});
9999
// eslint-disable-next-line no-template-curly-in-string
100-
expect(api).toContain('`/store/order/${encodeURIComponent(queryArg.orderId)}`');
100+
expect(api).toContain('`/store/order/${encodeURIComponent(String(queryArg.orderId))}`');
101101
});
102102

103103
it('should not encode body parameters', async () => {
@@ -106,7 +106,7 @@ describe('option encodeParams', () => {
106106
filterEndpoints: ['addPet'],
107107
});
108108
expect(api).toContain('body: queryArg.pet');
109-
expect(api).not.toContain('body: encodeURIComponent(queryArg.pet)');
109+
expect(api).not.toContain('body: encodeURIComponent(String(queryArg.pet))');
110110
});
111111

112112
it('should work correctly with flattenArg option', async () => {
@@ -116,7 +116,7 @@ describe('option encodeParams', () => {
116116
filterEndpoints: ['getOrderById'],
117117
});
118118
// eslint-disable-next-line no-template-curly-in-string
119-
expect(api).toContain('`/store/order/${encodeURIComponent(queryArg)}`');
119+
expect(api).toContain('`/store/order/${encodeURIComponent(String(queryArg))}`');
120120
});
121121

122122
it('should not encode parameters when encodeParams is false', async () => {

0 commit comments

Comments
 (0)