[aspnetcore] use default in model constructors, supports enums (#4573)

* [aspnetcore] Use default rather than null in ctor

See original issue #3608

This adds same model constructor logic to aspnetcore as what was added
to csharp generator by PR #4145.

This doesn't include NancyFX because model construction relies more on
object initialization in that generator.

* [aspnetcore] ctor defaults + enum support

This follows up to #4145, and modifies model constructors to use
default(x) instead of initializing to nulls. default(x) works in all
cases using intuitive default values it is intended to support.

Example:

  csharp> public enum Color { RED = -1, BLUE = 0, GREEN }
  csharp> var color = default(Color)
  csharp> color
  BLUE

In the above example, The default of BLUE=0 is expected. For nullable
enums, this would be null as a default.

The aspnetcore generated code is also updated to support enums and
nested enums to account for changed to the petstore.yaml used to
generate the sample.

* [aspnetcore] Regenerate sample
This commit is contained in:
Jim Schubert
2017-01-18 03:43:12 -05:00
committed by wing328
parent da01b2e71a
commit da6b57f3fc
14 changed files with 136 additions and 61 deletions

View File

@@ -0,0 +1,14 @@
/// <summary>
/// {{^description}}Gets or Sets {{{name}}}{{/description}}{{#description}}{{{description}}}{{/description}}
/// </summary>{{#description}}
/// <value>{{{description}}}</value>{{/description}}
public enum {{#datatypeWithEnum}}{{.}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}}
{
{{#allowableValues}}{{#enumVars}}
/// <summary>
/// Enum {{name}} for {{{value}}}
/// </summary>
[EnumMember(Value = {{#isLong}}"{{/isLong}}{{#isInteger}}"{{/isInteger}}{{#isFloat}}"{{/isFloat}}{{#isDouble}}"{{/isDouble}}{{{value}}}{{#isLong}}"{{/isLong}}{{#isInteger}}"{{/isInteger}}{{#isDouble}}"{{/isDouble}}{{#isFloat}}"{{/isFloat}})]
{{name}}{{#isLong}} = {{{value}}}{{/isLong}}{{#isInteger}} = {{{value}}}{{/isInteger}}{{^-last}},
{{/-last}}{{/enumVars}}{{/allowableValues}}
}

View File

@@ -1,5 +1,5 @@
{
"projects": [ "src", "test" ],
"projects": [ "src" ],
"sdk": {
"version": "1.0.0-preview2-003121",
"runtime": "coreclr"

View File

@@ -13,18 +13,40 @@ using Newtonsoft.Json;
{{#model}}
namespace {{packageName}}.Models
{
{{#isEnum}}{{>enumClass}}{{/isEnum}}{{^isEnum}}
/// <summary>
/// {{description}}
/// </summary>
[DataContract]
public partial class {{classname}} : {{#parent}}{{{parent}}}, {{/parent}} IEquatable<{{classname}}>
{
{{#vars}}
{{#isEnum}}
{{>enumClass}}
{{/isEnum}}
{{#items.isEnum}}
{{#items}}
{{>enumClass}}
{{/items}}
{{/items.isEnum}}
{{/vars}}
{{#vars}}
{{#isEnum}}
/// <summary>
/// {{^description}}Gets or Sets {{{name}}}{{/description}}{{#description}}{{{description}}}{{/description}}
/// </summary>{{#description}}
/// <value>{{{description}}}</value>{{/description}}
[DataMember(Name="{{baseName}}")]
public {{{datatypeWithEnum}}}{{#isEnum}}{{^isContainer}}?{{/isContainer}}{{/isEnum}} {{name}} { get; set; }
{{/isEnum}}
{{/vars}}
/// <summary>
/// Initializes a new instance of the <see cref="{{classname}}" /> class.
/// </summary>
{{#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}})
public {{classname}}({{#readWriteVars}}{{{datatypeWithEnum}}}{{#isEnum}}{{^isContainer}}?{{/isContainer}}{{/isEnum}} {{name}} = {{#defaultValue}}{{{defaultValue}}}{{/defaultValue}}{{^defaultValue}}default({{{datatypeWithEnum}}}{{#isEnum}}{{^isContainer}}?{{/isContainer}}{{/isEnum}}){{/defaultValue}}{{^-last}}, {{/-last}}{{/readWriteVars}})
{
{{#vars}}{{#required}}// to ensure "{{name}}" is required (not null)
if ({{name}} == null)
@@ -49,13 +71,14 @@ namespace {{packageName}}.Models
}
{{#vars}}
{{^isEnum}}
/// <summary>
/// {{^description}}Gets or Sets {{{name}}}{{/description}}{{#description}}{{{description}}}{{/description}}
/// {{^description}}Gets or Sets {{{name}}}{{/description}}{{#description}}{{description}}{{/description}}
/// </summary>{{#description}}
/// <value>{{{description}}}</value>{{/description}}
/// <value>{{description}}</value>{{/description}}
[DataMember(Name="{{baseName}}")]
public {{{datatype}}} {{name}} { get; set; }
public {{{datatype}}} {{name}} { get; {{#isReadOnly}}private {{/isReadOnly}}set; }
{{/isEnum}}
{{/vars}}
/// <summary>
@@ -153,6 +176,7 @@ namespace {{packageName}}.Models
#endregion Operators
}
{{/isEnum}}
{{/model}}
{{/models}}
}

View File

@@ -22,7 +22,8 @@
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
"Microsoft.EntityFrameworkCore": "1.0.0",
"Swashbuckle.SwaggerGen": "6.0.0-beta901",
"Swashbuckle.SwaggerUi": "6.0.0-beta901"
"Swashbuckle.SwaggerUi": "6.0.0-beta901",
"Newtonsoft.Json": "9.0.1"
},
"tools": {

View File

@@ -7,7 +7,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
global.json = global.json
EndProjectSection
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "IO.Swagger", "src\IO.Swagger\IO.Swagger.xproj", "{85CF9021-4522-4D1B-871B-D4C1C6483FA1}"
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "IO.Swagger", "src\IO.Swagger\IO.Swagger.xproj", "{795C6B14-1C3E-45F5-AF6F-EE47B197FF1E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -15,10 +15,10 @@ Global
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{85CF9021-4522-4D1B-871B-D4C1C6483FA1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{85CF9021-4522-4D1B-871B-D4C1C6483FA1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{85CF9021-4522-4D1B-871B-D4C1C6483FA1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{85CF9021-4522-4D1B-871B-D4C1C6483FA1}.Release|Any CPU.Build.0 = Release|Any CPU
{795C6B14-1C3E-45F5-AF6F-EE47B197FF1E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{795C6B14-1C3E-45F5-AF6F-EE47B197FF1E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{795C6B14-1C3E-45F5-AF6F-EE47B197FF1E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{795C6B14-1C3E-45F5-AF6F-EE47B197FF1E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@@ -1,7 +1,7 @@
{
"projects": [ "src", "test" ],
"projects": [ "src" ],
"sdk": {
"version": "1.0.0-preview2-003121",
"version": "1.0.0-preview3-003171",
"runtime": "coreclr"
}
}

View File

@@ -6,7 +6,7 @@
</PropertyGroup>
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>{85CF9021-4522-4D1B-871B-D4C1C6483FA1}</ProjectGuid>
<ProjectGuid>{795C6B14-1C3E-45F5-AF6F-EE47B197FF1E}</ProjectGuid>
<RootNamespace>IO.Swagger</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>

View File

@@ -20,19 +20,21 @@ using Newtonsoft.Json;
namespace IO.Swagger.Models
{
/// <summary>
/// Describes the result of uploading an image resource
/// </summary>
[DataContract]
public partial class ApiResponse : IEquatable<ApiResponse>
{
/// <summary>
/// Initializes a new instance of the <see cref="ApiResponse" /> class.
/// </summary>
/// <param name="Code">Code.</param>
/// <param name="Type">Type.</param>
/// <param name="Message">Message.</param>
public ApiResponse(int? Code = null, string Type = null, string Message = null)
public ApiResponse(int? Code = default(int?), string Type = default(string), string Message = default(string))
{
this.Code = Code;
this.Type = Type;
@@ -45,20 +47,17 @@ namespace IO.Swagger.Models
/// </summary>
[DataMember(Name="code")]
public int? Code { get; set; }
/// <summary>
/// Gets or Sets Type
/// </summary>
[DataMember(Name="type")]
public string Type { get; set; }
/// <summary>
/// Gets or Sets Message
/// </summary>
[DataMember(Name="message")]
public string Message { get; set; }
/// <summary>
/// Returns the string presentation of the object
/// </summary>

View File

@@ -20,18 +20,20 @@ using Newtonsoft.Json;
namespace IO.Swagger.Models
{
/// <summary>
/// A category for a pet
/// </summary>
[DataContract]
public partial class Category : IEquatable<Category>
{
/// <summary>
/// Initializes a new instance of the <see cref="Category" /> class.
/// </summary>
/// <param name="Id">Id.</param>
/// <param name="Name">Name.</param>
public Category(long? Id = null, string Name = null)
public Category(long? Id = default(long?), string Name = default(string))
{
this.Id = Id;
this.Name = Name;
@@ -43,14 +45,12 @@ namespace IO.Swagger.Models
/// </summary>
[DataMember(Name="id")]
public long? Id { get; set; }
/// <summary>
/// Gets or Sets Name
/// </summary>
[DataMember(Name="name")]
public string Name { get; set; }
/// <summary>
/// Returns the string presentation of the object
/// </summary>

View File

@@ -20,12 +20,45 @@ using Newtonsoft.Json;
namespace IO.Swagger.Models
{
/// <summary>
/// An order for a pets from the pet store
/// </summary>
[DataContract]
public partial class Order : IEquatable<Order>
{
/// <summary>
/// Order Status
/// </summary>
/// <value>Order Status</value>
public enum StatusEnum
{
/// <summary>
/// Enum PlacedEnum for "placed"
/// </summary>
[EnumMember(Value = "placed")]
PlacedEnum,
/// <summary>
/// Enum ApprovedEnum for "approved"
/// </summary>
[EnumMember(Value = "approved")]
ApprovedEnum,
/// <summary>
/// Enum DeliveredEnum for "delivered"
/// </summary>
[EnumMember(Value = "delivered")]
DeliveredEnum
}
/// <summary>
/// Order Status
/// </summary>
/// <value>Order Status</value>
[DataMember(Name="status")]
public StatusEnum? Status { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="Order" /> class.
/// </summary>
@@ -35,7 +68,7 @@ namespace IO.Swagger.Models
/// <param name="ShipDate">ShipDate.</param>
/// <param name="Status">Order Status.</param>
/// <param name="Complete">Complete (default to false).</param>
public Order(long? Id = null, long? PetId = null, int? Quantity = null, DateTime? ShipDate = null, string Status = null, bool? Complete = null)
public Order(long? Id = default(long?), long? PetId = default(long?), int? Quantity = default(int?), DateTime? ShipDate = default(DateTime?), StatusEnum? Status = default(StatusEnum?), bool? Complete = false)
{
this.Id = Id;
this.PetId = PetId;
@@ -59,39 +92,27 @@ namespace IO.Swagger.Models
/// </summary>
[DataMember(Name="id")]
public long? Id { get; set; }
/// <summary>
/// Gets or Sets PetId
/// </summary>
[DataMember(Name="petId")]
public long? PetId { get; set; }
/// <summary>
/// Gets or Sets Quantity
/// </summary>
[DataMember(Name="quantity")]
public int? Quantity { get; set; }
/// <summary>
/// Gets or Sets ShipDate
/// </summary>
[DataMember(Name="shipDate")]
public DateTime? ShipDate { get; set; }
/// <summary>
/// Order Status
/// </summary>
/// <value>Order Status</value>
[DataMember(Name="status")]
public string Status { get; set; }
/// <summary>
/// Gets or Sets Complete
/// </summary>
[DataMember(Name="complete")]
public bool? Complete { get; set; }
/// <summary>
/// Returns the string presentation of the object
/// </summary>

View File

@@ -20,12 +20,45 @@ using Newtonsoft.Json;
namespace IO.Swagger.Models
{
/// <summary>
/// A pet for sale in the pet store
/// </summary>
[DataContract]
public partial class Pet : IEquatable<Pet>
{
/// <summary>
/// pet status in the store
/// </summary>
/// <value>pet status in the store</value>
public enum StatusEnum
{
/// <summary>
/// Enum AvailableEnum for "available"
/// </summary>
[EnumMember(Value = "available")]
AvailableEnum,
/// <summary>
/// Enum PendingEnum for "pending"
/// </summary>
[EnumMember(Value = "pending")]
PendingEnum,
/// <summary>
/// Enum SoldEnum for "sold"
/// </summary>
[EnumMember(Value = "sold")]
SoldEnum
}
/// <summary>
/// pet status in the store
/// </summary>
/// <value>pet status in the store</value>
[DataMember(Name="status")]
public StatusEnum? Status { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="Pet" /> class.
/// </summary>
@@ -35,7 +68,7 @@ namespace IO.Swagger.Models
/// <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)
public Pet(long? Id = default(long?), Category Category = default(Category), string Name = default(string), List<string> PhotoUrls = default(List<string>), List<Tag> Tags = default(List<Tag>), StatusEnum? Status = default(StatusEnum?))
{
// to ensure "Name" is required (not null)
if (Name == null)
@@ -67,39 +100,27 @@ namespace IO.Swagger.Models
/// </summary>
[DataMember(Name="id")]
public long? Id { get; set; }
/// <summary>
/// Gets or Sets Category
/// </summary>
[DataMember(Name="category")]
public Category Category { get; set; }
/// <summary>
/// Gets or Sets Name
/// </summary>
[DataMember(Name="name")]
public string Name { get; set; }
/// <summary>
/// Gets or Sets PhotoUrls
/// </summary>
[DataMember(Name="photoUrls")]
public List<string> PhotoUrls { get; set; }
/// <summary>
/// Gets or Sets Tags
/// </summary>
[DataMember(Name="tags")]
public List<Tag> Tags { get; set; }
/// <summary>
/// pet status in the store
/// </summary>
/// <value>pet status in the store</value>
[DataMember(Name="status")]
public string Status { get; set; }
/// <summary>
/// Returns the string presentation of the object
/// </summary>

View File

@@ -20,18 +20,20 @@ using Newtonsoft.Json;
namespace IO.Swagger.Models
{
/// <summary>
/// A tag for a pet
/// </summary>
[DataContract]
public partial class Tag : IEquatable<Tag>
{
/// <summary>
/// Initializes a new instance of the <see cref="Tag" /> class.
/// </summary>
/// <param name="Id">Id.</param>
/// <param name="Name">Name.</param>
public Tag(long? Id = null, string Name = null)
public Tag(long? Id = default(long?), string Name = default(string))
{
this.Id = Id;
this.Name = Name;
@@ -43,14 +45,12 @@ namespace IO.Swagger.Models
/// </summary>
[DataMember(Name="id")]
public long? Id { get; set; }
/// <summary>
/// Gets or Sets Name
/// </summary>
[DataMember(Name="name")]
public string Name { get; set; }
/// <summary>
/// Returns the string presentation of the object
/// </summary>

View File

@@ -20,12 +20,14 @@ using Newtonsoft.Json;
namespace IO.Swagger.Models
{
/// <summary>
/// A User who is purchasing from the pet store
/// </summary>
[DataContract]
public partial class User : IEquatable<User>
{
/// <summary>
/// Initializes a new instance of the <see cref="User" /> class.
/// </summary>
@@ -37,7 +39,7 @@ namespace IO.Swagger.Models
/// <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)
public User(long? Id = default(long?), string Username = default(string), string FirstName = default(string), string LastName = default(string), string Email = default(string), string Password = default(string), string Phone = default(string), int? UserStatus = default(int?))
{
this.Id = Id;
this.Username = Username;
@@ -55,43 +57,36 @@ namespace IO.Swagger.Models
/// </summary>
[DataMember(Name="id")]
public long? Id { get; set; }
/// <summary>
/// Gets or Sets Username
/// </summary>
[DataMember(Name="username")]
public string Username { get; set; }
/// <summary>
/// Gets or Sets FirstName
/// </summary>
[DataMember(Name="firstName")]
public string FirstName { get; set; }
/// <summary>
/// Gets or Sets LastName
/// </summary>
[DataMember(Name="lastName")]
public string LastName { get; set; }
/// <summary>
/// Gets or Sets Email
/// </summary>
[DataMember(Name="email")]
public string Email { get; set; }
/// <summary>
/// Gets or Sets Password
/// </summary>
[DataMember(Name="password")]
public string Password { get; set; }
/// <summary>
/// Gets or Sets Phone
/// </summary>
[DataMember(Name="phone")]
public string Phone { get; set; }
/// <summary>
/// User Status
/// </summary>
@@ -99,7 +94,6 @@ namespace IO.Swagger.Models
[DataMember(Name="userStatus")]
public int? UserStatus { get; set; }
/// <summary>
/// Returns the string presentation of the object
/// </summary>

View File

@@ -22,7 +22,8 @@
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
"Microsoft.EntityFrameworkCore": "1.0.0",
"Swashbuckle.SwaggerGen": "6.0.0-beta901",
"Swashbuckle.SwaggerUi": "6.0.0-beta901"
"Swashbuckle.SwaggerUi": "6.0.0-beta901",
"Newtonsoft.Json": "9.0.1"
},
"tools": {