Skip to content

Commit 68818d4

Browse files
authored
Fix generated SDK query parameter names (#754)
Co-authored-by: JoesnageL <294642943+JoesnageL@users.noreply.github.com>
1 parent a353458 commit 68818d4

2 files changed

Lines changed: 11 additions & 3 deletions

File tree

packages/openapi/src/gen-sdk-ts/index.test.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ const SPEC = {
1111
servers: [{ url: 'https://api.petstore.io/v1' }],
1212
paths: {
1313
'/pets': {
14-
get: { operationId: 'listPets', tags: ['pets'], responses: { '200': { description: 'ok' } } },
14+
get: {
15+
operationId: 'listPets',
16+
tags: ['pets'],
17+
parameters: [{ name: 'page-size', in: 'query', schema: { type: 'integer' } }],
18+
responses: { '200': { description: 'ok' } },
19+
},
1520
post: {
1621
operationId: 'createPet',
1722
tags: ['pets'],
@@ -38,7 +43,8 @@ describe('generateTsSdk', () => {
3843
expect(files.map((f) => f.path).sort()).toEqual(['client.ts', 'package.json']);
3944
const src = await readFile(join(outDir, 'client.ts'), 'utf8');
4045
expect(src).toContain('class PetstoreClient');
41-
expect(src).toContain('async listPets()');
46+
expect(src).toContain('async listPets(opts: { query?: { _page_size?: number } } = {})');
47+
expect(src).toContain('query: { "page-size": opts.query?._page_size }');
4248
expect(src).toContain('async getPet(petId: string)');
4349
expect(src).toContain('${encodeURIComponent(petId)}');
4450
expect(src).toContain('async createPet(opts: { body: unknown }):');

packages/openapi/src/gen-sdk-ts/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,9 @@ function renderMethod(op: Operation): string {
117117

118118
const pathExpr = op.path.replace(/\{([^}]+)\}/g, (_, n) => `\${encodeURIComponent(${safe(n)})}`);
119119
const callParts: string[] = [];
120-
if (queryParams.length) callParts.push('query: opts.query');
120+
if (queryParams.length) {
121+
callParts.push(`query: { ${queryParams.map((p) => `${JSON.stringify(p.name)}: opts.query?.${safe(p.name)}`).join(', ')} }`);
122+
}
121123
if (headerParams.length) callParts.push('headers: opts.headers');
122124
if (op.requestBody) callParts.push('body: opts.body');
123125
const callOpts = callParts.length ? `, { ${callParts.join(', ')} }` : '';

0 commit comments

Comments
 (0)