Skip to content

Commit a4ec9b9

Browse files
committed
Merge pull request #2317 from wing328/csharp_object_type
[C#] add type object support to C# API client
2 parents 123bf19 + d2baf9c commit a4ec9b9

File tree

6 files changed

+164
-32
lines changed

6 files changed

+164
-32
lines changed

modules/swagger-codegen/src/main/resources/csharp/ApiClient.mustache

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -241,16 +241,10 @@ namespace {{packageName}}.Client
241241
/// <returns>Object representation of the JSON string.</returns>
242242
public object Deserialize(IRestResponse response, Type type)
243243
{
244-
byte[] data = response.RawBytes;
245-
string content = response.Content;
246244
IList<Parameter> headers = response.Headers;
247-
if (type == typeof(Object)) // return an object
245+
if (type == typeof(byte[])) // return byte array
248246
{
249-
return content;
250-
}
251-
else if (type == typeof(byte[])) // return byte array
252-
{
253-
return data;
247+
return response.RawBytes;
254248
}
255249

256250
if (type == typeof(Stream))
@@ -267,29 +261,29 @@ namespace {{packageName}}.Client
267261
if (match.Success)
268262
{
269263
string fileName = filePath + SanitizeFilename(match.Groups[1].Value.Replace("\"", "").Replace("'", ""));
270-
File.WriteAllBytes(fileName, data);
264+
File.WriteAllBytes(fileName, response.RawBytes);
271265
return new FileStream(fileName, FileMode.Open);
272266
}
273267
}
274268
}
275-
var stream = new MemoryStream(data);
269+
var stream = new MemoryStream(response.RawBytes);
276270
return stream;
277271
}
278272

279273
if (type.Name.StartsWith("System.Nullable`1[[System.DateTime")) // return a datetime object
280274
{
281-
return DateTime.Parse(content, null, System.Globalization.DateTimeStyles.RoundtripKind);
275+
return DateTime.Parse(response.Content, null, System.Globalization.DateTimeStyles.RoundtripKind);
282276
}
283277

284278
if (type == typeof(String) || type.Name.StartsWith("System.Nullable")) // return primitive type
285279
{
286-
return ConvertType(content, type);
280+
return ConvertType(response.Content, type);
287281
}
288282

289283
// at this point, it must be a model (json)
290284
try
291285
{
292-
return JsonConvert.DeserializeObject(content, type);
286+
return JsonConvert.DeserializeObject(response.Content, type);
293287
}
294288
catch (Exception e)
295289
{

samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Client/ApiClient.cs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -241,16 +241,10 @@ public string ParameterToString(object obj)
241241
/// <returns>Object representation of the JSON string.</returns>
242242
public object Deserialize(IRestResponse response, Type type)
243243
{
244-
byte[] data = response.RawBytes;
245-
string content = response.Content;
246244
IList<Parameter> headers = response.Headers;
247-
if (type == typeof(Object)) // return an object
245+
if (type == typeof(byte[])) // return byte array
248246
{
249-
return content;
250-
}
251-
else if (type == typeof(byte[])) // return byte array
252-
{
253-
return data;
247+
return response.RawBytes;
254248
}
255249

256250
if (type == typeof(Stream))
@@ -267,29 +261,29 @@ public object Deserialize(IRestResponse response, Type type)
267261
if (match.Success)
268262
{
269263
string fileName = filePath + SanitizeFilename(match.Groups[1].Value.Replace("\"", "").Replace("'", ""));
270-
File.WriteAllBytes(fileName, data);
264+
File.WriteAllBytes(fileName, response.RawBytes);
271265
return new FileStream(fileName, FileMode.Open);
272266
}
273267
}
274268
}
275-
var stream = new MemoryStream(data);
269+
var stream = new MemoryStream(response.RawBytes);
276270
return stream;
277271
}
278272

279273
if (type.Name.StartsWith("System.Nullable`1[[System.DateTime")) // return a datetime object
280274
{
281-
return DateTime.Parse(content, null, System.Globalization.DateTimeStyles.RoundtripKind);
275+
return DateTime.Parse(response.Content, null, System.Globalization.DateTimeStyles.RoundtripKind);
282276
}
283277

284278
if (type == typeof(String) || type.Name.StartsWith("System.Nullable")) // return primitive type
285279
{
286-
return ConvertType(content, type);
280+
return ConvertType(response.Content, type);
287281
}
288282

289283
// at this point, it must be a model (json)
290284
try
291285
{
292-
return JsonConvert.DeserializeObject(content, type);
286+
return JsonConvert.DeserializeObject(response.Content, type);
293287
}
294288
catch (Exception e)
295289
{

samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Model/InlineResponse200.cs

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,38 @@ namespace IO.Swagger.Model
1818
public partial class InlineResponse200 : IEquatable<InlineResponse200>
1919
{
2020

21+
[JsonConverter(typeof(StringEnumConverter))]
22+
public enum StatusEnum {
23+
24+
[EnumMember(Value = "available")]
25+
Available,
26+
27+
[EnumMember(Value = "pending")]
28+
Pending,
29+
30+
[EnumMember(Value = "sold")]
31+
Sold
32+
}
33+
34+
/// <summary>
35+
/// pet status in the store
36+
/// </summary>
37+
/// <value>pet status in the store</value>
38+
[DataMember(Name="status", EmitDefaultValue=false)]
39+
public StatusEnum? Status { get; set; }
40+
2141
/// <summary>
2242
/// Initializes a new instance of the <see cref="InlineResponse200" /> class.
2343
/// Initializes a new instance of the <see cref="InlineResponse200" />class.
2444
/// </summary>
45+
/// <param name="Tags">Tags.</param>
2546
/// <param name="Id">Id (required).</param>
2647
/// <param name="Category">Category.</param>
48+
/// <param name="Status">pet status in the store.</param>
2749
/// <param name="Name">Name.</param>
50+
/// <param name="PhotoUrls">PhotoUrls.</param>
2851

29-
public InlineResponse200(long? Id = null, Object Category = null, string Name = null)
52+
public InlineResponse200(List<Tag> Tags = null, long? Id = null, Object Category = null, StatusEnum? Status = null, string Name = null, List<string> PhotoUrls = null)
3053
{
3154
// to ensure "Id" is required (not null)
3255
if (Id == null)
@@ -37,12 +60,21 @@ public InlineResponse200(long? Id = null, Object Category = null, string Name =
3760
{
3861
this.Id = Id;
3962
}
63+
this.Tags = Tags;
4064
this.Category = Category;
65+
this.Status = Status;
4166
this.Name = Name;
67+
this.PhotoUrls = PhotoUrls;
4268

4369
}
4470

4571

72+
/// <summary>
73+
/// Gets or Sets Tags
74+
/// </summary>
75+
[DataMember(Name="tags", EmitDefaultValue=false)]
76+
public List<Tag> Tags { get; set; }
77+
4678
/// <summary>
4779
/// Gets or Sets Id
4880
/// </summary>
@@ -61,6 +93,12 @@ public InlineResponse200(long? Id = null, Object Category = null, string Name =
6193
[DataMember(Name="name", EmitDefaultValue=false)]
6294
public string Name { get; set; }
6395

96+
/// <summary>
97+
/// Gets or Sets PhotoUrls
98+
/// </summary>
99+
[DataMember(Name="photoUrls", EmitDefaultValue=false)]
100+
public List<string> PhotoUrls { get; set; }
101+
64102
/// <summary>
65103
/// Returns the string presentation of the object
66104
/// </summary>
@@ -69,9 +107,12 @@ public override string ToString()
69107
{
70108
var sb = new StringBuilder();
71109
sb.Append("class InlineResponse200 {\n");
110+
sb.Append(" Tags: ").Append(Tags).Append("\n");
72111
sb.Append(" Id: ").Append(Id).Append("\n");
73112
sb.Append(" Category: ").Append(Category).Append("\n");
113+
sb.Append(" Status: ").Append(Status).Append("\n");
74114
sb.Append(" Name: ").Append(Name).Append("\n");
115+
sb.Append(" PhotoUrls: ").Append(PhotoUrls).Append("\n");
75116

76117
sb.Append("}\n");
77118
return sb.ToString();
@@ -109,6 +150,11 @@ public bool Equals(InlineResponse200 other)
109150
return false;
110151

111152
return
153+
(
154+
this.Tags == other.Tags ||
155+
this.Tags != null &&
156+
this.Tags.SequenceEqual(other.Tags)
157+
) &&
112158
(
113159
this.Id == other.Id ||
114160
this.Id != null &&
@@ -119,10 +165,20 @@ public bool Equals(InlineResponse200 other)
119165
this.Category != null &&
120166
this.Category.Equals(other.Category)
121167
) &&
168+
(
169+
this.Status == other.Status ||
170+
this.Status != null &&
171+
this.Status.Equals(other.Status)
172+
) &&
122173
(
123174
this.Name == other.Name ||
124175
this.Name != null &&
125176
this.Name.Equals(other.Name)
177+
) &&
178+
(
179+
this.PhotoUrls == other.PhotoUrls ||
180+
this.PhotoUrls != null &&
181+
this.PhotoUrls.SequenceEqual(other.PhotoUrls)
126182
);
127183
}
128184

@@ -138,15 +194,24 @@ public override int GetHashCode()
138194
int hash = 41;
139195
// Suitable nullity checks etc, of course :)
140196

197+
if (this.Tags != null)
198+
hash = hash * 59 + this.Tags.GetHashCode();
199+
141200
if (this.Id != null)
142201
hash = hash * 59 + this.Id.GetHashCode();
143202

144203
if (this.Category != null)
145204
hash = hash * 59 + this.Category.GetHashCode();
146205

206+
if (this.Status != null)
207+
hash = hash * 59 + this.Status.GetHashCode();
208+
147209
if (this.Name != null)
148210
hash = hash * 59 + this.Name.GetHashCode();
149211

212+
if (this.PhotoUrls != null)
213+
hash = hash * 59 + this.PhotoUrls.GetHashCode();
214+
150215
return hash;
151216
}
152217
}

samples/client/petstore/csharp/SwaggerClientTest/SwaggerClientTest.userprefs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
<Properties StartupItem="SwaggerClientTest.csproj">
22
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" />
3-
<MonoDevelop.Ide.Workbench ActiveDocument="Lib/SwaggerClient/src/main/csharp/IO/Swagger/Api/PetApi.cs">
3+
<MonoDevelop.Ide.Workbench ActiveDocument="TestPet.cs">
44
<Files>
5-
<File FileName="TestPet.cs" Line="1" Column="1" />
6-
<File FileName="TestOrder.cs" Line="1" Column="1" />
7-
<File FileName="Lib/SwaggerClient/src/main/csharp/IO/Swagger/Api/PetApi.cs" Line="18" Column="9" />
5+
<File FileName="TestPet.cs" Line="189" Column="4" />
6+
<File FileName="TestOrder.cs" Line="88" Column="47" />
87
</Files>
8+
<Pads>
9+
<Pad Id="MonoDevelop.NUnit.TestPad">
10+
<State name="__root__">
11+
<Node name="SwaggerClientTest" expanded="True" />
12+
</State>
13+
</Pad>
14+
</Pads>
915
</MonoDevelop.Ide.Workbench>
1016
<MonoDevelop.Ide.DebuggingService.Breakpoints>
1117
<BreakpointStore />

samples/client/petstore/csharp/SwaggerClientTest/TestOrder.cs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
using Newtonsoft.Json;
1111

1212

13-
namespace SwaggerClientTest.TestORder
13+
namespace SwaggerClientTest.TestOrder
1414
{
1515
[TestFixture ()]
1616
public class TestOrder
@@ -53,6 +53,48 @@ public void TesOrderDeserialization()
5353
Assert.AreEqual (true, o.Complete);
5454

5555
}
56+
57+
/// <summary>
58+
/// Test GetInvetory
59+
/// </summary>
60+
[Test ()]
61+
public void TestGetInventory ()
62+
{
63+
// set timeout to 10 seconds
64+
Configuration c1 = new Configuration (timeout: 10000);
65+
66+
StoreApi storeApi = new StoreApi (c1);
67+
Dictionary<String, int?> response = storeApi.GetInventory ();
68+
69+
foreach(KeyValuePair<string, int?> entry in response)
70+
{
71+
Assert.IsInstanceOf (typeof(int?), entry.Value);
72+
}
73+
74+
}
75+
76+
/// <summary>
77+
/// Test GetInvetoryInObject
78+
/// </summary>
79+
[Test ()]
80+
public void TestGetInventoryInObject ()
81+
{
82+
// set timeout to 10 seconds
83+
Configuration c1 = new Configuration (timeout: 10000);
84+
85+
StoreApi storeApi = new StoreApi (c1);
86+
Newtonsoft.Json.Linq.JObject response = (Newtonsoft.Json.Linq.JObject)storeApi.GetInventoryInObject ();
87+
88+
// should be a Newtonsoft.Json.Linq.JObject since type is object
89+
Assert.IsInstanceOf (typeof(Newtonsoft.Json.Linq.JObject), response);
90+
91+
foreach(KeyValuePair<string, string> entry in response.ToObject<Dictionary<string, string>>())
92+
{
93+
Assert.IsInstanceOf (typeof(int?), Int32.Parse(entry.Value));
94+
}
95+
96+
}
97+
5698
}
5799
}
58100

samples/client/petstore/csharp/SwaggerClientTest/TestPet.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,37 @@ public void TestGetPetById ()
158158

159159
}
160160

161+
/// <summary>
162+
/// Test GetPetByIdInObject
163+
/// </summary>
164+
[Test ()]
165+
public void TestGetPetByIdInObject ()
166+
{
167+
// set timeout to 10 seconds
168+
Configuration c1 = new Configuration (timeout: 10000);
169+
170+
PetApi petApi = new PetApi (c1);
171+
InlineResponse200 response = petApi.GetPetByIdInObject (petId);
172+
Assert.IsInstanceOf<InlineResponse200> (response, "Response is a Pet");
173+
174+
Assert.AreEqual ("Csharp test", response.Name);
175+
Assert.AreEqual (InlineResponse200.StatusEnum.Available, response.Status);
176+
177+
Assert.IsInstanceOf<List<Tag>> (response.Tags, "Response.Tags is a Array");
178+
Assert.AreEqual (petId, response.Tags [0].Id);
179+
Assert.AreEqual ("csharp sample tag name1", response.Tags [0].Name);
180+
181+
Assert.IsInstanceOf<List<String>> (response.PhotoUrls, "Response.PhotoUrls is a Array");
182+
Assert.AreEqual ("sample photoUrls", response.PhotoUrls [0]);
183+
184+
Assert.IsInstanceOf<Newtonsoft.Json.Linq.JObject> (response.Category, "Response.Category is a Newtonsoft.Json.Linq.JObject");
185+
186+
Newtonsoft.Json.Linq.JObject category = (Newtonsoft.Json.Linq.JObject)response.Category;
187+
Assert.AreEqual (56, (int)category ["id"]);
188+
Assert.AreEqual ("sample category name2", (string) category["name"]);
189+
190+
}
191+
161192
/// <summary>
162193
/// Test GetPetByIdWithByteArray
163194
/// </summary>

0 commit comments

Comments
 (0)