add type object support to C#

This commit is contained in:
wing328 2016-03-05 16:29:25 +08:00
parent 123bf19b97
commit d2baf9cbd7
6 changed files with 164 additions and 32 deletions

View File

@ -241,16 +241,10 @@ namespace {{packageName}}.Client
/// <returns>Object representation of the JSON string.</returns> /// <returns>Object representation of the JSON string.</returns>
public object Deserialize(IRestResponse response, Type type) public object Deserialize(IRestResponse response, Type type)
{ {
byte[] data = response.RawBytes;
string content = response.Content;
IList<Parameter> headers = response.Headers; IList<Parameter> headers = response.Headers;
if (type == typeof(Object)) // return an object if (type == typeof(byte[])) // return byte array
{ {
return content; return response.RawBytes;
}
else if (type == typeof(byte[])) // return byte array
{
return data;
} }
if (type == typeof(Stream)) if (type == typeof(Stream))
@ -267,29 +261,29 @@ namespace {{packageName}}.Client
if (match.Success) if (match.Success)
{ {
string fileName = filePath + SanitizeFilename(match.Groups[1].Value.Replace("\"", "").Replace("'", "")); string fileName = filePath + SanitizeFilename(match.Groups[1].Value.Replace("\"", "").Replace("'", ""));
File.WriteAllBytes(fileName, data); File.WriteAllBytes(fileName, response.RawBytes);
return new FileStream(fileName, FileMode.Open); return new FileStream(fileName, FileMode.Open);
} }
} }
} }
var stream = new MemoryStream(data); var stream = new MemoryStream(response.RawBytes);
return stream; return stream;
} }
if (type.Name.StartsWith("System.Nullable`1[[System.DateTime")) // return a datetime object if (type.Name.StartsWith("System.Nullable`1[[System.DateTime")) // return a datetime object
{ {
return DateTime.Parse(content, null, System.Globalization.DateTimeStyles.RoundtripKind); return DateTime.Parse(response.Content, null, System.Globalization.DateTimeStyles.RoundtripKind);
} }
if (type == typeof(String) || type.Name.StartsWith("System.Nullable")) // return primitive type if (type == typeof(String) || type.Name.StartsWith("System.Nullable")) // return primitive type
{ {
return ConvertType(content, type); return ConvertType(response.Content, type);
} }
// at this point, it must be a model (json) // at this point, it must be a model (json)
try try
{ {
return JsonConvert.DeserializeObject(content, type); return JsonConvert.DeserializeObject(response.Content, type);
} }
catch (Exception e) catch (Exception e)
{ {

View File

@ -241,16 +241,10 @@ namespace IO.Swagger.Client
/// <returns>Object representation of the JSON string.</returns> /// <returns>Object representation of the JSON string.</returns>
public object Deserialize(IRestResponse response, Type type) public object Deserialize(IRestResponse response, Type type)
{ {
byte[] data = response.RawBytes;
string content = response.Content;
IList<Parameter> headers = response.Headers; IList<Parameter> headers = response.Headers;
if (type == typeof(Object)) // return an object if (type == typeof(byte[])) // return byte array
{ {
return content; return response.RawBytes;
}
else if (type == typeof(byte[])) // return byte array
{
return data;
} }
if (type == typeof(Stream)) if (type == typeof(Stream))
@ -267,29 +261,29 @@ namespace IO.Swagger.Client
if (match.Success) if (match.Success)
{ {
string fileName = filePath + SanitizeFilename(match.Groups[1].Value.Replace("\"", "").Replace("'", "")); string fileName = filePath + SanitizeFilename(match.Groups[1].Value.Replace("\"", "").Replace("'", ""));
File.WriteAllBytes(fileName, data); File.WriteAllBytes(fileName, response.RawBytes);
return new FileStream(fileName, FileMode.Open); return new FileStream(fileName, FileMode.Open);
} }
} }
} }
var stream = new MemoryStream(data); var stream = new MemoryStream(response.RawBytes);
return stream; return stream;
} }
if (type.Name.StartsWith("System.Nullable`1[[System.DateTime")) // return a datetime object if (type.Name.StartsWith("System.Nullable`1[[System.DateTime")) // return a datetime object
{ {
return DateTime.Parse(content, null, System.Globalization.DateTimeStyles.RoundtripKind); return DateTime.Parse(response.Content, null, System.Globalization.DateTimeStyles.RoundtripKind);
} }
if (type == typeof(String) || type.Name.StartsWith("System.Nullable")) // return primitive type if (type == typeof(String) || type.Name.StartsWith("System.Nullable")) // return primitive type
{ {
return ConvertType(content, type); return ConvertType(response.Content, type);
} }
// at this point, it must be a model (json) // at this point, it must be a model (json)
try try
{ {
return JsonConvert.DeserializeObject(content, type); return JsonConvert.DeserializeObject(response.Content, type);
} }
catch (Exception e) catch (Exception e)
{ {

View File

@ -18,15 +18,38 @@ namespace IO.Swagger.Model
public partial class InlineResponse200 : IEquatable<InlineResponse200> public partial class InlineResponse200 : IEquatable<InlineResponse200>
{ {
[JsonConverter(typeof(StringEnumConverter))]
public enum StatusEnum {
[EnumMember(Value = "available")]
Available,
[EnumMember(Value = "pending")]
Pending,
[EnumMember(Value = "sold")]
Sold
}
/// <summary>
/// pet status in the store
/// </summary>
/// <value>pet status in the store</value>
[DataMember(Name="status", EmitDefaultValue=false)]
public StatusEnum? Status { get; set; }
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="InlineResponse200" /> class. /// Initializes a new instance of the <see cref="InlineResponse200" /> class.
/// Initializes a new instance of the <see cref="InlineResponse200" />class. /// Initializes a new instance of the <see cref="InlineResponse200" />class.
/// </summary> /// </summary>
/// <param name="Tags">Tags.</param>
/// <param name="Id">Id (required).</param> /// <param name="Id">Id (required).</param>
/// <param name="Category">Category.</param> /// <param name="Category">Category.</param>
/// <param name="Status">pet status in the store.</param>
/// <param name="Name">Name.</param> /// <param name="Name">Name.</param>
/// <param name="PhotoUrls">PhotoUrls.</param>
public InlineResponse200(long? Id = null, Object Category = null, string Name = null) public InlineResponse200(List<Tag> Tags = null, long? Id = null, Object Category = null, StatusEnum? Status = null, string Name = null, List<string> PhotoUrls = null)
{ {
// to ensure "Id" is required (not null) // to ensure "Id" is required (not null)
if (Id == null) if (Id == null)
@ -37,12 +60,21 @@ namespace IO.Swagger.Model
{ {
this.Id = Id; this.Id = Id;
} }
this.Tags = Tags;
this.Category = Category; this.Category = Category;
this.Status = Status;
this.Name = Name; this.Name = Name;
this.PhotoUrls = PhotoUrls;
} }
/// <summary>
/// Gets or Sets Tags
/// </summary>
[DataMember(Name="tags", EmitDefaultValue=false)]
public List<Tag> Tags { get; set; }
/// <summary> /// <summary>
/// Gets or Sets Id /// Gets or Sets Id
/// </summary> /// </summary>
@ -61,6 +93,12 @@ namespace IO.Swagger.Model
[DataMember(Name="name", EmitDefaultValue=false)] [DataMember(Name="name", EmitDefaultValue=false)]
public string Name { get; set; } public string Name { get; set; }
/// <summary>
/// Gets or Sets PhotoUrls
/// </summary>
[DataMember(Name="photoUrls", EmitDefaultValue=false)]
public List<string> PhotoUrls { get; set; }
/// <summary> /// <summary>
/// Returns the string presentation of the object /// Returns the string presentation of the object
/// </summary> /// </summary>
@ -69,9 +107,12 @@ namespace IO.Swagger.Model
{ {
var sb = new StringBuilder(); var sb = new StringBuilder();
sb.Append("class InlineResponse200 {\n"); sb.Append("class InlineResponse200 {\n");
sb.Append(" Tags: ").Append(Tags).Append("\n");
sb.Append(" Id: ").Append(Id).Append("\n"); sb.Append(" Id: ").Append(Id).Append("\n");
sb.Append(" Category: ").Append(Category).Append("\n"); sb.Append(" Category: ").Append(Category).Append("\n");
sb.Append(" Status: ").Append(Status).Append("\n");
sb.Append(" Name: ").Append(Name).Append("\n"); sb.Append(" Name: ").Append(Name).Append("\n");
sb.Append(" PhotoUrls: ").Append(PhotoUrls).Append("\n");
sb.Append("}\n"); sb.Append("}\n");
return sb.ToString(); return sb.ToString();
@ -109,6 +150,11 @@ namespace IO.Swagger.Model
return false; return false;
return return
(
this.Tags == other.Tags ||
this.Tags != null &&
this.Tags.SequenceEqual(other.Tags)
) &&
( (
this.Id == other.Id || this.Id == other.Id ||
this.Id != null && this.Id != null &&
@ -119,10 +165,20 @@ namespace IO.Swagger.Model
this.Category != null && this.Category != null &&
this.Category.Equals(other.Category) this.Category.Equals(other.Category)
) && ) &&
(
this.Status == other.Status ||
this.Status != null &&
this.Status.Equals(other.Status)
) &&
( (
this.Name == other.Name || this.Name == other.Name ||
this.Name != null && this.Name != null &&
this.Name.Equals(other.Name) this.Name.Equals(other.Name)
) &&
(
this.PhotoUrls == other.PhotoUrls ||
this.PhotoUrls != null &&
this.PhotoUrls.SequenceEqual(other.PhotoUrls)
); );
} }
@ -138,15 +194,24 @@ namespace IO.Swagger.Model
int hash = 41; int hash = 41;
// Suitable nullity checks etc, of course :) // Suitable nullity checks etc, of course :)
if (this.Tags != null)
hash = hash * 59 + this.Tags.GetHashCode();
if (this.Id != null) if (this.Id != null)
hash = hash * 59 + this.Id.GetHashCode(); hash = hash * 59 + this.Id.GetHashCode();
if (this.Category != null) if (this.Category != null)
hash = hash * 59 + this.Category.GetHashCode(); hash = hash * 59 + this.Category.GetHashCode();
if (this.Status != null)
hash = hash * 59 + this.Status.GetHashCode();
if (this.Name != null) if (this.Name != null)
hash = hash * 59 + this.Name.GetHashCode(); hash = hash * 59 + this.Name.GetHashCode();
if (this.PhotoUrls != null)
hash = hash * 59 + this.PhotoUrls.GetHashCode();
return hash; return hash;
} }
} }

View File

@ -1,11 +1,17 @@
<Properties StartupItem="SwaggerClientTest.csproj"> <Properties StartupItem="SwaggerClientTest.csproj">
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" /> <MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" />
<MonoDevelop.Ide.Workbench ActiveDocument="Lib/SwaggerClient/src/main/csharp/IO/Swagger/Api/PetApi.cs"> <MonoDevelop.Ide.Workbench ActiveDocument="TestPet.cs">
<Files> <Files>
<File FileName="TestPet.cs" Line="1" Column="1" /> <File FileName="TestPet.cs" Line="189" Column="4" />
<File FileName="TestOrder.cs" Line="1" Column="1" /> <File FileName="TestOrder.cs" Line="88" Column="47" />
<File FileName="Lib/SwaggerClient/src/main/csharp/IO/Swagger/Api/PetApi.cs" Line="18" Column="9" />
</Files> </Files>
<Pads>
<Pad Id="MonoDevelop.NUnit.TestPad">
<State name="__root__">
<Node name="SwaggerClientTest" expanded="True" />
</State>
</Pad>
</Pads>
</MonoDevelop.Ide.Workbench> </MonoDevelop.Ide.Workbench>
<MonoDevelop.Ide.DebuggingService.Breakpoints> <MonoDevelop.Ide.DebuggingService.Breakpoints>
<BreakpointStore /> <BreakpointStore />

View File

@ -10,7 +10,7 @@ using System.Reflection;
using Newtonsoft.Json; using Newtonsoft.Json;
namespace SwaggerClientTest.TestORder namespace SwaggerClientTest.TestOrder
{ {
[TestFixture ()] [TestFixture ()]
public class TestOrder public class TestOrder
@ -53,6 +53,48 @@ namespace SwaggerClientTest.TestORder
Assert.AreEqual (true, o.Complete); Assert.AreEqual (true, o.Complete);
} }
/// <summary>
/// Test GetInvetory
/// </summary>
[Test ()]
public void TestGetInventory ()
{
// set timeout to 10 seconds
Configuration c1 = new Configuration (timeout: 10000);
StoreApi storeApi = new StoreApi (c1);
Dictionary<String, int?> response = storeApi.GetInventory ();
foreach(KeyValuePair<string, int?> entry in response)
{
Assert.IsInstanceOf (typeof(int?), entry.Value);
}
}
/// <summary>
/// Test GetInvetoryInObject
/// </summary>
[Test ()]
public void TestGetInventoryInObject ()
{
// set timeout to 10 seconds
Configuration c1 = new Configuration (timeout: 10000);
StoreApi storeApi = new StoreApi (c1);
Newtonsoft.Json.Linq.JObject response = (Newtonsoft.Json.Linq.JObject)storeApi.GetInventoryInObject ();
// should be a Newtonsoft.Json.Linq.JObject since type is object
Assert.IsInstanceOf (typeof(Newtonsoft.Json.Linq.JObject), response);
foreach(KeyValuePair<string, string> entry in response.ToObject<Dictionary<string, string>>())
{
Assert.IsInstanceOf (typeof(int?), Int32.Parse(entry.Value));
}
}
} }
} }

View File

@ -158,6 +158,37 @@ namespace SwaggerClientTest.TestPet
} }
/// <summary>
/// Test GetPetByIdInObject
/// </summary>
[Test ()]
public void TestGetPetByIdInObject ()
{
// set timeout to 10 seconds
Configuration c1 = new Configuration (timeout: 10000);
PetApi petApi = new PetApi (c1);
InlineResponse200 response = petApi.GetPetByIdInObject (petId);
Assert.IsInstanceOf<InlineResponse200> (response, "Response is a Pet");
Assert.AreEqual ("Csharp test", response.Name);
Assert.AreEqual (InlineResponse200.StatusEnum.Available, response.Status);
Assert.IsInstanceOf<List<Tag>> (response.Tags, "Response.Tags is a Array");
Assert.AreEqual (petId, response.Tags [0].Id);
Assert.AreEqual ("csharp sample tag name1", response.Tags [0].Name);
Assert.IsInstanceOf<List<String>> (response.PhotoUrls, "Response.PhotoUrls is a Array");
Assert.AreEqual ("sample photoUrls", response.PhotoUrls [0]);
Assert.IsInstanceOf<Newtonsoft.Json.Linq.JObject> (response.Category, "Response.Category is a Newtonsoft.Json.Linq.JObject");
Newtonsoft.Json.Linq.JObject category = (Newtonsoft.Json.Linq.JObject)response.Category;
Assert.AreEqual (56, (int)category ["id"]);
Assert.AreEqual ("sample category name2", (string) category["name"]);
}
/// <summary> /// <summary>
/// Test GetPetByIdWithByteArray /// Test GetPetByIdWithByteArray
/// </summary> /// </summary>