Skip to content
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
10 changes: 8 additions & 2 deletions packages/openapi/src/gen-sdk-ts/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ const SPEC = {
servers: [{ url: 'https://api.petstore.io/v1' }],
paths: {
'/pets': {
get: { operationId: 'listPets', tags: ['pets'], responses: { '200': { description: 'ok' } } },
get: {
operationId: 'listPets',
tags: ['pets'],
parameters: [{ name: 'page-size', in: 'query', schema: { type: 'integer' } }],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 The test only exercises one query parameter at a time. It verifies that page-size maps to _page_size, but it doesn't confirm that a valid-identifier param coexisting in the same operation still gets the right wire key. Adding a second param (e.g. limit) to the fixture and asserting "limit": opts.query?.limit in the generated output would give complete confidence that the mapping loop works across both the transformed-name and unchanged-name branches.

Suggested change
parameters: [{ name: 'page-size', in: 'query', schema: { type: 'integer' } }],
parameters: [
{ name: 'page-size', in: 'query', schema: { type: 'integer' } },
{ name: 'limit', in: 'query', schema: { type: 'integer' } },
],

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

responses: { '200': { description: 'ok' } },
},
post: {
operationId: 'createPet',
tags: ['pets'],
Expand All @@ -38,7 +43,8 @@ describe('generateTsSdk', () => {
expect(files.map((f) => f.path).sort()).toEqual(['client.ts', 'package.json']);
const src = await readFile(join(outDir, 'client.ts'), 'utf8');
expect(src).toContain('class PetstoreClient');
expect(src).toContain('async listPets()');
expect(src).toContain('async listPets(opts: { query?: { _page_size?: number } } = {})');
expect(src).toContain('query: { "page-size": opts.query?._page_size }');
expect(src).toContain('async getPet(petId: string)');
expect(src).toContain('${encodeURIComponent(petId)}');
expect(src).toContain('async createPet(opts: { body: unknown }):');
Expand Down
4 changes: 3 additions & 1 deletion packages/openapi/src/gen-sdk-ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ function renderMethod(op: Operation): string {

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