Skip to content

Commit d686ad4

Browse files
authored
Fix form parameter and delete it only when undefined (#600)
#597 (comment) As I checked in my local, channel api client's request is rejected. The root cause was that parameter used camel case as form parameter. Snake case is expected according to official document. This change fixes it.
1 parent 421fedd commit d686ad4

File tree

4 files changed

+68
-28
lines changed

4 files changed

+68
-28
lines changed

generator/src/main/resources/line-bot-sdk-nodejs-generator/apiBody/normal.pebble

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,14 @@
1010
{% if op.hasBodyParam %}const params = {{op.bodyParam.paramName}};
1111
{% elseif op.hasFormParams %}const formParams = {
1212
{% for param in op.formParams -%}
13-
"{{param.paramName}}": {{param.paramName}},
13+
"{{param.baseName}}": {{param.paramName}},
1414
{% endfor %}
1515
};
16+
Object.keys(formParams).forEach((key: keyof typeof formParams) => {
17+
if (formParams[key] === undefined) {
18+
delete formParams[key];
19+
}
20+
});
1621
{% elseif op.hasQueryParams %}const queryParams = {
1722
{% for param in op.queryParams -%}
1823
"{{param.paramName}}": {{param.paramName}},

lib/channel-access-token/api/channelAccessTokenClient.ts

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,15 @@ export class ChannelAccessTokenClient {
9696
clientSecret?: string,
9797
): Promise<IssueShortLivedChannelAccessTokenResponse> {
9898
const formParams = {
99-
grantType: grantType,
100-
clientId: clientId,
101-
clientSecret: clientSecret,
99+
grant_type: grantType,
100+
client_id: clientId,
101+
client_secret: clientSecret,
102102
};
103+
Object.keys(formParams).forEach((key: keyof typeof formParams) => {
104+
if (formParams[key] === undefined) {
105+
delete formParams[key];
106+
}
107+
});
103108

104109
const res =
105110
this.httpClient.postForm<IssueShortLivedChannelAccessTokenResponse>(
@@ -122,10 +127,15 @@ export class ChannelAccessTokenClient {
122127
clientAssertion?: string,
123128
): Promise<IssueChannelAccessTokenResponse> {
124129
const formParams = {
125-
grantType: grantType,
126-
clientAssertionType: clientAssertionType,
127-
clientAssertion: clientAssertion,
130+
grant_type: grantType,
131+
client_assertion_type: clientAssertionType,
132+
client_assertion: clientAssertion,
128133
};
134+
Object.keys(formParams).forEach((key: keyof typeof formParams) => {
135+
if (formParams[key] === undefined) {
136+
delete formParams[key];
137+
}
138+
});
129139

130140
const res = this.httpClient.postForm<IssueChannelAccessTokenResponse>(
131141
"/oauth2/v2.1/token",
@@ -151,12 +161,17 @@ export class ChannelAccessTokenClient {
151161
clientSecret?: string,
152162
): Promise<IssueStatelessChannelAccessTokenResponse> {
153163
const formParams = {
154-
grantType: grantType,
155-
clientAssertionType: clientAssertionType,
156-
clientAssertion: clientAssertion,
157-
clientId: clientId,
158-
clientSecret: clientSecret,
164+
grant_type: grantType,
165+
client_assertion_type: clientAssertionType,
166+
client_assertion: clientAssertion,
167+
client_id: clientId,
168+
client_secret: clientSecret,
159169
};
170+
Object.keys(formParams).forEach((key: keyof typeof formParams) => {
171+
if (formParams[key] === undefined) {
172+
delete formParams[key];
173+
}
174+
});
160175

161176
const res =
162177
this.httpClient.postForm<IssueStatelessChannelAccessTokenResponse>(
@@ -175,8 +190,13 @@ export class ChannelAccessTokenClient {
175190
accessToken?: string,
176191
): Promise<Types.MessageAPIResponseBase> {
177192
const formParams = {
178-
accessToken: accessToken,
193+
access_token: accessToken,
179194
};
195+
Object.keys(formParams).forEach((key: keyof typeof formParams) => {
196+
if (formParams[key] === undefined) {
197+
delete formParams[key];
198+
}
199+
});
180200

181201
const res = this.httpClient.postForm("/v2/oauth/revoke", formParams);
182202
return ensureJSON(res);
@@ -195,10 +215,15 @@ export class ChannelAccessTokenClient {
195215
accessToken?: string,
196216
): Promise<Types.MessageAPIResponseBase> {
197217
const formParams = {
198-
clientId: clientId,
199-
clientSecret: clientSecret,
200-
accessToken: accessToken,
218+
client_id: clientId,
219+
client_secret: clientSecret,
220+
access_token: accessToken,
201221
};
222+
Object.keys(formParams).forEach((key: keyof typeof formParams) => {
223+
if (formParams[key] === undefined) {
224+
delete formParams[key];
225+
}
226+
});
202227

203228
const res = this.httpClient.postForm("/oauth2/v2.1/revoke", formParams);
204229
return ensureJSON(res);
@@ -213,8 +238,13 @@ export class ChannelAccessTokenClient {
213238
accessToken?: string,
214239
): Promise<VerifyChannelAccessTokenResponse> {
215240
const formParams = {
216-
accessToken: accessToken,
241+
access_token: accessToken,
217242
};
243+
Object.keys(formParams).forEach((key: keyof typeof formParams) => {
244+
if (formParams[key] === undefined) {
245+
delete formParams[key];
246+
}
247+
});
218248

219249
const res = this.httpClient.postForm<VerifyChannelAccessTokenResponse>(
220250
"/v2/oauth/verify",

lib/module-attach/api/lineModuleAttachClient.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,22 @@ export class LineModuleAttachClient {
8686
brandType?: string,
8787
): Promise<AttachModuleResponse> {
8888
const formParams = {
89-
grantType: grantType,
89+
grant_type: grantType,
9090
code: code,
91-
redirectUri: redirectUri,
92-
codeVerifier: codeVerifier,
93-
clientId: clientId,
94-
clientSecret: clientSecret,
91+
redirect_uri: redirectUri,
92+
code_verifier: codeVerifier,
93+
client_id: clientId,
94+
client_secret: clientSecret,
9595
region: region,
96-
basicSearchId: basicSearchId,
96+
basic_search_id: basicSearchId,
9797
scope: scope,
98-
brandType: brandType,
98+
brand_type: brandType,
9999
};
100+
Object.keys(formParams).forEach((key: keyof typeof formParams) => {
101+
if (formParams[key] === undefined) {
102+
delete formParams[key];
103+
}
104+
});
100105

101106
const res = this.httpClient.postForm<AttachModuleResponse>(
102107
"/module/auth/v1/token",

test/libs-channelAccessToken.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ describe("channelAccessToken", () => {
3434
);
3535
equal(
3636
await request.text(),
37-
"grantType=test_client_id&clientAssertionType=test_client_secret&clientAssertion=test_grant_type&clientId=test_redirect_uri&clientSecret=test_code",
37+
"grant_type=test_client_id&client_id=1234&client_secret=test_code",
3838
);
3939

4040
return HttpResponse.json({});
@@ -44,9 +44,9 @@ describe("channelAccessToken", () => {
4444

4545
const res = await client.issueStatelessChannelToken(
4646
"test_client_id",
47-
"test_client_secret",
48-
"test_grant_type",
49-
"test_redirect_uri",
47+
undefined,
48+
undefined,
49+
"1234",
5050
"test_code",
5151
);
5252
deepEqual(res, {});

0 commit comments

Comments
 (0)