Skip to content

Commit

Permalink
ContentType selection fix for csharp. (#6633)
Browse files Browse the repository at this point in the history
* ContentType selection fix for csharp.
Updated to reflect java implementation. Previously any request body of type string was having the content type overridden to 'application/json'.  This prevented custom json ContentTypes

* updated the petshop codegen for C#

* Fixed content type selection test for csharp

* Replaced tabs with 4 spaces

* Removed trailing space / string comparison
  • Loading branch information
alex-fisher authored and wing328 committed Oct 17, 2017
1 parent be3e33f commit ff9c723
Show file tree
Hide file tree
Showing 10 changed files with 1,018 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,24 +165,12 @@ namespace {{packageName}}.Client

if (postBody != null) // http body (model or byte[]) parameter
{
if (postBody.GetType() == typeof(String))
{
{{#netStandard}}
request.AddParameter(new Parameter { Value = postBody, Type = ParameterType.RequestBody, ContentType = "application/json" });
{{/netStandard}}
{{^netStandard}}
request.AddParameter("application/json", postBody, ParameterType.RequestBody);
{{/netStandard}}
}
else if (postBody.GetType() == typeof(byte[]))
{
{{#netStandard}}
request.AddParameter(new Parameter { Value = postBody, Type = ParameterType.RequestBody, ContentType = contentType });
{{/netStandard}}
{{^netStandard}}
request.AddParameter(contentType, postBody, ParameterType.RequestBody);
{{/netStandard}}
}
{{#netStandard}}
request.AddParameter(new Parameter { Value = postBody, Type = ParameterType.RequestBody, ContentType = contentType });
{{/netStandard}}
{{^netStandard}}
request.AddParameter(contentType, postBody, ParameterType.RequestBody);
{{/netStandard}}
}

return request;
Expand Down Expand Up @@ -399,21 +387,40 @@ namespace {{packageName}}.Client
}
}

/// <summary>
///Check if the given MIME is a JSON MIME.
///JSON MIME examples:
/// application/json
/// application/json; charset=UTF8
/// APPLICATION/JSON
/// application/vnd.company+json
/// </summary>
/// <param name="mime">MIME</param>
/// <returns>Returns True if MIME type is json.</returns>
public bool IsJsonMime(String mime)
{
var jsonRegex = new Regex("(?i)^(application/json|[^;/ \t]+/[^;/ \t]+[+]json)[ \t]*(;.*)?$");
return mime != null && (jsonRegex.IsMatch(mime) || mime.Equals("application/json-patch+json"));
}

/// <summary>
/// Select the Content-Type header's value from the given content-type array:
/// if JSON exists in the given array, use it;
/// if JSON type exists in the given array, use it;
/// otherwise use the first one defined in 'consumes'
/// </summary>
/// <param name="contentTypes">The Content-Type array to select from.</param>
/// <returns>The Content-Type header to use.</returns>
public String SelectHeaderContentType(String[] contentTypes)
{
if (contentTypes.Length == 0)
return null;
if (contentTypes.Contains("application/json", StringComparer.OrdinalIgnoreCase))
return "application/json";
foreach (var contentType in contentTypes)
{
if (IsJsonMime(contentType.ToLower()))
return contentType;
}

return contentTypes[0]; // use the first content type specified in 'consumes'
}

Expand Down
12 changes: 7 additions & 5 deletions samples/client/petstore/csharp/SwaggerClient/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,18 @@ namespace Example
public void main()
{

var apiInstance = new FakeApi();
var body = new OuterBoolean(); // OuterBoolean | Input boolean as post body (optional)
var apiInstance = new AnotherFakeApi();
var body = new ModelClient(); // ModelClient | client model
try
{
OuterBoolean result = apiInstance.FakeOuterBooleanSerialize(body);
// To test special tags
ModelClient result = apiInstance.TestSpecialTags(body);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling FakeApi.FakeOuterBooleanSerialize: " + e.Message );
Debug.Print("Exception when calling AnotherFakeApi.TestSpecialTags: " + e.Message );
}

}
Expand All @@ -93,6 +94,7 @@ All URIs are relative to *http://petstore.swagger.io:80/v2*

Class | Method | HTTP request | Description
------------ | ------------- | ------------- | -------------
*AnotherFakeApi* | [**TestSpecialTags**](docs/AnotherFakeApi.md#testspecialtags) | **PATCH** /another-fake/dummy | To test special tags
*FakeApi* | [**FakeOuterBooleanSerialize**](docs/FakeApi.md#fakeouterbooleanserialize) | **POST** /fake/outer/boolean |
*FakeApi* | [**FakeOuterCompositeSerialize**](docs/FakeApi.md#fakeoutercompositeserialize) | **POST** /fake/outer/composite |
*FakeApi* | [**FakeOuterNumberSerialize**](docs/FakeApi.md#fakeouternumberserialize) | **POST** /fake/outer/number |
Expand All @@ -101,7 +103,7 @@ Class | Method | HTTP request | Description
*FakeApi* | [**TestEndpointParameters**](docs/FakeApi.md#testendpointparameters) | **POST** /fake | Fake endpoint for testing various parameters 假端點 偽のエンドポイント 가짜 엔드 포인트
*FakeApi* | [**TestEnumParameters**](docs/FakeApi.md#testenumparameters) | **GET** /fake | To test enum parameters
*FakeApi* | [**TestJsonFormData**](docs/FakeApi.md#testjsonformdata) | **GET** /fake/jsonFormData | test json serialization of form data
*Fake_classname_tags123Api* | [**TestClassname**](docs/Fake_classname_tags123Api.md#testclassname) | **PATCH** /fake_classname_test | To test class name in snake case
*FakeClassnameTags123Api* | [**TestClassname**](docs/FakeClassnameTags123Api.md#testclassname) | **PATCH** /fake_classname_test | To test class name in snake case
*PetApi* | [**AddPet**](docs/PetApi.md#addpet) | **POST** /pet | Add a new pet to the store
*PetApi* | [**DeletePet**](docs/PetApi.md#deletepet) | **DELETE** /pet/{petId} | Deletes a pet
*PetApi* | [**FindPetsByStatus**](docs/PetApi.md#findpetsbystatus) | **GET** /pet/findByStatus | Finds Pets by status
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# IO.Swagger.Api.AnotherFakeApi

All URIs are relative to *http://petstore.swagger.io:80/v2*

Method | HTTP request | Description
------------- | ------------- | -------------
[**TestSpecialTags**](AnotherFakeApi.md#testspecialtags) | **PATCH** /another-fake/dummy | To test special tags


<a name="testspecialtags"></a>
# **TestSpecialTags**
> ModelClient TestSpecialTags (ModelClient body)
To test special tags

To test special tags

### Example
```csharp
using System;
using System.Diagnostics;
using IO.Swagger.Api;
using IO.Swagger.Client;
using IO.Swagger.Model;

namespace Example
{
public class TestSpecialTagsExample
{
public void main()
{
var apiInstance = new AnotherFakeApi();
var body = new ModelClient(); // ModelClient | client model
try
{
// To test special tags
ModelClient result = apiInstance.TestSpecialTags(body);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling AnotherFakeApi.TestSpecialTags: " + e.Message );
}
}
}
}
```

### Parameters

Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**body** | [**ModelClient**](ModelClient.md)| client model |

### Return type

[**ModelClient**](ModelClient.md)

### Authorization

No authorization required

### HTTP request headers

- **Content-Type**: application/json
- **Accept**: application/json

[[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md)

Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# IO.Swagger.Api.FakeClassnameTags123Api

All URIs are relative to *http://petstore.swagger.io:80/v2*

Method | HTTP request | Description
------------- | ------------- | -------------
[**TestClassname**](FakeClassnameTags123Api.md#testclassname) | **PATCH** /fake_classname_test | To test class name in snake case


<a name="testclassname"></a>
# **TestClassname**
> ModelClient TestClassname (ModelClient body)
To test class name in snake case

### Example
```csharp
using System;
using System.Diagnostics;
using IO.Swagger.Api;
using IO.Swagger.Client;
using IO.Swagger.Model;

namespace Example
{
public class TestClassnameExample
{
public void main()
{
// Configure API key authorization: api_key_query
Configuration.Default.AddApiKey("api_key_query", "YOUR_API_KEY");
// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
// Configuration.Default.AddApiKeyPrefix("api_key_query", "Bearer");
var apiInstance = new FakeClassnameTags123Api();
var body = new ModelClient(); // ModelClient | client model
try
{
// To test class name in snake case
ModelClient result = apiInstance.TestClassname(body);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling FakeClassnameTags123Api.TestClassname: " + e.Message );
}
}
}
}
```

### Parameters

Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**body** | [**ModelClient**](ModelClient.md)| client model |

### Return type

[**ModelClient**](ModelClient.md)

### Authorization

[api_key_query](../README.md#api_key_query)

### HTTP request headers

- **Content-Type**: application/json
- **Accept**: application/json

[[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md)

Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Swagger Petstore
*
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
*
* OpenAPI spec version: 1.0.0
* Contact: apiteam@swagger.io
* Generated by: https://github.com/swagger-api/swagger-codegen.git
*/

using System;
using System.IO;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using RestSharp;
using NUnit.Framework;

using IO.Swagger.Client;
using IO.Swagger.Api;
using IO.Swagger.Model;

namespace IO.Swagger.Test
{
/// <summary>
/// Class for testing AnotherFakeApi
/// </summary>
/// <remarks>
/// This file is automatically generated by Swagger Codegen.
/// Please update the test case below to test the API endpoint.
/// </remarks>
[TestFixture]
public class AnotherFakeApiTests
{
private AnotherFakeApi instance;

/// <summary>
/// Setup before each unit test
/// </summary>
[SetUp]
public void Init()
{
instance = new AnotherFakeApi();
}

/// <summary>
/// Clean up after each unit test
/// </summary>
[TearDown]
public void Cleanup()
{

}

/// <summary>
/// Test an instance of AnotherFakeApi
/// </summary>
[Test]
public void InstanceTest()
{
// TODO uncomment below to test 'IsInstanceOfType' AnotherFakeApi
//Assert.IsInstanceOfType(typeof(AnotherFakeApi), instance, "instance is a AnotherFakeApi");
}


/// <summary>
/// Test TestSpecialTags
/// </summary>
[Test]
public void TestSpecialTagsTest()
{
// TODO uncomment below to test the method and replace null with proper value
//ModelClient body = null;
//var response = instance.TestSpecialTags(body);
//Assert.IsInstanceOf<ModelClient> (response, "response is ModelClient");
}

}

}
Loading

0 comments on commit ff9c723

Please sign in to comment.