Repository metrics
- Stars
- (6,291 stars)
- PR merge metrics
- (No merged PRs in 30d)
Description
Hello,
First of all, thanks for your contribution by creating this awesome project. I am using NSwagStudio to generate C# Client class from a swagger spec, and I'm having issues with DateTime and Enum in ConvertToString method.
Here is the NSwagStudio generated code for the ConvertToString method (all request params are converted to string using this method):
private string ConvertToString(object value, System.Globalization.CultureInfo cultureInfo)
{
if (value is System.Enum)
{
string name = System.Enum.GetName(value.GetType(), value);
if (name != null)
{
var field = System.Reflection.IntrospectionExtensions.GetTypeInfo(value.GetType()).GetDeclaredField(name);
if (field != null)
{
var attribute = System.Reflection.CustomAttributeExtensions.GetCustomAttribute(field, typeof(System.Runtime.Serialization.EnumMemberAttribute))
as System.Runtime.Serialization.EnumMemberAttribute;
if (attribute != null)
{
return attribute.Value;
}
}
}
}
else if (value is byte[])
{
return System.Convert.ToBase64String((byte[]) value);
}
else if (value != null && value.GetType().IsArray)
{
var array = System.Linq.Enumerable.OfType<object>((System.Array) value);
return string.Join(",", System.Linq.Enumerable.Select(array, o => ConvertToString(o, cultureInfo)));
}
return System.Convert.ToString(value, cultureInfo);
}
-
For DateTime conversion, I am getting the string format of the current culture, in my case spanish, and that's causing an error on the API side because it isn't able to parse the value back to DateTime. It would be great if we can pass anywhere the DateTime format to convert to, before sending to the API.
-
For Enum conversion, I am always getting the ToString() value of the Enum name:
Swagger Spec fragment for Enum class:
{
"name": "TransferType",
"in": "formData",
"required": true,
"type": "integer",
"format": "int32",
"enum": [
0,
1
]
},
Original Enum class:
public enum TransferCombination
{
OneWay, RoundTrip
}
NSwagStudio Generated Enum class:
public enum TransferType
{
_0 = 0,
_1 = 1,
}
Here I would like to, first of all be able to get original names in the NSwagStudio Generated Enum class, and also be able to decide if send enum value or enum name to the API in the request.
Thank you Best regards David