Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use NameValueCollection instead of FormUrlEncodedContent in codegen, fix #109 #113

Merged
merged 1 commit into from
Apr 5, 2024
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
6 changes: 2 additions & 4 deletions srcgen/dotUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,11 @@ function formatAddQueryParam(param) {
let nc = param.required ? '' : `if (null != ${param.name}) `;

switch (prop.type) {
case 'array': return `${nc}queryParams.AddRange(${param.name}.Select(`
+ `w => new KeyValuePair<string?, string?>(${k}, ${formatQueryParamStringify('w', prop.items)})))`;
case 'array': return `${nc}Array.ForEach(${param.name}, w => queryParams.Add(${k}, ${formatQueryParamStringify('w', prop.items)}))`;
case 'object': throw new Error('Object unsupported for query param.');
default: {
let expr = param.name + ((param.required || 'string' === prop.type) ? '' : '.Value');
return `${nc}queryParams.Add(new KeyValuePair<string?, string?>(${k}, `
+ `${formatQueryParamStringify(expr, prop)}))`;
return `${nc}queryParams.Add(${k}, ${formatQueryParamStringify(expr, prop)})`;
}
}
}
Expand Down
11 changes: 5 additions & 6 deletions srcgen/endpointMethods.dt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
// Do not directly edit.
// Generated on {{= (new Date).toISOString() }}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections.Specialized;
using System.Net.Http;
using System.Runtime.CompilerServices;
using System.Text;
Expand Down Expand Up @@ -177,18 +178,16 @@ namespace Camille.{{= it.namespace }}
{
{{? isAsync }}
{{? queryParams.length }}
var queryParams = new List<KeyValuePair<string?, string?>>();
var queryParams = new NameValueCollection();
{{
for (const queryParam of queryParams)
{
{
}}
{{= dotUtils.formatAddQueryParam(queryParam) }};
{{
}
}}
HttpRequestMessage request;
using (var content = new FormUrlEncodedContent(queryParams))
request = new HttpRequestMessage(HttpMethod.{{= httpVerb }}, $"{{= dotUtils.replaceEnumCasts(route) }}?{content.ReadAsStringAsync().Result}");
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.{{= httpVerb }}, $"{{= dotUtils.replaceEnumCasts(route) }}?{queryParams}");
{{?}}
{{? !queryParams.length }}
var request = new HttpRequestMessage(HttpMethod.{{= httpVerb }}, $"{{= dotUtils.replaceEnumCasts(route) }}");
Expand Down
Loading