From 4b896c759c00a414b2b3d54acc1f0d3a580c802c Mon Sep 17 00:00:00 2001 From: "Peter S. May" Date: Thu, 2 Jul 2015 15:33:30 -0400 Subject: [PATCH] [C#] Use RestSharp's mechanism for path parameters in URL templates Previously, a simple find-and-replace had been used to substitute path parameters into the path. Among other omissions, this resulted in special characters such as `/` being left unescaped. The RestSharp request object, as it turns out, expects a path template in the same format as provided by Swagger (with param names in braces), to be filled in using `AddParameter()`. In this edit, the code now uses this mechanism. The form parameter values are now passed to `CallApi*()` as a `Dictionary`, where they are added to the request before submission. Since this was already how query and form parameters (etc.) were implemented, the resulting code is more consistent with itself than before. --- .../main/resources/csharp/ApiClient.mustache | 10 +++++++--- .../src/main/resources/csharp/api.mustache | 20 +++++++++++-------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/csharp/ApiClient.mustache b/modules/swagger-codegen/src/main/resources/csharp/ApiClient.mustache index c6f57bd4a37..b11a3a8c1b5 100644 --- a/modules/swagger-codegen/src/main/resources/csharp/ApiClient.mustache +++ b/modules/swagger-codegen/src/main/resources/csharp/ApiClient.mustache @@ -38,16 +38,16 @@ namespace {{packageName}}.Client { private Dictionary defaultHeaderMap = new Dictionary(); public Object CallApi(String Path, RestSharp.Method Method, Dictionary QueryParams, String PostBody, - Dictionary HeaderParams, Dictionary FormParams, Dictionary FileParams, String[] AuthSettings) { + Dictionary HeaderParams, Dictionary FormParams, Dictionary FileParams, Dictionary PathParams, String[] AuthSettings) { var response = Task.Run(async () => { - var resp = await CallApiAsync(Path, Method, QueryParams, PostBody, HeaderParams, FormParams, FileParams, AuthSettings); + var resp = await CallApiAsync(Path, Method, QueryParams, PostBody, HeaderParams, FormParams, FileParams, PathParams, AuthSettings); return resp; }); return response.Result; } public async Task CallApiAsync(String Path, RestSharp.Method Method, Dictionary QueryParams, String PostBody, - Dictionary HeaderParams, Dictionary FormParams, Dictionary FileParams, String[] AuthSettings) { + Dictionary HeaderParams, Dictionary FormParams, Dictionary FileParams, Dictionary PathParams, String[] AuthSettings) { var request = new RestRequest(Path, Method); @@ -57,6 +57,10 @@ namespace {{packageName}}.Client { foreach(KeyValuePair defaultHeader in this.defaultHeaderMap) request.AddHeader(defaultHeader.Key, defaultHeader.Value); + // add path parameter, if any + foreach(KeyValuePair param in PathParams) + request.AddParameter(param.Key, param.Value, ParameterType.UrlSegment); + // add header parameter, if any foreach(KeyValuePair param in HeaderParams) request.AddHeader(param.Key, param.Value); diff --git a/modules/swagger-codegen/src/main/resources/csharp/api.mustache b/modules/swagger-codegen/src/main/resources/csharp/api.mustache index 652d15a30b3..70bc634ca3a 100644 --- a/modules/swagger-codegen/src/main/resources/csharp/api.mustache +++ b/modules/swagger-codegen/src/main/resources/csharp/api.mustache @@ -92,16 +92,18 @@ namespace {{packageName}}.Api { {{/required}}{{/allParams}} var path = "{{path}}"; - path = path.Replace("{format}", "json"); - {{#pathParams}}path = path.Replace("{" + "{{baseName}}" + "}", apiClient.ParameterToString({{{paramName}}})); - {{/pathParams}} + var pathParams = new Dictionary(); var queryParams = new Dictionary(); var headerParams = new Dictionary(); var formParams = new Dictionary(); var fileParams = new Dictionary(); String postBody = null; + pathParams.Add("format", "json"); + {{#pathParams}} if ({{paramName}} != null) pathParams.Add("{{baseName}}", apiClient.ParameterToString({{paramName}})); // path parameter + {{/pathParams}} + {{#queryParams}} if ({{paramName}} != null) queryParams.Add("{{baseName}}", apiClient.ParameterToString({{paramName}})); // query parameter {{/queryParams}} {{#headerParams}} if ({{paramName}} != null) headerParams.Add("{{baseName}}", apiClient.ParameterToString({{paramName}})); // header parameter @@ -115,7 +117,7 @@ namespace {{packageName}}.Api { String[] authSettings = new String[] { {{#authMethods}}"{{name}}"{{#hasMore}}, {{/hasMore}}{{/authMethods}} }; // make the HTTP request - IRestResponse response = (IRestResponse) apiClient.CallApi(path, Method.{{httpMethod}}, queryParams, postBody, headerParams, formParams, fileParams, authSettings); + IRestResponse response = (IRestResponse) apiClient.CallApi(path, Method.{{httpMethod}}, queryParams, postBody, headerParams, formParams, fileParams, pathParams, authSettings); if (((int)response.StatusCode) >= 400) { throw new ApiException ((int)response.StatusCode, "Error calling {{nickname}}: " + response.Content, response.Content); @@ -137,16 +139,18 @@ namespace {{packageName}}.Api { {{/required}}{{/allParams}} var path = "{{path}}"; - path = path.Replace("{format}", "json"); - {{#pathParams}}path = path.Replace("{" + "{{baseName}}" + "}", apiClient.ParameterToString({{{paramName}}})); - {{/pathParams}} + var pathParams = new Dictionary(); var queryParams = new Dictionary(); var headerParams = new Dictionary(); var formParams = new Dictionary(); var fileParams = new Dictionary(); String postBody = null; + pathParams.Add("format", "json"); + {{#pathParams}} if ({{paramName}} != null) pathParams.Add("{{baseName}}", apiClient.ParameterToString({{paramName}})); // path parameter + {{/pathParams}} + {{#queryParams}} if ({{paramName}} != null) queryParams.Add("{{baseName}}", apiClient.ParameterToString({{paramName}})); // query parameter {{/queryParams}} {{#headerParams}} if ({{paramName}} != null) headerParams.Add("{{baseName}}", apiClient.ParameterToString({{paramName}})); // header parameter @@ -160,7 +164,7 @@ namespace {{packageName}}.Api { String[] authSettings = new String[] { {{#authMethods}}"{{name}}"{{#hasMore}}, {{/hasMore}}{{/authMethods}} }; // make the HTTP request - IRestResponse response = (IRestResponse) await apiClient.CallApiAsync(path, Method.{{httpMethod}}, queryParams, postBody, headerParams, formParams, fileParams, authSettings); + IRestResponse response = (IRestResponse) await apiClient.CallApiAsync(path, Method.{{httpMethod}}, queryParams, postBody, headerParams, formParams, fileParams, pathParams, authSettings); if (((int)response.StatusCode) >= 400) { throw new ApiException ((int)response.StatusCode, "Error calling {{nickname}}: " + response.Content, response.Content); }