Skip to content

Commit

Permalink
csharp-null-ref-fix (#46)
Browse files Browse the repository at this point in the history
* csharp-null-ref-fix

* Update CSharpClientGenerator.ts

* Update CSharpClientGenerator.ts
  • Loading branch information
barnuri authored Aug 15, 2024
1 parent 5676488 commit a3a4110
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions src/generators/client/CSharpClientGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,7 @@ ${controllerPropsCtor}
const haveQueryParams = controllerPath.queryParams.length > 0;
url += !haveQueryParams
? ''
: '?' + controllerPath.queryParams.map(x => {
if (this.getPropDesc(x.schema!).includes('[]')) {
return `{string.Join("&", q${capitalize(x.name)}?.Select(x => $"${x.name}={x}"))}`;
}
return `${x.name}={q${capitalize(x.name)}}`;
}).join('&');
: `?{string.Join("&", query)}`;
const queryParams = haveQueryParams
? controllerPath.queryParams
.map(
Expand All @@ -102,7 +97,19 @@ ${controllerPropsCtor}
)
.join(', ') + ', '
: ``;

let queryContent = ``;
if (haveQueryParams) {
queryContent += `\t\tvar query = new List<string>();\n`;
for (const queryParam of controllerPath.queryParams) {
const csharpParam = `q${capitalize(queryParam.name)}`;
const isList = this.getPropDesc(queryParam.schema!).includes('[]');
if (isList) {
queryContent += `\t\tif (${csharpParam} != null) { query.AddRange(${csharpParam}.Select(x => $"${queryParam.name}={x}")); }\n`
} else {
queryContent += `\t\tquery.Add($"${queryParam.name}={${csharpParam}}");\n`
}
}
}
let methodCommonText = `\t\t\t"${capitalize(controllerPath.method.toLowerCase())}",\n`;
methodCommonText += `\t\t\t\$"${url}\",\n`;
methodCommonText += `\t\t\t${controllerPath.body.haveBody ? 'body' : 'default'},\n`;
Expand All @@ -125,31 +132,33 @@ ${controllerPropsCtor}
let methodContent = '';
// method one
methodContent += `\tpublic Task<${responseType}${nullableMark}> ${methodName}Async(${methodParams}) \n\t{\n`.replace(', )', ')');
methodContent += queryContent;
methodContent += `\t\treturn Method<${requestType},${responseType}${nullableMark}>(\n`;
methodContent += methodCommonText;

// method two
methodContent += `\tpublic Task<T${nullableMark}> ${methodName}Async<T>(${methodParams}) \n\t{\n`.replace(', )', ')');
methodContent += queryContent;
methodContent += `\t\treturn Method<${requestType},T${nullableMark}>(\n`;
methodContent += methodCommonText;

// method three
const genericBodyParam = controllerPath.body.haveBody ? `S body, ` : '';
const genericBodyMethodParams = `${genericBodyParam}${pathParams}${queryParams}${headersParams}`;
methodContent += `\tpublic Task<T${nullableMark}> ${methodName}Async<T, S>(${genericBodyMethodParams}) \n\t{\n`.replace(', )', ')');
methodContent += queryContent;
methodContent += `\t\treturn Method<S,T${nullableMark}>(\n`;
methodContent += methodCommonText;

// method four
methodContent += `\tpublic Task<string${nullableMark}> ${methodName}ContentAsync(${methodParams}) \n\t{\n`.replace(', )', ')');
methodContent += queryContent;
methodContent += `\t\treturn Method<${requestType}>(\n`;
methodContent += methodCommonText;

// method five
methodContent += `\tpublic Task<string${nullableMark}> ${methodName}ContentAsync<S>(${genericBodyMethodParams}) \n\t{\n`.replace(
', )',
')',
);
methodContent += `\tpublic Task<string${nullableMark}> ${methodName}ContentAsync<S>(${genericBodyMethodParams}) \n\t{\n`.replace(', )', ')',);
methodContent += queryContent;
methodContent += `\t\treturn Method<S>(\n`;
methodContent += methodCommonText;

Expand Down

0 comments on commit a3a4110

Please sign in to comment.