forked from loafle/openapi-generator-original
fix deserialization of string and other primitive
This commit is contained in:
@@ -132,13 +132,13 @@ namespace {{packageName}}.Client {
|
||||
/// <summary>
|
||||
/// Deserialize the JSON string into a proper object
|
||||
/// </summary>
|
||||
/// <param name="json"> JSON string
|
||||
/// <param name="content"> HTTP body (e.g. string, JSON)
|
||||
/// <param name="type"> Object type
|
||||
/// <returns>Object representation of the JSON string</returns>
|
||||
public object Deserialize(string content, Type type, IList<Parameter> headers=null) {
|
||||
if (type.GetType() == typeof(Object)) {
|
||||
if (type.GetType() == typeof(Object)) { // return an object
|
||||
return (Object)content;
|
||||
} else if (type.Name == "FileStream") {
|
||||
} else if (type.Name == "FileStream") { // return a file
|
||||
// e.g. Content-Disposition: attachment; filename=checkimage.jpp
|
||||
String fileName;
|
||||
String filePath;
|
||||
@@ -158,9 +158,13 @@ namespace {{packageName}}.Client {
|
||||
}
|
||||
System.IO.File.WriteAllText (fileName, content);
|
||||
return File.Open (fileName, FileMode.Open);
|
||||
} else if (type.Name.StartsWith("System.Nullable`1[[System.DateTime")) { // return a datetime object
|
||||
return DateTime.Parse(content, null, System.Globalization.DateTimeStyles.RoundtripKind);
|
||||
} else if (type.Name == "String" || type.Name.StartsWith("System.Nullable")) { // return primitive
|
||||
return ConvertType(content, type);
|
||||
}
|
||||
|
||||
|
||||
// at this point, it must be a model (json)
|
||||
try
|
||||
{
|
||||
return JsonConvert.DeserializeObject(content, type);
|
||||
@@ -238,5 +242,15 @@ namespace {{packageName}}.Client {
|
||||
return System.Convert.ToBase64String(textByte);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Dynamically cast the object into target type
|
||||
/// Ref: http://stackoverflow.com/questions/4925718/c-dynamic-runtime-cast
|
||||
/// </summary>
|
||||
/// <param name="dynamic">Object to be casted</param>
|
||||
/// <param name="dest">Target type</param>
|
||||
public static dynamic ConvertType(dynamic source, Type dest) {
|
||||
return Convert.ChangeType(source, dest);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -165,7 +165,7 @@ namespace {{packageName}}.Api {
|
||||
if (((int)response.StatusCode) >= 400) {
|
||||
throw new ApiException ((int)response.StatusCode, "Error calling {{nickname}}: " + response.Content, response.Content);
|
||||
}
|
||||
{{#returnType}}return ({{{returnType}}}) ApiClient.Deserialize(response.Content, typeof({{{returnType}}}));{{/returnType}}{{^returnType}}
|
||||
{{#returnType}}return ({{{returnType}}}) ApiClient.Deserialize(response.Content, typeof({{{returnType}}}), response.Headers);{{/returnType}}{{^returnType}}
|
||||
return;{{/returnType}}
|
||||
}
|
||||
{{/operation}}
|
||||
|
||||
@@ -406,7 +406,7 @@ namespace IO.Swagger.Api {
|
||||
if (((int)response.StatusCode) >= 400) {
|
||||
throw new ApiException ((int)response.StatusCode, "Error calling FindPetsByStatus: " + response.Content, response.Content);
|
||||
}
|
||||
return (List<Pet>) ApiClient.Deserialize(response.Content, typeof(List<Pet>));
|
||||
return (List<Pet>) ApiClient.Deserialize(response.Content, typeof(List<Pet>), response.Headers);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -479,7 +479,7 @@ namespace IO.Swagger.Api {
|
||||
if (((int)response.StatusCode) >= 400) {
|
||||
throw new ApiException ((int)response.StatusCode, "Error calling FindPetsByTags: " + response.Content, response.Content);
|
||||
}
|
||||
return (List<Pet>) ApiClient.Deserialize(response.Content, typeof(List<Pet>));
|
||||
return (List<Pet>) ApiClient.Deserialize(response.Content, typeof(List<Pet>), response.Headers);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -558,7 +558,7 @@ namespace IO.Swagger.Api {
|
||||
if (((int)response.StatusCode) >= 400) {
|
||||
throw new ApiException ((int)response.StatusCode, "Error calling GetPetById: " + response.Content, response.Content);
|
||||
}
|
||||
return (Pet) ApiClient.Deserialize(response.Content, typeof(Pet));
|
||||
return (Pet) ApiClient.Deserialize(response.Content, typeof(Pet), response.Headers);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -184,7 +184,7 @@ namespace IO.Swagger.Api {
|
||||
if (((int)response.StatusCode) >= 400) {
|
||||
throw new ApiException ((int)response.StatusCode, "Error calling GetInventory: " + response.Content, response.Content);
|
||||
}
|
||||
return (Dictionary<String, int?>) ApiClient.Deserialize(response.Content, typeof(Dictionary<String, int?>));
|
||||
return (Dictionary<String, int?>) ApiClient.Deserialize(response.Content, typeof(Dictionary<String, int?>), response.Headers);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -257,7 +257,7 @@ namespace IO.Swagger.Api {
|
||||
if (((int)response.StatusCode) >= 400) {
|
||||
throw new ApiException ((int)response.StatusCode, "Error calling PlaceOrder: " + response.Content, response.Content);
|
||||
}
|
||||
return (Order) ApiClient.Deserialize(response.Content, typeof(Order));
|
||||
return (Order) ApiClient.Deserialize(response.Content, typeof(Order), response.Headers);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -336,7 +336,7 @@ namespace IO.Swagger.Api {
|
||||
if (((int)response.StatusCode) >= 400) {
|
||||
throw new ApiException ((int)response.StatusCode, "Error calling GetOrderById: " + response.Content, response.Content);
|
||||
}
|
||||
return (Order) ApiClient.Deserialize(response.Content, typeof(Order));
|
||||
return (Order) ApiClient.Deserialize(response.Content, typeof(Order), response.Headers);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -477,7 +477,7 @@ namespace IO.Swagger.Api {
|
||||
if (((int)response.StatusCode) >= 400) {
|
||||
throw new ApiException ((int)response.StatusCode, "Error calling LoginUser: " + response.Content, response.Content);
|
||||
}
|
||||
return (string) ApiClient.Deserialize(response.Content, typeof(string));
|
||||
return (string) ApiClient.Deserialize(response.Content, typeof(string), response.Headers);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -627,7 +627,7 @@ namespace IO.Swagger.Api {
|
||||
if (((int)response.StatusCode) >= 400) {
|
||||
throw new ApiException ((int)response.StatusCode, "Error calling GetUserByName: " + response.Content, response.Content);
|
||||
}
|
||||
return (User) ApiClient.Deserialize(response.Content, typeof(User));
|
||||
return (User) ApiClient.Deserialize(response.Content, typeof(User), response.Headers);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -132,13 +132,13 @@ namespace IO.Swagger.Client {
|
||||
/// <summary>
|
||||
/// Deserialize the JSON string into a proper object
|
||||
/// </summary>
|
||||
/// <param name="json"> JSON string
|
||||
/// <param name="content"> HTTP body (e.g. string, JSON)
|
||||
/// <param name="type"> Object type
|
||||
/// <returns>Object representation of the JSON string</returns>
|
||||
public object Deserialize(string content, Type type, IList<Parameter> headers=null) {
|
||||
if (type.GetType() == typeof(Object)) {
|
||||
if (type.GetType() == typeof(Object)) { // return an object
|
||||
return (Object)content;
|
||||
} else if (type.Name == "FileStream") {
|
||||
} else if (type.Name == "FileStream") { // return a file
|
||||
// e.g. Content-Disposition: attachment; filename=checkimage.jpp
|
||||
String fileName;
|
||||
String filePath;
|
||||
@@ -158,9 +158,13 @@ namespace IO.Swagger.Client {
|
||||
}
|
||||
System.IO.File.WriteAllText (fileName, content);
|
||||
return File.Open (fileName, FileMode.Open);
|
||||
} else if (type.Name.StartsWith("System.Nullable`1[[System.DateTime")) { // return a datetime object
|
||||
return DateTime.Parse(content, null, System.Globalization.DateTimeStyles.RoundtripKind);
|
||||
} else if (type.Name == "String" || type.Name.StartsWith("System.Nullable")) { // return primitive
|
||||
return ConvertType(content, type);
|
||||
}
|
||||
|
||||
|
||||
// at this point, it must be a model (json)
|
||||
try
|
||||
{
|
||||
return JsonConvert.DeserializeObject(content, type);
|
||||
@@ -243,5 +247,15 @@ namespace IO.Swagger.Client {
|
||||
return System.Convert.ToBase64String(textByte);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Dynamically cast the object into target type
|
||||
/// Ref: http://stackoverflow.com/questions/4925718/c-dynamic-runtime-cast
|
||||
/// </summary>
|
||||
/// <param name="dynamic">Object to be casted</param>
|
||||
/// <param name="dest">Target type</param>
|
||||
public static dynamic ConvertType(dynamic source, Type dest) {
|
||||
return Convert.ChangeType(source, dest);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,6 +39,7 @@
|
||||
<HintPath>Lib\SwaggerClient\bin\RestSharp.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System.Runtime.Serialization" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Lib\SwaggerClient\src\main\csharp\IO\Swagger\Api\PetApi.cs" />
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user