Skip to content

Commit 3c78e29

Browse files
committed
better Accept header for C#
1 parent 6cb4473 commit 3c78e29

File tree

12 files changed

+270
-16
lines changed

12 files changed

+270
-16
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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,16 @@ namespace {{packageName}}.Api
105105
var fileParams = new Dictionary<String, FileParameter>();
106106
String postBody = null;
107107

108+
// to determine the Accept header
109+
String[] http_header_accepts = new String[] {
110+
{{#produces}}"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/produces}}
111+
};
112+
String http_header_accept = ApiClient.SelectHeaderAccept(http_header_accepts);
113+
if (http_header_accept != null)
114+
headerParams.Add("Accept", ApiClient.SelectHeaderAccept(http_header_accepts));
115+
116+
// set "format" to json by default
117+
// e.g. /pet/{petId}.{format} becomes /pet/{petId}.json
108118
pathParams.Add("format", "json");
109119
{{#pathParams}}if ({{paramName}} != null) pathParams.Add("{{baseName}}", ApiClient.ParameterToString({{paramName}})); // path parameter
110120
{{/pathParams}}

samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Api/PetApi.cs

Lines changed: 92 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
namespace IO.Swagger.Api
99
{
1010

11+
/// <summary>
12+
/// Represents a collection of functions to interact with the API endpoints
13+
/// </summary>
1114
public interface IPetApi
1215
{
1316

@@ -43,28 +46,28 @@ public interface IPetApi
4346
/// Finds Pets by status Multiple status values can be provided with comma seperated strings
4447
/// </summary>
4548
/// <param name="status">Status values that need to be considered for filter</param>
46-
/// <returns>List<Pet></returns>
49+
/// <returns></returns>
4750
List<Pet> FindPetsByStatus (List<string> status);
4851

4952
/// <summary>
5053
/// Finds Pets by status Multiple status values can be provided with comma seperated strings
5154
/// </summary>
5255
/// <param name="status">Status values that need to be considered for filter</param>
53-
/// <returns>List<Pet></returns>
56+
/// <returns></returns>
5457
System.Threading.Tasks.Task<List<Pet>> FindPetsByStatusAsync (List<string> status);
5558

5659
/// <summary>
5760
/// Finds Pets by tags Muliple tags can be provided with comma seperated strings. Use tag1, tag2, tag3 for testing.
5861
/// </summary>
5962
/// <param name="tags">Tags to filter by</param>
60-
/// <returns>List<Pet></returns>
63+
/// <returns></returns>
6164
List<Pet> FindPetsByTags (List<string> tags);
6265

6366
/// <summary>
6467
/// Finds Pets by tags Muliple tags can be provided with comma seperated strings. Use tag1, tag2, tag3 for testing.
6568
/// </summary>
6669
/// <param name="tags">Tags to filter by</param>
67-
/// <returns>List<Pet></returns>
70+
/// <returns></returns>
6871
System.Threading.Tasks.Task<List<Pet>> FindPetsByTagsAsync (List<string> tags);
6972

7073
/// <summary>
@@ -184,7 +187,7 @@ public String GetBasePath()
184187
/// <summary>
185188
/// Gets or sets the API client.
186189
/// </summary>
187-
/// <value>An instance of the ApiClient</param>
190+
/// <value>An instance of the ApiClient</value>
188191
public ApiClient ApiClient {get; set;}
189192

190193

@@ -206,6 +209,16 @@ public void UpdatePet (Pet body)
206209
var fileParams = new Dictionary<String, FileParameter>();
207210
String postBody = null;
208211

212+
// to determine the Accept header
213+
String[] http_header_accepts = new String[] {
214+
"application/json", "application/xml"
215+
};
216+
String http_header_accept = ApiClient.SelectHeaderAccept(http_header_accepts);
217+
if (http_header_accept != null)
218+
headerParams.Add("Accept", ApiClient.SelectHeaderAccept(http_header_accepts));
219+
220+
// set "format" to json by default
221+
// e.g. /pet/{petId}.{format} becomes /pet/{petId}.json
209222
pathParams.Add("format", "json");
210223

211224

@@ -284,6 +297,16 @@ public void AddPet (Pet body)
284297
var fileParams = new Dictionary<String, FileParameter>();
285298
String postBody = null;
286299

300+
// to determine the Accept header
301+
String[] http_header_accepts = new String[] {
302+
"application/json", "application/xml"
303+
};
304+
String http_header_accept = ApiClient.SelectHeaderAccept(http_header_accepts);
305+
if (http_header_accept != null)
306+
headerParams.Add("Accept", ApiClient.SelectHeaderAccept(http_header_accepts));
307+
308+
// set "format" to json by default
309+
// e.g. /pet/{petId}.{format} becomes /pet/{petId}.json
287310
pathParams.Add("format", "json");
288311

289312

@@ -348,7 +371,7 @@ public async System.Threading.Tasks.Task AddPetAsync (Pet body)
348371
/// Finds Pets by status Multiple status values can be provided with comma seperated strings
349372
/// </summary>
350373
/// <param name="status">Status values that need to be considered for filter</param>
351-
/// <returns>List<Pet></returns>
374+
/// <returns></returns>
352375
public List<Pet> FindPetsByStatus (List<string> status)
353376
{
354377

@@ -362,6 +385,16 @@ public List<Pet> FindPetsByStatus (List<string> status)
362385
var fileParams = new Dictionary<String, FileParameter>();
363386
String postBody = null;
364387

388+
// to determine the Accept header
389+
String[] http_header_accepts = new String[] {
390+
"application/json", "application/xml"
391+
};
392+
String http_header_accept = ApiClient.SelectHeaderAccept(http_header_accepts);
393+
if (http_header_accept != null)
394+
headerParams.Add("Accept", ApiClient.SelectHeaderAccept(http_header_accepts));
395+
396+
// set "format" to json by default
397+
// e.g. /pet/{petId}.{format} becomes /pet/{petId}.json
365398
pathParams.Add("format", "json");
366399

367400
if (status != null) queryParams.Add("status", ApiClient.ParameterToString(status)); // query parameter
@@ -388,7 +421,7 @@ public List<Pet> FindPetsByStatus (List<string> status)
388421
/// Finds Pets by status Multiple status values can be provided with comma seperated strings
389422
/// </summary>
390423
/// <param name="status">Status values that need to be considered for filter</param>
391-
/// <returns>List<Pet></returns>
424+
/// <returns></returns>
392425
public async System.Threading.Tasks.Task<List<Pet>> FindPetsByStatusAsync (List<string> status)
393426
{
394427

@@ -425,7 +458,7 @@ public async System.Threading.Tasks.Task<List<Pet>> FindPetsByStatusAsync (List<
425458
/// Finds Pets by tags Muliple tags can be provided with comma seperated strings. Use tag1, tag2, tag3 for testing.
426459
/// </summary>
427460
/// <param name="tags">Tags to filter by</param>
428-
/// <returns>List<Pet></returns>
461+
/// <returns></returns>
429462
public List<Pet> FindPetsByTags (List<string> tags)
430463
{
431464

@@ -439,6 +472,16 @@ public List<Pet> FindPetsByTags (List<string> tags)
439472
var fileParams = new Dictionary<String, FileParameter>();
440473
String postBody = null;
441474

475+
// to determine the Accept header
476+
String[] http_header_accepts = new String[] {
477+
"application/json", "application/xml"
478+
};
479+
String http_header_accept = ApiClient.SelectHeaderAccept(http_header_accepts);
480+
if (http_header_accept != null)
481+
headerParams.Add("Accept", ApiClient.SelectHeaderAccept(http_header_accepts));
482+
483+
// set "format" to json by default
484+
// e.g. /pet/{petId}.{format} becomes /pet/{petId}.json
442485
pathParams.Add("format", "json");
443486

444487
if (tags != null) queryParams.Add("tags", ApiClient.ParameterToString(tags)); // query parameter
@@ -465,7 +508,7 @@ public List<Pet> FindPetsByTags (List<string> tags)
465508
/// Finds Pets by tags Muliple tags can be provided with comma seperated strings. Use tag1, tag2, tag3 for testing.
466509
/// </summary>
467510
/// <param name="tags">Tags to filter by</param>
468-
/// <returns>List<Pet></returns>
511+
/// <returns></returns>
469512
public async System.Threading.Tasks.Task<List<Pet>> FindPetsByTagsAsync (List<string> tags)
470513
{
471514

@@ -519,6 +562,16 @@ public Pet GetPetById (long? petId)
519562
var fileParams = new Dictionary<String, FileParameter>();
520563
String postBody = null;
521564

565+
// to determine the Accept header
566+
String[] http_header_accepts = new String[] {
567+
"application/json", "application/xml"
568+
};
569+
String http_header_accept = ApiClient.SelectHeaderAccept(http_header_accepts);
570+
if (http_header_accept != null)
571+
headerParams.Add("Accept", ApiClient.SelectHeaderAccept(http_header_accepts));
572+
573+
// set "format" to json by default
574+
// e.g. /pet/{petId}.{format} becomes /pet/{petId}.json
522575
pathParams.Add("format", "json");
523576
if (petId != null) pathParams.Add("petId", ApiClient.ParameterToString(petId)); // path parameter
524577

@@ -603,6 +656,16 @@ public void UpdatePetWithForm (string petId, string name, string status)
603656
var fileParams = new Dictionary<String, FileParameter>();
604657
String postBody = null;
605658

659+
// to determine the Accept header
660+
String[] http_header_accepts = new String[] {
661+
"application/json", "application/xml"
662+
};
663+
String http_header_accept = ApiClient.SelectHeaderAccept(http_header_accepts);
664+
if (http_header_accept != null)
665+
headerParams.Add("Accept", ApiClient.SelectHeaderAccept(http_header_accepts));
666+
667+
// set "format" to json by default
668+
// e.g. /pet/{petId}.{format} becomes /pet/{petId}.json
606669
pathParams.Add("format", "json");
607670
if (petId != null) pathParams.Add("petId", ApiClient.ParameterToString(petId)); // path parameter
608671

@@ -693,6 +756,16 @@ public void DeletePet (long? petId, string apiKey)
693756
var fileParams = new Dictionary<String, FileParameter>();
694757
String postBody = null;
695758

759+
// to determine the Accept header
760+
String[] http_header_accepts = new String[] {
761+
"application/json", "application/xml"
762+
};
763+
String http_header_accept = ApiClient.SelectHeaderAccept(http_header_accepts);
764+
if (http_header_accept != null)
765+
headerParams.Add("Accept", ApiClient.SelectHeaderAccept(http_header_accepts));
766+
767+
// set "format" to json by default
768+
// e.g. /pet/{petId}.{format} becomes /pet/{petId}.json
696769
pathParams.Add("format", "json");
697770
if (petId != null) pathParams.Add("petId", ApiClient.ParameterToString(petId)); // path parameter
698771

@@ -781,6 +854,16 @@ public void UploadFile (long? petId, string additionalMetadata, Stream file)
781854
var fileParams = new Dictionary<String, FileParameter>();
782855
String postBody = null;
783856

857+
// to determine the Accept header
858+
String[] http_header_accepts = new String[] {
859+
"application/json", "application/xml"
860+
};
861+
String http_header_accept = ApiClient.SelectHeaderAccept(http_header_accepts);
862+
if (http_header_accept != null)
863+
headerParams.Add("Accept", ApiClient.SelectHeaderAccept(http_header_accepts));
864+
865+
// set "format" to json by default
866+
// e.g. /pet/{petId}.{format} becomes /pet/{petId}.json
784867
pathParams.Add("format", "json");
785868
if (petId != null) pathParams.Add("petId", ApiClient.ParameterToString(petId)); // path parameter
786869

samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Api/StoreApi.cs

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,22 @@
88
namespace IO.Swagger.Api
99
{
1010

11+
/// <summary>
12+
/// Represents a collection of functions to interact with the API endpoints
13+
/// </summary>
1114
public interface IStoreApi
1215
{
1316

1417
/// <summary>
1518
/// Returns pet inventories by status Returns a map of status codes to quantities
1619
/// </summary>
17-
/// <returns>Dictionary<string, int?></returns>
20+
/// <returns></returns>
1821
Dictionary<string, int?> GetInventory ();
1922

2023
/// <summary>
2124
/// Returns pet inventories by status Returns a map of status codes to quantities
2225
/// </summary>
23-
/// <returns>Dictionary<string, int?></returns>
26+
/// <returns></returns>
2427
System.Threading.Tasks.Task<Dictionary<string, int?>> GetInventoryAsync ();
2528

2629
/// <summary>
@@ -116,14 +119,14 @@ public String GetBasePath()
116119
/// <summary>
117120
/// Gets or sets the API client.
118121
/// </summary>
119-
/// <value>An instance of the ApiClient</param>
122+
/// <value>An instance of the ApiClient</value>
120123
public ApiClient ApiClient {get; set;}
121124

122125

123126
/// <summary>
124127
/// Returns pet inventories by status Returns a map of status codes to quantities
125128
/// </summary>
126-
/// <returns>Dictionary<string, int?></returns>
129+
/// <returns></returns>
127130
public Dictionary<string, int?> GetInventory ()
128131
{
129132

@@ -137,6 +140,16 @@ public String GetBasePath()
137140
var fileParams = new Dictionary<String, FileParameter>();
138141
String postBody = null;
139142

143+
// to determine the Accept header
144+
String[] http_header_accepts = new String[] {
145+
"application/json", "application/xml"
146+
};
147+
String http_header_accept = ApiClient.SelectHeaderAccept(http_header_accepts);
148+
if (http_header_accept != null)
149+
headerParams.Add("Accept", ApiClient.SelectHeaderAccept(http_header_accepts));
150+
151+
// set "format" to json by default
152+
// e.g. /pet/{petId}.{format} becomes /pet/{petId}.json
140153
pathParams.Add("format", "json");
141154

142155

@@ -161,7 +174,7 @@ public String GetBasePath()
161174
/// <summary>
162175
/// Returns pet inventories by status Returns a map of status codes to quantities
163176
/// </summary>
164-
/// <returns>Dictionary<string, int?></returns>
177+
/// <returns></returns>
165178
public async System.Threading.Tasks.Task<Dictionary<string, int?>> GetInventoryAsync ()
166179
{
167180

@@ -211,6 +224,16 @@ public Order PlaceOrder (Order body)
211224
var fileParams = new Dictionary<String, FileParameter>();
212225
String postBody = null;
213226

227+
// to determine the Accept header
228+
String[] http_header_accepts = new String[] {
229+
"application/json", "application/xml"
230+
};
231+
String http_header_accept = ApiClient.SelectHeaderAccept(http_header_accepts);
232+
if (http_header_accept != null)
233+
headerParams.Add("Accept", ApiClient.SelectHeaderAccept(http_header_accepts));
234+
235+
// set "format" to json by default
236+
// e.g. /pet/{petId}.{format} becomes /pet/{petId}.json
214237
pathParams.Add("format", "json");
215238

216239

@@ -291,6 +314,16 @@ public Order GetOrderById (string orderId)
291314
var fileParams = new Dictionary<String, FileParameter>();
292315
String postBody = null;
293316

317+
// to determine the Accept header
318+
String[] http_header_accepts = new String[] {
319+
"application/json", "application/xml"
320+
};
321+
String http_header_accept = ApiClient.SelectHeaderAccept(http_header_accepts);
322+
if (http_header_accept != null)
323+
headerParams.Add("Accept", ApiClient.SelectHeaderAccept(http_header_accepts));
324+
325+
// set "format" to json by default
326+
// e.g. /pet/{petId}.{format} becomes /pet/{petId}.json
294327
pathParams.Add("format", "json");
295328
if (orderId != null) pathParams.Add("orderId", ApiClient.ParameterToString(orderId)); // path parameter
296329

@@ -373,6 +406,16 @@ public void DeleteOrder (string orderId)
373406
var fileParams = new Dictionary<String, FileParameter>();
374407
String postBody = null;
375408

409+
// to determine the Accept header
410+
String[] http_header_accepts = new String[] {
411+
"application/json", "application/xml"
412+
};
413+
String http_header_accept = ApiClient.SelectHeaderAccept(http_header_accepts);
414+
if (http_header_accept != null)
415+
headerParams.Add("Accept", ApiClient.SelectHeaderAccept(http_header_accepts));
416+
417+
// set "format" to json by default
418+
// e.g. /pet/{petId}.{format} becomes /pet/{petId}.json
376419
pathParams.Add("format", "json");
377420
if (orderId != null) pathParams.Add("orderId", ApiClient.ParameterToString(orderId)); // path parameter
378421

0 commit comments

Comments
 (0)