Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[C#] ContentType selection fix #6633

Merged
merged 5 commits into from
Oct 17, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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[]))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check previously existed to support file inputs. Can you verify your change works when posting files?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jimschubert I implemented the 'UploadFile' test for the generated Petstore csharp api solution. The test passed and the api response was a 200. Let me know if there is any further testing you would like me to do.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alex-fisher I don't see that in this PR. Is it on another branch?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was done in the tests generated by the sdk and not in the actual testing suite. Would you like me to add a permanent test for this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it repeatable? If you share it here as a snippet, we can evaluate and either add it to this pr or to a later pr. It would be ideal as a permanent test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any further investigation I can do on my part for this request?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me add back the tests shortly. Stay tuned.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests added back #6663

Please rebase on the latest master to add the tests to this PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for taking that down. I've had a rough week and not much time after work.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worry :)

{
{{#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", StringComparison.InvariantCultureIgnoreCase));
}

/// <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))
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