Skip to content

Commit 228e039

Browse files
committed
Merge pull request #1333 from wing328/csharp_fix_accept
[C#] better Accept header selection for C# API client
2 parents b27663e + 7b79abe commit 228e039

File tree

12 files changed

+669
-79
lines changed

12 files changed

+669
-79
lines changed

modules/swagger-codegen/src/main/resources/csharp/ApiClient.mustache

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,21 @@ namespace {{packageName}}.Client
326326
}
327327
}
328328
}
329+
330+
/// <summary>
331+
/// Select the Accept header's value from the given accepts array:
332+
/// if JSON exists in the given array, use it;
333+
/// otherwise use all of them (joining into a string)
334+
/// </summary>
335+
/// <param name="accepts">The accepts array to select from.</param>
336+
/// <returns>The Accept header to use.</returns>
337+
public String SelectHeaderAccept(String[] accepts) {
338+
if (accepts.Length == 0)
339+
return null;
340+
if (accepts.Contains("application/json", StringComparer.OrdinalIgnoreCase))
341+
return "application/json";
342+
return String.Join(",", accepts);
343+
}
329344

330345
/// <summary>
331346
/// Encode string in base64 format.

modules/swagger-codegen/src/main/resources/csharp/api.mustache

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,21 @@ namespace {{packageName}}.Api
1616
{
1717
{{#operation}}
1818
/// <summary>
19-
/// {{summary}} {{notes}}
19+
/// {{summary}}
2020
/// </summary>
21+
/// <remarks>
22+
/// {{notes}}
23+
/// </remarks>
2124
{{#allParams}}/// <param name="{{paramName}}">{{description}}</param>
2225
{{/allParams}}/// <returns>{{#returnType}}{{^returnContainer}}{{{returnType}}}{{/returnContainer}}{{/returnType}}</returns>
2326
{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}} {{nickname}} ({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});
2427

2528
/// <summary>
26-
/// {{summary}} {{notes}}
29+
/// {{summary}}
2730
/// </summary>
31+
/// <remarks>
32+
/// {{notes}}
33+
/// </remarks>
2834
{{#allParams}}/// <param name="{{paramName}}">{{description}}</param>
2935
{{/allParams}}/// <returns>{{#returnType}}{{^returnContainer}}{{{returnType}}}{{/returnContainer}}{{/returnType}}</returns>
3036
{{#returnType}}System.Threading.Tasks.Task<{{{returnType}}}>{{/returnType}}{{^returnType}}System.Threading.Tasks.Task{{/returnType}} {{nickname}}Async ({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});
@@ -105,6 +111,16 @@ namespace {{packageName}}.Api
105111
var fileParams = new Dictionary<String, FileParameter>();
106112
String postBody = null;
107113

114+
// to determine the Accept header
115+
String[] http_header_accepts = new String[] {
116+
{{#produces}}"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/produces}}
117+
};
118+
String http_header_accept = ApiClient.SelectHeaderAccept(http_header_accepts);
119+
if (http_header_accept != null)
120+
headerParams.Add("Accept", ApiClient.SelectHeaderAccept(http_header_accepts));
121+
122+
// set "format" to json by default
123+
// e.g. /pet/{petId}.{format} becomes /pet/{petId}.json
108124
pathParams.Add("format", "json");
109125
{{#pathParams}}if ({{paramName}} != null) pathParams.Add("{{baseName}}", ApiClient.ParameterToString({{paramName}})); // path parameter
110126
{{/pathParams}}
@@ -150,7 +166,17 @@ namespace {{packageName}}.Api
150166
var formParams = new Dictionary<String, String>();
151167
var fileParams = new Dictionary<String, FileParameter>();
152168
String postBody = null;
153-
169+
170+
// to determine the Accept header
171+
String[] http_header_accepts = new String[] {
172+
{{#produces}}"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/produces}}
173+
};
174+
String http_header_accept = ApiClient.SelectHeaderAccept(http_header_accepts);
175+
if (http_header_accept != null)
176+
headerParams.Add("Accept", ApiClient.SelectHeaderAccept(http_header_accepts));
177+
178+
// set "format" to json by default
179+
// e.g. /pet/{petId}.{format} becomes /pet/{petId}.json
154180
pathParams.Add("format", "json");
155181
{{#pathParams}}if ({{paramName}} != null) pathParams.Add("{{baseName}}", ApiClient.ParameterToString({{paramName}})); // path parameter
156182
{{/pathParams}}

0 commit comments

Comments
 (0)