Merge pull request #2218 from wing328/csharp_test_readonly

[C#] add test case, update c# model constructor
This commit is contained in:
wing328 2016-02-24 10:27:33 +08:00
commit 6ae6fe10ec
16 changed files with 175 additions and 43 deletions

View File

@ -20,10 +20,30 @@ namespace {{packageName}}.Models
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="{{classname}}" /> class. /// Initializes a new instance of the <see cref="{{classname}}" /> class.
/// </summary> /// </summary>
public {{classname}}() {{#vars}} /// <param name="{{name}}">{{#description}}{{description}}{{/description}}{{^description}}{{name}}{{/description}}{{#required}} (required){{/required}}{{#defaultValue}} (default to {{defaultValue}}){{/defaultValue}}.</param>
{{/vars}}
public {{classname}}({{#vars}}{{{datatype}}} {{name}} = null{{#hasMore}}, {{/hasMore}}{{/vars}})
{ {
{{#vars}}{{#defaultValue}}this.{{name}} = {{{defaultValue}}}; {{#vars}}{{#required}}// to ensure "{{name}}" is required (not null)
{{/defaultValue}}{{/vars}} if ({{name}} == null)
{
throw new InvalidDataException("{{name}} is a required property for {{classname}} and cannot be null");
}
else
{
this.{{name}} = {{name}};
}
{{/required}}{{/vars}}{{#vars}}{{^required}}{{#defaultValue}}// use default value if no "{{name}}" provided
if ({{name}} == null)
{
this.{{name}} = {{{defaultValue}}};
}
else
{
this.{{name}} = {{name}};
}
{{/defaultValue}}{{^defaultValue}}this.{{name}} = {{name}};
{{/defaultValue}}{{/required}}{{/vars}}
} }
{{#vars}} {{#vars}}

View File

@ -22,12 +22,11 @@ namespace {{packageName}}.Model
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="{{classname}}" />class. /// Initializes a new instance of the <see cref="{{classname}}" />class.
/// </summary> /// </summary>
/// <exception cref="System.InvalidDataException">Thrown when required property is null</exception> {{#vars}}{{^isReadOnly}} /// <param name="{{name}}">{{#description}}{{description}}{{/description}}{{^description}}{{name}}{{/description}}{{#required}} (required){{/required}}{{#defaultValue}} (default to {{defaultValue}}){{/defaultValue}}.</param>
{{#vars}} /// <param name="{{name}}">{{#description}}{{description}}{{/description}}{{^description}}{{name}}{{/description}}{{#required}} (required){{/required}}{{#defaultValue}} (default to {{defaultValue}}){{/defaultValue}}.</param> {{/isReadOnly}}{{/vars}}
{{/vars}} public {{classname}}({{#vars}}{{^isReadOnly}}{{{datatype}}} {{name}} = null{{#hasMore}}, {{/hasMore}}{{/isReadOnly}}{{/vars}})
public {{classname}}({{#vars}}{{{datatype}}} {{name}} = null{{#hasMore}}, {{/hasMore}}{{/vars}})
{ {
{{#vars}}{{#required}}// to ensure "{{name}}" is required (not null) {{#vars}}{{^isReadOnly}}{{#required}}// to ensure "{{name}}" is required (not null)
if ({{name}} == null) if ({{name}} == null)
{ {
throw new InvalidDataException("{{name}} is a required property for {{classname}} and cannot be null"); throw new InvalidDataException("{{name}} is a required property for {{classname}} and cannot be null");
@ -36,7 +35,7 @@ namespace {{packageName}}.Model
{ {
this.{{name}} = {{name}}; this.{{name}} = {{name}};
} }
{{/required}}{{/vars}}{{#vars}}{{^required}}{{#defaultValue}}// use default value if no "{{name}}" provided {{/required}}{{/isReadOnly}}{{/vars}}{{#vars}}{{^isReadOnly}}{{^required}}{{#defaultValue}}// use default value if no "{{name}}" provided
if ({{name}} == null) if ({{name}} == null)
{ {
this.{{name}} = {{{defaultValue}}}; this.{{name}} = {{{defaultValue}}};
@ -46,7 +45,7 @@ namespace {{packageName}}.Model
this.{{name}} = {{name}}; this.{{name}} = {{name}};
} }
{{/defaultValue}}{{^defaultValue}}this.{{name}} = {{name}}; {{/defaultValue}}{{^defaultValue}}this.{{name}} = {{name}};
{{/defaultValue}}{{/required}}{{/vars}} {{/defaultValue}}{{/required}}{{/isReadOnly}}{{/vars}}
} }
{{#vars}} {{#vars}}

View File

@ -20,9 +20,9 @@ namespace IO.Swagger.Model
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Category" />class. /// Initializes a new instance of the <see cref="Category" />class.
/// </summary> /// </summary>
/// <exception cref="System.InvalidDataException">Thrown when required property is null</exception>
/// <param name="Id">Id.</param> /// <param name="Id">Id.</param>
/// <param name="Name">Name.</param> /// <param name="Name">Name.</param>
public Category(long? Id = null, string Name = null) public Category(long? Id = null, string Name = null)
{ {
this.Id = Id; this.Id = Id;

View File

@ -20,16 +20,14 @@ namespace IO.Swagger.Model
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Order" />class. /// Initializes a new instance of the <see cref="Order" />class.
/// </summary> /// </summary>
/// <exception cref="System.InvalidDataException">Thrown when required property is null</exception>
/// <param name="Id">Id.</param>
/// <param name="PetId">PetId.</param> /// <param name="PetId">PetId.</param>
/// <param name="Quantity">Quantity.</param> /// <param name="Quantity">Quantity.</param>
/// <param name="ShipDate">ShipDate.</param> /// <param name="ShipDate">ShipDate.</param>
/// <param name="Status">Order Status.</param> /// <param name="Status">Order Status.</param>
/// <param name="Complete">Complete.</param> /// <param name="Complete">Complete.</param>
public Order(long? Id = null, long? PetId = null, int? Quantity = null, DateTime? ShipDate = null, string Status = null, bool? Complete = null)
public Order(long? PetId = null, int? Quantity = null, DateTime? ShipDate = null, string Status = null, bool? Complete = null)
{ {
this.Id = Id;
this.PetId = PetId; this.PetId = PetId;
this.Quantity = Quantity; this.Quantity = Quantity;
this.ShipDate = ShipDate; this.ShipDate = ShipDate;

View File

@ -20,13 +20,13 @@ namespace IO.Swagger.Model
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Pet" />class. /// Initializes a new instance of the <see cref="Pet" />class.
/// </summary> /// </summary>
/// <exception cref="System.InvalidDataException">Thrown when required property is null</exception>
/// <param name="Id">Id.</param> /// <param name="Id">Id.</param>
/// <param name="Category">Category.</param> /// <param name="Category">Category.</param>
/// <param name="Name">Name (required).</param> /// <param name="Name">Name (required).</param>
/// <param name="PhotoUrls">PhotoUrls (required).</param> /// <param name="PhotoUrls">PhotoUrls (required).</param>
/// <param name="Tags">Tags.</param> /// <param name="Tags">Tags.</param>
/// <param name="Status">pet status in the store.</param> /// <param name="Status">pet status in the store.</param>
public Pet(long? Id = null, Category Category = null, string Name = null, List<string> PhotoUrls = null, List<Tag> Tags = null, string Status = null) public Pet(long? Id = null, Category Category = null, string Name = null, List<string> PhotoUrls = null, List<Tag> Tags = null, string Status = null)
{ {
// to ensure "Name" is required (not null) // to ensure "Name" is required (not null)

View File

@ -20,9 +20,9 @@ namespace IO.Swagger.Model
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Tag" />class. /// Initializes a new instance of the <see cref="Tag" />class.
/// </summary> /// </summary>
/// <exception cref="System.InvalidDataException">Thrown when required property is null</exception>
/// <param name="Id">Id.</param> /// <param name="Id">Id.</param>
/// <param name="Name">Name.</param> /// <param name="Name">Name.</param>
public Tag(long? Id = null, string Name = null) public Tag(long? Id = null, string Name = null)
{ {
this.Id = Id; this.Id = Id;

View File

@ -20,7 +20,6 @@ namespace IO.Swagger.Model
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="User" />class. /// Initializes a new instance of the <see cref="User" />class.
/// </summary> /// </summary>
/// <exception cref="System.InvalidDataException">Thrown when required property is null</exception>
/// <param name="Id">Id.</param> /// <param name="Id">Id.</param>
/// <param name="Username">Username.</param> /// <param name="Username">Username.</param>
/// <param name="FirstName">FirstName.</param> /// <param name="FirstName">FirstName.</param>
@ -29,6 +28,7 @@ namespace IO.Swagger.Model
/// <param name="Password">Password.</param> /// <param name="Password">Password.</param>
/// <param name="Phone">Phone.</param> /// <param name="Phone">Phone.</param>
/// <param name="UserStatus">User Status.</param> /// <param name="UserStatus">User Status.</param>
public User(long? Id = null, string Username = null, string FirstName = null, string LastName = null, string Email = null, string Password = null, string Phone = null, int? UserStatus = null) public User(long? Id = null, string Username = null, string FirstName = null, string LastName = null, string Email = null, string Password = null, string Phone = null, int? UserStatus = null)
{ {
this.Id = Id; this.Id = Id;

View File

@ -58,6 +58,7 @@
<Compile Include="TestApiClient.cs" /> <Compile Include="TestApiClient.cs" />
<Compile Include="TestConfiguration.cs" /> <Compile Include="TestConfiguration.cs" />
<Compile Include="Lib\SwaggerClient\src\main\csharp\IO\Swagger\Client\ApiResponse.cs" /> <Compile Include="Lib\SwaggerClient\src\main\csharp\IO\Swagger\Client\ApiResponse.cs" />
<Compile Include="TestOrder.cs" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup> <ItemGroup>

View File

@ -1,18 +1,10 @@
<Properties StartupItem="SwaggerClientTest.csproj"> <Properties StartupItem="SwaggerClientTest.csproj">
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" /> <MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" />
<MonoDevelop.Ide.Workbench ActiveDocument="TestPet.cs"> <MonoDevelop.Ide.Workbench ActiveDocument="TestOrder.cs">
<Files> <Files>
<File FileName="TestPet.cs" Line="250" Column="4" /> <File FileName="TestPet.cs" Line="84" Column="10" />
<File FileName="Lib/SwaggerClient/src/main/csharp/IO/Swagger/Model/Pet.cs" Line="21" Column="64" /> <File FileName="TestOrder.cs" Line="42" Column="12" />
<File FileName="TestConfiguration.cs" Line="1" Column="1" />
</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

@ -0,0 +1,58 @@
using NUnit.Framework;
using System;
using System.Linq;
using System.IO;
using System.Collections.Generic;
using IO.Swagger.Api;
using IO.Swagger.Model;
using IO.Swagger.Client;
using System.Reflection;
using Newtonsoft.Json;
namespace SwaggerClientTest.TestORder
{
[TestFixture ()]
public class TestOrder
{
public TestOrder ()
{
}
/// <summary>
/// Test creating a new instance of Order
/// </summary>
[Test ()]
public void TestNewOrder()
{
Order o = new Order ();
Assert.IsNull (o.Id);
}
/// <summary>
/// Test deserialization of JSON to Order and its readonly property
/// </summary>
[Test ()]
public void TesOrderDeserialization()
{
string json = @"{
'id': 1982,
'petId': 1020,
'quantity': 1,
'status': 'placed',
'complete': true,
}";
var o = JsonConvert.DeserializeObject<Order>(json);
Assert.AreEqual (1982, o.Id);
Assert.AreEqual (1020, o.PetId);
Assert.AreEqual (1, o.Quantity);
Assert.AreEqual ("placed", o.Status);
Assert.AreEqual (true, o.Complete);
}
}
}

View File

@ -1,9 +1,9 @@
/Users/williamcheng/Code/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/.NETFramework,Version=v4.5.AssemblyAttribute.cs /Users/williamcheng/Code/wing328/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/.NETFramework,Version=v4.5.AssemblyAttribute.cs
/Users/williamcheng/Code/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/SwaggerClientTest.swagger-logo.png /Users/williamcheng/Code/wing328/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/SwaggerClientTest.swagger-logo.png
/Users/williamcheng/Code/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/SwaggerClientTest.dll.mdb /Users/williamcheng/Code/wing328/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/SwaggerClientTest.dll.mdb
/Users/williamcheng/Code/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/SwaggerClientTest.dll /Users/williamcheng/Code/wing328/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/SwaggerClientTest.dll
/Users/williamcheng/Code/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/SwaggerClientTest.dll /Users/williamcheng/Code/wing328/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/SwaggerClientTest.dll
/Users/williamcheng/Code/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/SwaggerClientTest.dll.mdb /Users/williamcheng/Code/wing328/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/SwaggerClientTest.dll.mdb
/Users/williamcheng/Code/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/Newtonsoft.Json.dll /Users/williamcheng/Code/wing328/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/Newtonsoft.Json.dll
/Users/williamcheng/Code/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/nunit.framework.dll /Users/williamcheng/Code/wing328/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/nunit.framework.dll
/Users/williamcheng/Code/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/RestSharp.dll /Users/williamcheng/Code/wing328/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/RestSharp.dll

View File

@ -18,8 +18,12 @@ namespace IO.Swagger.Models
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Category" /> class. /// Initializes a new instance of the <see cref="Category" /> class.
/// </summary> /// </summary>
public Category() /// <param name="Id">Id.</param>
/// <param name="Name">Name.</param>
public Category(long? Id = null, string Name = null)
{ {
this.Id = Id;
this.Name = Name;
} }

View File

@ -18,8 +18,20 @@ namespace IO.Swagger.Models
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Order" /> class. /// Initializes a new instance of the <see cref="Order" /> class.
/// </summary> /// </summary>
public Order() /// <param name="Id">Id.</param>
/// <param name="PetId">PetId.</param>
/// <param name="Quantity">Quantity.</param>
/// <param name="ShipDate">ShipDate.</param>
/// <param name="Status">Order Status.</param>
/// <param name="Complete">Complete.</param>
public Order(long? Id = null, long? PetId = null, int? Quantity = null, DateTime? ShipDate = null, string Status = null, bool? Complete = null)
{ {
this.Id = Id;
this.PetId = PetId;
this.Quantity = Quantity;
this.ShipDate = ShipDate;
this.Status = Status;
this.Complete = Complete;
} }

View File

@ -18,8 +18,36 @@ namespace IO.Swagger.Models
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Pet" /> class. /// Initializes a new instance of the <see cref="Pet" /> class.
/// </summary> /// </summary>
public Pet() /// <param name="Id">Id.</param>
/// <param name="Category">Category.</param>
/// <param name="Name">Name (required).</param>
/// <param name="PhotoUrls">PhotoUrls (required).</param>
/// <param name="Tags">Tags.</param>
/// <param name="Status">pet status in the store.</param>
public Pet(long? Id = null, Category Category = null, string Name = null, List<string> PhotoUrls = null, List<Tag> Tags = null, string Status = null)
{ {
// to ensure "Name" is required (not null)
if (Name == null)
{
throw new InvalidDataException("Name is a required property for Pet and cannot be null");
}
else
{
this.Name = Name;
}
// to ensure "PhotoUrls" is required (not null)
if (PhotoUrls == null)
{
throw new InvalidDataException("PhotoUrls is a required property for Pet and cannot be null");
}
else
{
this.PhotoUrls = PhotoUrls;
}
this.Id = Id;
this.Category = Category;
this.Tags = Tags;
this.Status = Status;
} }

View File

@ -18,8 +18,12 @@ namespace IO.Swagger.Models
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Tag" /> class. /// Initializes a new instance of the <see cref="Tag" /> class.
/// </summary> /// </summary>
public Tag() /// <param name="Id">Id.</param>
/// <param name="Name">Name.</param>
public Tag(long? Id = null, string Name = null)
{ {
this.Id = Id;
this.Name = Name;
} }

View File

@ -18,8 +18,24 @@ namespace IO.Swagger.Models
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="User" /> class. /// Initializes a new instance of the <see cref="User" /> class.
/// </summary> /// </summary>
public User() /// <param name="Id">Id.</param>
/// <param name="Username">Username.</param>
/// <param name="FirstName">FirstName.</param>
/// <param name="LastName">LastName.</param>
/// <param name="Email">Email.</param>
/// <param name="Password">Password.</param>
/// <param name="Phone">Phone.</param>
/// <param name="UserStatus">User Status.</param>
public User(long? Id = null, string Username = null, string FirstName = null, string LastName = null, string Email = null, string Password = null, string Phone = null, int? UserStatus = null)
{ {
this.Id = Id;
this.Username = Username;
this.FirstName = FirstName;
this.LastName = LastName;
this.Email = Email;
this.Password = Password;
this.Phone = Phone;
this.UserStatus = UserStatus;
} }