Merge pull request #2199 from wing328/readonly_property

[C#] Add support for Readonly property
This commit is contained in:
wing328 2016-02-21 23:54:52 +08:00
commit ad6380cb88
11 changed files with 138 additions and 39 deletions

0
bin/jaxrs-resteasy-petstore-server.sh Normal file → Executable file
View File

View File

@ -177,6 +177,9 @@ public class CodegenProperty {
if (this.isEnum != other.isEnum) { if (this.isEnum != other.isEnum) {
return false; return false;
} }
if (this.isReadOnly != other.isReadOnly && (this.isReadOnly == null || !this.isReadOnly.equals(other.isReadOnly))) {
return false;
}
if (this._enum != other._enum && (this._enum == null || !this._enum.equals(other._enum))) { if (this._enum != other._enum && (this._enum == null || !this._enum.equals(other._enum))) {
return false; return false;
} }

View File

@ -20,12 +20,33 @@ namespace {{packageName}}.Model
public partial class {{classname}} : {{#parent}}{{{parent}}}, {{/parent}} IEquatable<{{classname}}> public partial class {{classname}} : {{#parent}}{{{parent}}}, {{/parent}} IEquatable<{{classname}}>
{ {
/// <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}}() /// <exception cref="System.InvalidDataException">Thrown when required property is null</exception>
{{#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}}
@ -34,7 +55,7 @@ namespace {{packageName}}.Model
/// </summary>{{#description}} /// </summary>{{#description}}
/// <value>{{{description}}}</value>{{/description}} /// <value>{{{description}}}</value>{{/description}}
[DataMember(Name="{{baseName}}", EmitDefaultValue=false)] [DataMember(Name="{{baseName}}", EmitDefaultValue=false)]
public {{{datatype}}} {{name}} { get; set; } public {{{datatype}}} {{name}} { get; {{#isReadOnly}}private {{/isReadOnly}}set; }
{{/vars}} {{/vars}}

View File

@ -1069,7 +1069,8 @@
"properties": { "properties": {
"id": { "id": {
"type": "integer", "type": "integer",
"format": "int64" "format": "int64",
"readOnly": true
}, },
"petId": { "petId": {
"type": "integer", "type": "integer",

View File

@ -18,10 +18,15 @@ namespace IO.Swagger.Model
public partial class Category : IEquatable<Category> public partial class Category : IEquatable<Category>
{ {
/// <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() /// <exception cref="System.InvalidDataException">Thrown when required property is null</exception>
/// <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,10 +18,23 @@ namespace IO.Swagger.Model
public partial class Order : IEquatable<Order> public partial class Order : IEquatable<Order>
{ {
/// <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() /// <exception cref="System.InvalidDataException">Thrown when required property is null</exception>
/// <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;
} }
@ -30,7 +43,7 @@ namespace IO.Swagger.Model
/// Gets or Sets Id /// Gets or Sets Id
/// </summary> /// </summary>
[DataMember(Name="id", EmitDefaultValue=false)] [DataMember(Name="id", EmitDefaultValue=false)]
public long? Id { get; set; } public long? Id { get; private set; }
/// <summary> /// <summary>

View File

@ -18,10 +18,39 @@ namespace IO.Swagger.Model
public partial class Pet : IEquatable<Pet> public partial class Pet : IEquatable<Pet>
{ {
/// <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() /// <exception cref="System.InvalidDataException">Thrown when required property is null</exception>
/// <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,10 +18,15 @@ namespace IO.Swagger.Model
public partial class Tag : IEquatable<Tag> public partial class Tag : IEquatable<Tag>
{ {
/// <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() /// <exception cref="System.InvalidDataException">Thrown when required property is null</exception>
/// <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,10 +18,27 @@ namespace IO.Swagger.Model
public partial class User : IEquatable<User> public partial class User : IEquatable<User>
{ {
/// <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() /// <exception cref="System.InvalidDataException">Thrown when required property is null</exception>
/// <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;
} }

View File

@ -1,14 +1,18 @@
<Properties StartupItem="SwaggerClientTest.csproj"> <Properties StartupItem="SwaggerClientTest.csproj">
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" /> <MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" />
<MonoDevelop.Ide.Workbench ActiveDocument="TestApiClient.cs"> <MonoDevelop.Ide.Workbench ActiveDocument="TestPet.cs">
<Files> <Files>
<File FileName="TestPet.cs" Line="1" Column="1" /> <File FileName="TestPet.cs" Line="250" Column="4" />
<File FileName="Lib/SwaggerClient/src/main/csharp/IO/Swagger/Model/Pet.cs" Line="21" Column="64" />
<File FileName="TestConfiguration.cs" Line="1" Column="1" /> <File FileName="TestConfiguration.cs" Line="1" Column="1" />
<File FileName="Lib/SwaggerClient/src/main/csharp/IO/Swagger/Api/PetApi.cs" Line="1" Column="1" />
<File FileName="Lib/SwaggerClient/src/main/csharp/IO/Swagger/Client/ApiClient.cs" Line="1" Column="1" />
<File FileName="TestApiClient.cs" Line="8" Column="2" />
<File FileName="Lib/SwaggerClient/src/main/csharp/IO/Swagger/Client/Configuration.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

@ -21,9 +21,9 @@ namespace SwaggerClientTest.TestPet
private Pet createPet() private Pet createPet()
{ {
// create pet // create pet
Pet p = new Pet(); Pet p = new Pet(Name: "Csharp test", PhotoUrls: new List<string> { "http://petstore.com/csharp_test" });
p.Id = petId; p.Id = petId;
p.Name = "Csharp test"; //p.Name = "Csharp test";
p.Status = "available"; p.Status = "available";
// create Category object // create Category object
Category category = new Category(); Category category = new Category();
@ -33,7 +33,7 @@ namespace SwaggerClientTest.TestPet
// create Tag object // create Tag object
Tag tag = new Tag(); Tag tag = new Tag();
tag.Id = petId; tag.Id = petId;
tag.Name = "sample tag name1"; tag.Name = "csharp sample tag name1";
List<Tag> tags = new List<Tag>(new Tag[] {tag}); List<Tag> tags = new List<Tag>(new Tag[] {tag});
p.Tags = tags; p.Tags = tags;
p.Category = category; p.Category = category;
@ -86,7 +86,7 @@ namespace SwaggerClientTest.TestPet
Assert.IsInstanceOf<List<Tag>> (response.Tags, "Response.Tags is a Array"); Assert.IsInstanceOf<List<Tag>> (response.Tags, "Response.Tags is a Array");
Assert.AreEqual (petId, response.Tags [0].Id); Assert.AreEqual (petId, response.Tags [0].Id);
Assert.AreEqual ("sample tag name1", response.Tags [0].Name); Assert.AreEqual ("csharp sample tag name1", response.Tags [0].Name);
Assert.IsInstanceOf<List<String>> (response.PhotoUrls, "Response.PhotoUrls is a Array"); Assert.IsInstanceOf<List<String>> (response.PhotoUrls, "Response.PhotoUrls is a Array");
Assert.AreEqual ("sample photoUrls", response.PhotoUrls [0]); Assert.AreEqual ("sample photoUrls", response.PhotoUrls [0]);
@ -118,7 +118,7 @@ namespace SwaggerClientTest.TestPet
Assert.IsInstanceOf<List<Tag>> (response.Tags, "Response.Tags is a Array"); Assert.IsInstanceOf<List<Tag>> (response.Tags, "Response.Tags is a Array");
Assert.AreEqual (petId, response.Tags [0].Id); Assert.AreEqual (petId, response.Tags [0].Id);
Assert.AreEqual ("sample tag name1", response.Tags [0].Name); Assert.AreEqual ("csharp sample tag name1", response.Tags [0].Name);
Assert.IsInstanceOf<List<String>> (response.PhotoUrls, "Response.PhotoUrls is a Array"); Assert.IsInstanceOf<List<String>> (response.PhotoUrls, "Response.PhotoUrls is a Array");
Assert.AreEqual ("sample photoUrls", response.PhotoUrls [0]); Assert.AreEqual ("sample photoUrls", response.PhotoUrls [0]);
@ -147,7 +147,7 @@ namespace SwaggerClientTest.TestPet
Assert.IsInstanceOf<List<Tag>> (response.Tags, "Response.Tags is a Array"); Assert.IsInstanceOf<List<Tag>> (response.Tags, "Response.Tags is a Array");
Assert.AreEqual (petId, response.Tags [0].Id); Assert.AreEqual (petId, response.Tags [0].Id);
Assert.AreEqual ("sample tag name1", response.Tags [0].Name); Assert.AreEqual ("csharp sample tag name1", response.Tags [0].Name);
Assert.IsInstanceOf<List<String>> (response.PhotoUrls, "Response.PhotoUrls is a Array"); Assert.IsInstanceOf<List<String>> (response.PhotoUrls, "Response.PhotoUrls is a Array");
Assert.AreEqual ("sample photoUrls", response.PhotoUrls [0]); Assert.AreEqual ("sample photoUrls", response.PhotoUrls [0]);
@ -235,16 +235,16 @@ namespace SwaggerClientTest.TestPet
/// Test FindPetByStatus /// Test FindPetByStatus
/// </summary> /// </summary>
[Test ()] [Test ()]
public void TestFindPetByStatus () public void TestFindPetByTags ()
{ {
PetApi petApi = new PetApi (); PetApi petApi = new PetApi ();
List<String> statusList = new List<String>(new String[] {"available"}); List<String> tagsList = new List<String>(new String[] {"available"});
List<Pet> listPet = petApi.FindPetsByStatus (statusList); List<Pet> listPet = petApi.FindPetsByTags (tagsList);
foreach (Pet pet in listPet) // Loop through List with foreach. foreach (Pet pet in listPet) // Loop through List with foreach.
{ {
Assert.IsInstanceOf<Pet> (pet, "Response is a Pet"); Assert.IsInstanceOf<Pet> (pet, "Response is a Pet");
Assert.AreEqual ("available", pet.Status); Assert.AreEqual ("csharp sample tag name1", pet.Tags[0]);
} }
} }
@ -256,9 +256,9 @@ namespace SwaggerClientTest.TestPet
public void TestEqual() public void TestEqual()
{ {
// create pet // create pet
Pet p1 = new Pet(); Pet p1 = new Pet(Name: "Csharp test", PhotoUrls: new List<string> { "http://petstore.com/csharp_test"} );
p1.Id = petId; p1.Id = petId;
p1.Name = "Csharp test"; //p1.Name = "Csharp test";
p1.Status = "available"; p1.Status = "available";
// create Category object // create Category object
Category category1 = new Category(); Category category1 = new Category();
@ -268,14 +268,14 @@ namespace SwaggerClientTest.TestPet
// create Tag object // create Tag object
Tag tag1 = new Tag(); Tag tag1 = new Tag();
tag1.Id = petId; tag1.Id = petId;
tag1.Name = "sample tag name1"; tag1.Name = "csharp sample tag name1";
List<Tag> tags1 = new List<Tag>(new Tag[] {tag1}); List<Tag> tags1 = new List<Tag>(new Tag[] {tag1});
p1.Tags = tags1; p1.Tags = tags1;
p1.Category = category1; p1.Category = category1;
p1.PhotoUrls = photoUrls1; p1.PhotoUrls = photoUrls1;
// create pet 2 // create pet 2
Pet p2 = new Pet(); Pet p2 = new Pet(Name: "Csharp test", PhotoUrls: new List<string> { "http://petstore.com/csharp_test"} );
p2.Id = petId; p2.Id = petId;
p2.Name = "Csharp test"; p2.Name = "Csharp test";
p2.Status = "available"; p2.Status = "available";
@ -287,7 +287,7 @@ namespace SwaggerClientTest.TestPet
// create Tag object // create Tag object
Tag tag2 = new Tag(); Tag tag2 = new Tag();
tag2.Id = petId; tag2.Id = petId;
tag2.Name = "sample tag name1"; tag2.Name = "csharp sample tag name1";
List<Tag> tags2 = new List<Tag>(new Tag[] {tag2}); List<Tag> tags2 = new List<Tag>(new Tag[] {tag2});
p2.Tags = tags2; p2.Tags = tags2;
p2.Category = category2; p2.Category = category2;
@ -334,8 +334,9 @@ namespace SwaggerClientTest.TestPet
public void TestDefaultHeader () public void TestDefaultHeader ()
{ {
PetApi petApi = new PetApi (); PetApi petApi = new PetApi ();
// commented out the warning test below as it's confirmed the warning is working as expected
// there should be a warning for using AddDefaultHeader (deprecated) below // there should be a warning for using AddDefaultHeader (deprecated) below
petApi.AddDefaultHeader ("header_key", "header_value"); //petApi.AddDefaultHeader ("header_key", "header_value");
// the following should be used instead as suggested in the doc // the following should be used instead as suggested in the doc
petApi.Configuration.AddDefaultHeader ("header_key2", "header_value2"); petApi.Configuration.AddDefaultHeader ("header_key2", "header_value2");