Skip to content

Commit 00149e3

Browse files
authored
fix(query-parameter): Support element access (#68)
1 parent 50eab5b commit 00149e3

File tree

6 files changed

+45
-7
lines changed

6 files changed

+45
-7
lines changed

src/code-templates/api-client/ApiClientClass/MethodBody/index.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import ts from "typescript";
22

33
import type { TsGenerator } from "../../../../api";
44
import type { CodeGenerator } from "../../../../types";
5+
import { escapeText2 as escapeText } from "../../../../utils";
56
import * as Utils from "../../utils";
67
import * as CallRequest from "./CallRequest";
78
import * as HeaderParameter from "./HeaderParameter";
@@ -61,9 +62,11 @@ export const create = (factory: TsGenerator.Factory.Type, params: CodeGenerator.
6162
if (convertedParams.hasQueryParameters) {
6263
const queryParameter = pickedParameters.filter(item => item.in === "query");
6364
const queryObject = Object.values(queryParameter).reduce<{ [key: string]: QueryParameter.Item }>((previous, current) => {
65+
const { text, escaped } = escapeText(current.name);
66+
const variableDeclaraText = escaped ? `params.parameter[${text}]` : `params.parameter.${text}`;
6467
return {
6568
...previous,
66-
[current.name]: { type: "variable", value: `params.parameter.${current.name}`, style: current.style, explode: !!current.explode },
69+
[current.name]: { type: "variable", value: variableDeclaraText, style: current.style, explode: !!current.explode },
6770
};
6871
}, {});
6972
statements.push(QueryParameter.create(factory, { variableName: "queryParameters", object: queryObject }));

test/__tests__/__snapshots__/parameter-test.ts.snap

+13-1
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,10 @@ exports[`Parameter api.test.domain 1`] = `
332332
\\"name\\": \\"book.name\\",
333333
\\"in\\": \\"path\\",
334334
\\"required\\": true
335+
},
336+
{
337+
\\"name\\": \\"from.publishedAt\\",
338+
\\"in\\": \\"query\\"
335339
}
336340
],
337341
\\"requestContentTypes\\": [],
@@ -348,7 +352,7 @@ exports[`Parameter api.test.domain 1`] = `
348352
\\"successResponseFirstContentType\\": \\"application/json\\",
349353
\\"has2OrMoreSuccessResponseContentTypes\\": false,
350354
\\"hasAdditionalHeaders\\": false,
351-
\\"hasQueryParameters\\": false
355+
\\"hasQueryParameters\\": true
352356
},
353357
\\"operationParams\\": {
354358
\\"httpMethod\\": \\"get\\",
@@ -363,6 +367,14 @@ exports[`Parameter api.test.domain 1`] = `
363367
\\"schema\\": {
364368
\\"type\\": \\"string\\"
365369
}
370+
},
371+
{
372+
\\"in\\": \\"query\\",
373+
\\"name\\": \\"from.publishedAt\\",
374+
\\"schema\\": {
375+
\\"type\\": \\"integer\\",
376+
\\"format\\": \\"int32\\"
377+
}
366378
}
367379
],
368380
\\"responses\\": {

test/__tests__/__snapshots__/spit-code-test.ts.snap

+5-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export interface Response$getReferenceItems$Status$200 {
4444
}
4545
export interface Parameter$searchBook {
4646
\\"book.name\\": string;
47+
\\"from.publishedAt\\"?: number;
4748
}
4849
export interface Response$searchBook$Status$200 {
4950
\\"application/json\\": {
@@ -183,7 +184,10 @@ export class Client<RequestOption> {
183184
const headers = {
184185
Accept: \\"application/json\\"
185186
};
186-
return this.apiClient.request(\\"GET\\", url, headers, undefined, undefined, option);
187+
const queryParameters: QueryParameters = {
188+
\\"from.publishedAt\\": { value: params.parameter[\\"from.publishedAt\\"], explode: false }
189+
};
190+
return this.apiClient.request(\\"GET\\", url, headers, undefined, queryParameters, option);
187191
}
188192
/**
189193
* operationId: getBookById

test/__tests__/__snapshots__/template-only-test.ts.snap

+8-2
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,10 @@ export class Client<RequestOption> {
106106
const headers = {
107107
Accept: \\"application/json\\"
108108
};
109-
return this.apiClient.request(\\"GET\\", url, headers, undefined, undefined, option);
109+
const queryParameters: QueryParameters = {
110+
\\"from.publishedAt\\": { value: params.parameter[\\"from.publishedAt\\"], explode: false }
111+
};
112+
return this.apiClient.request(\\"GET\\", url, headers, undefined, queryParameters, option);
110113
}
111114
public async getBookById(params: Params$getBookById, option?: RequestOption): Promise<Response$getBookById$Status$200[\\"application/json\\"]> {
112115
const url = this.baseUrl + \`/get/books/\${params.parameter.id}\`;
@@ -232,7 +235,10 @@ export class Client<RequestOption> {
232235
const headers = {
233236
Accept: \\"application/json\\"
234237
};
235-
return this.apiClient.request(\\"GET\\", url, headers, undefined, undefined, option);
238+
const queryParameters: QueryParameters = {
239+
\\"from.publishedAt\\": { value: params.parameter[\\"from.publishedAt\\"], explode: false }
240+
};
241+
return this.apiClient.request(\\"GET\\", url, headers, undefined, queryParameters, option);
236242
}
237243
public getBookById(params: Params$getBookById, option?: RequestOption): Response$getBookById$Status$200[\\"application/json\\"] {
238244
const url = this.baseUrl + \`/get/books/\${params.parameter.id}\`;

test/__tests__/__snapshots__/typedef-with-template-test.ts.snap

+10-2
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ export interface Response$getReferenceItems$Status$200 {
320320
}
321321
export interface Parameter$searchBook {
322322
\\"book.name\\": string;
323+
\\"from.publishedAt\\"?: number;
323324
}
324325
export interface Response$searchBook$Status$200 {
325326
\\"application/json\\": {
@@ -439,7 +440,10 @@ export class Client<RequestOption> {
439440
const headers = {
440441
Accept: \\"application/json\\"
441442
};
442-
return this.apiClient.request(\\"GET\\", url, headers, undefined, undefined, option);
443+
const queryParameters: QueryParameters = {
444+
\\"from.publishedAt\\": { value: params.parameter[\\"from.publishedAt\\"], explode: false }
445+
};
446+
return this.apiClient.request(\\"GET\\", url, headers, undefined, queryParameters, option);
443447
}
444448
public async getBookById(params: Params$getBookById, option?: RequestOption): Promise<Response$getBookById$Status$200[\\"application/json\\"]> {
445449
const url = this.baseUrl + \`/get/books/\${params.parameter.id}\`;
@@ -842,6 +846,7 @@ export interface Response$getReferenceItems$Status$200 {
842846
}
843847
export interface Parameter$searchBook {
844848
\\"book.name\\": string;
849+
\\"from.publishedAt\\"?: number;
845850
}
846851
export interface Response$searchBook$Status$200 {
847852
\\"application/json\\": {
@@ -961,7 +966,10 @@ export class Client<RequestOption> {
961966
const headers = {
962967
Accept: \\"application/json\\"
963968
};
964-
return this.apiClient.request(\\"GET\\", url, headers, undefined, undefined, option);
969+
const queryParameters: QueryParameters = {
970+
\\"from.publishedAt\\": { value: params.parameter[\\"from.publishedAt\\"], explode: false }
971+
};
972+
return this.apiClient.request(\\"GET\\", url, headers, undefined, queryParameters, option);
965973
}
966974
public getBookById(params: Params$getBookById, option?: RequestOption): Response$getBookById$Status$200[\\"application/json\\"] {
967975
const url = this.baseUrl + \`/get/books/\${params.parameter.id}\`;

test/api.test.domain/index.yml

+5
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,11 @@ paths:
389389
required: true
390390
schema:
391391
type: string
392+
- in: query
393+
name: from.publishedAt
394+
schema:
395+
type: integer
396+
format: int32
392397
responses:
393398
200:
394399
description: Search Book Result

0 commit comments

Comments
 (0)