Merge pull request #2484 from wing328/csharp_fix_constructor

[C#] fix extra comma in the constructor when last property is read-only
This commit is contained in:
wing328 2016-04-09 21:14:30 +08:00
commit 5ea66f6762
9 changed files with 386 additions and 14 deletions

View File

@ -32,6 +32,7 @@ public class CodegenProperty {
public Boolean exclusiveMinimum; public Boolean exclusiveMinimum;
public Boolean exclusiveMaximum; public Boolean exclusiveMaximum;
public Boolean hasMore, required, secondaryParam; public Boolean hasMore, required, secondaryParam;
public Boolean hasMoreNonReadOnly; // for model constructor, true if next properyt is not readonly
public Boolean isPrimitiveType, isContainer, isNotContainer; public Boolean isPrimitiveType, isContainer, isNotContainer;
public Boolean isString, isInteger, isLong, isFloat, isDouble, isByteArray, isBinary, isBoolean, isDate, isDateTime; public Boolean isString, isInteger, isLong, isFloat, isDouble, isByteArray, isBinary, isBoolean, isDate, isDateTime;
public Boolean isListContainer, isMapContainer; public Boolean isListContainer, isMapContainer;
@ -63,6 +64,7 @@ public class CodegenProperty {
result = prime * result + ((exclusiveMinimum == null) ? 0 : exclusiveMinimum.hashCode()); result = prime * result + ((exclusiveMinimum == null) ? 0 : exclusiveMinimum.hashCode());
result = prime * result + ((getter == null) ? 0 : getter.hashCode()); result = prime * result + ((getter == null) ? 0 : getter.hashCode());
result = prime * result + ((hasMore == null) ? 0 : hasMore.hashCode()); result = prime * result + ((hasMore == null) ? 0 : hasMore.hashCode());
result = prime * result + ((hasMoreNonReadOnly == null) ? 0 : hasMoreNonReadOnly.hashCode());
result = prime * result + ((isContainer == null) ? 0 : isContainer.hashCode()); result = prime * result + ((isContainer == null) ? 0 : isContainer.hashCode());
result = prime * result + (isEnum ? 1231 : 1237); result = prime * result + (isEnum ? 1231 : 1237);
result = prime * result + ((isNotContainer == null) ? 0 : isNotContainer.hashCode()); result = prime * result + ((isNotContainer == null) ? 0 : isNotContainer.hashCode());

View File

@ -2220,9 +2220,12 @@ public class DefaultCodegen {
} }
private void addVars(CodegenModel m, List<CodegenProperty> vars, Map<String, Property> properties, Set<String> mandatory) { private void addVars(CodegenModel m, List<CodegenProperty> vars, Map<String, Property> properties, Set<String> mandatory) {
final int totalCount = properties.size(); // convert set to list so that we can access the next entry in the loop
int count = 0; List<Map.Entry<String, Property>> propertyList = new ArrayList<Map.Entry<String, Property>>(properties.entrySet());
for (Map.Entry<String, Property> entry : properties.entrySet()) { final int totalCount = propertyList.size();
for (int i = 0; i < totalCount; i++) {
Map.Entry<String, Property> entry = propertyList.get(i);
final String key = entry.getKey(); final String key = entry.getKey();
final Property prop = entry.getValue(); final Property prop = entry.getValue();
@ -2236,13 +2239,19 @@ public class DefaultCodegen {
// m.hasEnums to be set incorrectly if allProperties has enumerations but properties does not. // m.hasEnums to be set incorrectly if allProperties has enumerations but properties does not.
m.hasEnums = true; m.hasEnums = true;
} }
count++;
if (count != totalCount) { if (i+1 != totalCount) {
cp.hasMore = true; cp.hasMore = true;
// check the next entry to see if it's read only
if (!Boolean.TRUE.equals(propertyList.get(i+1).getValue().getReadOnly())) {
cp.hasMoreNonReadOnly = true; // next entry is not ready only
}
} }
if (cp.isContainer != null) { if (cp.isContainer != null) {
addImport(m, typeMapping.get("array")); addImport(m, typeMapping.get("array"));
} }
addImport(m, cp.baseType); addImport(m, cp.baseType);
addImport(m, cp.complexType); addImport(m, cp.complexType);
vars.add(cp); vars.add(cp);

View File

@ -101,6 +101,7 @@ public abstract class AbstractCSharpCodegen extends DefaultCodegen implements Co
typeMapping = new HashMap<String, String>(); typeMapping = new HashMap<String, String>();
typeMapping.put("string", "string"); typeMapping.put("string", "string");
typeMapping.put("binary", "byte[]"); typeMapping.put("binary", "byte[]");
typeMapping.put("bytearray", "byte[]");
typeMapping.put("boolean", "bool?"); typeMapping.put("boolean", "bool?");
typeMapping.put("integer", "int?"); typeMapping.put("integer", "int?");
typeMapping.put("float", "float?"); typeMapping.put("float", "float?");

View File

@ -41,7 +41,7 @@ namespace {{packageName}}.Model
/// </summary> /// </summary>
{{#vars}}{{^isReadOnly}} /// <param name="{{name}}">{{#description}}{{description}}{{/description}}{{^description}}{{name}}{{/description}}{{#required}} (required){{/required}}{{#defaultValue}} (default to {{defaultValue}}){{/defaultValue}}.</param> {{#vars}}{{^isReadOnly}} /// <param name="{{name}}">{{#description}}{{description}}{{/description}}{{^description}}{{name}}{{/description}}{{#required}} (required){{/required}}{{#defaultValue}} (default to {{defaultValue}}){{/defaultValue}}.</param>
{{/isReadOnly}}{{/vars}} {{/isReadOnly}}{{/vars}}
public {{classname}}({{#vars}}{{^isReadOnly}}{{{datatypeWithEnum}}} {{name}} = null{{#hasMore}}, {{/hasMore}}{{/isReadOnly}}{{/vars}}) public {{classname}}({{#vars}}{{^isReadOnly}}{{{datatypeWithEnum}}} {{name}} = null{{#hasMoreNonReadOnly}}, {{/hasMoreNonReadOnly}}{{/isReadOnly}}{{/vars}})
{ {
{{#vars}}{{^isReadOnly}}{{#required}}// to ensure "{{name}}" is required (not null) {{#vars}}{{^isReadOnly}}{{#required}}// to ensure "{{name}}" is required (not null)
if ({{name}} == null) if ({{name}} == null)

View File

@ -1314,12 +1314,16 @@
}, },
"Name": { "Name": {
"description": "Model for testing model name same as property name", "description": "Model for testing model name same as property name",
"required": [
"name"
],
"properties": { "properties": {
"name": { "name": {
"type": "integer", "type": "integer",
"format": "int32" "format": "int32"
}, },
"snake_case": { "snake_case": {
"readOnly": true,
"type": "integer", "type": "integer",
"format": "int32" "format": "int32"
} }
@ -1375,6 +1379,59 @@
"type" : "string" "type" : "string"
} }
} }
},
"format_test" : {
"type" : "object",
"required": [
"number"
],
"properties" : {
"integer" : {
"type": "integer"
},
"int32" : {
"type": "integer",
"format": "int32"
},
"int64" : {
"type": "integer",
"format": "int64"
},
"number" : {
"type": "number"
},
"float" : {
"type": "number",
"format": "float"
},
"double" : {
"type": "number",
"format": "double"
},
"string" : {
"type": "string"
},
"byte" : {
"type": "string",
"format": "byte"
},
"binary" : {
"type": "string",
"format": "binary"
},
"date" : {
"type": "string",
"format": "date"
},
"dateTime" : {
"type": "string",
"format": "date-time"
},
"dateTime" : {
"type": "string",
"format": "password"
}
}
} }
} }
} }

View File

@ -0,0 +1,291 @@
using System;
using System.Linq;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Runtime.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
namespace IO.Swagger.Model
{
/// <summary>
///
/// </summary>
[DataContract]
public partial class FormatTest : IEquatable<FormatTest>
{
/// <summary>
/// Initializes a new instance of the <see cref="FormatTest" /> class.
/// Initializes a new instance of the <see cref="FormatTest" />class.
/// </summary>
/// <param name="Integer">Integer.</param>
/// <param name="Int32">Int32.</param>
/// <param name="Int64">Int64.</param>
/// <param name="Number">Number (required).</param>
/// <param name="_Float">_Float.</param>
/// <param name="_Double">_Double.</param>
/// <param name="_String">_String.</param>
/// <param name="_Byte">_Byte.</param>
/// <param name="Binary">Binary.</param>
/// <param name="Date">Date.</param>
/// <param name="DateTime">DateTime.</param>
public FormatTest(int? Integer = null, int? Int32 = null, long? Int64 = null, double? Number = null, float? _Float = null, double? _Double = null, string _String = null, byte[] _Byte = null, byte[] Binary = null, DateTime? Date = null, string DateTime = null)
{
// to ensure "Number" is required (not null)
if (Number == null)
{
throw new InvalidDataException("Number is a required property for FormatTest and cannot be null");
}
else
{
this.Number = Number;
}
this.Integer = Integer;
this.Int32 = Int32;
this.Int64 = Int64;
this._Float = _Float;
this._Double = _Double;
this._String = _String;
this._Byte = _Byte;
this.Binary = Binary;
this.Date = Date;
this.DateTime = DateTime;
}
/// <summary>
/// Gets or Sets Integer
/// </summary>
[DataMember(Name="integer", EmitDefaultValue=false)]
public int? Integer { get; set; }
/// <summary>
/// Gets or Sets Int32
/// </summary>
[DataMember(Name="int32", EmitDefaultValue=false)]
public int? Int32 { get; set; }
/// <summary>
/// Gets or Sets Int64
/// </summary>
[DataMember(Name="int64", EmitDefaultValue=false)]
public long? Int64 { get; set; }
/// <summary>
/// Gets or Sets Number
/// </summary>
[DataMember(Name="number", EmitDefaultValue=false)]
public double? Number { get; set; }
/// <summary>
/// Gets or Sets _Float
/// </summary>
[DataMember(Name="float", EmitDefaultValue=false)]
public float? _Float { get; set; }
/// <summary>
/// Gets or Sets _Double
/// </summary>
[DataMember(Name="double", EmitDefaultValue=false)]
public double? _Double { get; set; }
/// <summary>
/// Gets or Sets _String
/// </summary>
[DataMember(Name="string", EmitDefaultValue=false)]
public string _String { get; set; }
/// <summary>
/// Gets or Sets _Byte
/// </summary>
[DataMember(Name="byte", EmitDefaultValue=false)]
public byte[] _Byte { get; set; }
/// <summary>
/// Gets or Sets Binary
/// </summary>
[DataMember(Name="binary", EmitDefaultValue=false)]
public byte[] Binary { get; set; }
/// <summary>
/// Gets or Sets Date
/// </summary>
[DataMember(Name="date", EmitDefaultValue=false)]
public DateTime? Date { get; set; }
/// <summary>
/// Gets or Sets DateTime
/// </summary>
[DataMember(Name="dateTime", EmitDefaultValue=false)]
public string DateTime { get; set; }
/// <summary>
/// Returns the string presentation of the object
/// </summary>
/// <returns>String presentation of the object</returns>
public override string ToString()
{
var sb = new StringBuilder();
sb.Append("class FormatTest {\n");
sb.Append(" Integer: ").Append(Integer).Append("\n");
sb.Append(" Int32: ").Append(Int32).Append("\n");
sb.Append(" Int64: ").Append(Int64).Append("\n");
sb.Append(" Number: ").Append(Number).Append("\n");
sb.Append(" _Float: ").Append(_Float).Append("\n");
sb.Append(" _Double: ").Append(_Double).Append("\n");
sb.Append(" _String: ").Append(_String).Append("\n");
sb.Append(" _Byte: ").Append(_Byte).Append("\n");
sb.Append(" Binary: ").Append(Binary).Append("\n");
sb.Append(" Date: ").Append(Date).Append("\n");
sb.Append(" DateTime: ").Append(DateTime).Append("\n");
sb.Append("}\n");
return sb.ToString();
}
/// <summary>
/// Returns the JSON string presentation of the object
/// </summary>
/// <returns>JSON string presentation of the object</returns>
public string ToJson()
{
return JsonConvert.SerializeObject(this, Formatting.Indented);
}
/// <summary>
/// Returns true if objects are equal
/// </summary>
/// <param name="obj">Object to be compared</param>
/// <returns>Boolean</returns>
public override bool Equals(object obj)
{
// credit: http://stackoverflow.com/a/10454552/677735
return this.Equals(obj as FormatTest);
}
/// <summary>
/// Returns true if FormatTest instances are equal
/// </summary>
/// <param name="other">Instance of FormatTest to be compared</param>
/// <returns>Boolean</returns>
public bool Equals(FormatTest other)
{
// credit: http://stackoverflow.com/a/10454552/677735
if (other == null)
return false;
return
(
this.Integer == other.Integer ||
this.Integer != null &&
this.Integer.Equals(other.Integer)
) &&
(
this.Int32 == other.Int32 ||
this.Int32 != null &&
this.Int32.Equals(other.Int32)
) &&
(
this.Int64 == other.Int64 ||
this.Int64 != null &&
this.Int64.Equals(other.Int64)
) &&
(
this.Number == other.Number ||
this.Number != null &&
this.Number.Equals(other.Number)
) &&
(
this._Float == other._Float ||
this._Float != null &&
this._Float.Equals(other._Float)
) &&
(
this._Double == other._Double ||
this._Double != null &&
this._Double.Equals(other._Double)
) &&
(
this._String == other._String ||
this._String != null &&
this._String.Equals(other._String)
) &&
(
this._Byte == other._Byte ||
this._Byte != null &&
this._Byte.Equals(other._Byte)
) &&
(
this.Binary == other.Binary ||
this.Binary != null &&
this.Binary.Equals(other.Binary)
) &&
(
this.Date == other.Date ||
this.Date != null &&
this.Date.Equals(other.Date)
) &&
(
this.DateTime == other.DateTime ||
this.DateTime != null &&
this.DateTime.Equals(other.DateTime)
);
}
/// <summary>
/// Gets the hash code
/// </summary>
/// <returns>Hash code</returns>
public override int GetHashCode()
{
// credit: http://stackoverflow.com/a/263416/677735
unchecked // Overflow is fine, just wrap
{
int hash = 41;
// Suitable nullity checks etc, of course :)
if (this.Integer != null)
hash = hash * 59 + this.Integer.GetHashCode();
if (this.Int32 != null)
hash = hash * 59 + this.Int32.GetHashCode();
if (this.Int64 != null)
hash = hash * 59 + this.Int64.GetHashCode();
if (this.Number != null)
hash = hash * 59 + this.Number.GetHashCode();
if (this._Float != null)
hash = hash * 59 + this._Float.GetHashCode();
if (this._Double != null)
hash = hash * 59 + this._Double.GetHashCode();
if (this._String != null)
hash = hash * 59 + this._String.GetHashCode();
if (this._Byte != null)
hash = hash * 59 + this._Byte.GetHashCode();
if (this.Binary != null)
hash = hash * 59 + this.Binary.GetHashCode();
if (this.Date != null)
hash = hash * 59 + this.Date.GetHashCode();
if (this.DateTime != null)
hash = hash * 59 + this.DateTime.GetHashCode();
return hash;
}
}
}
}

View File

@ -22,13 +22,19 @@ namespace IO.Swagger.Model
/// Initializes a new instance of the <see cref="Name" /> class. /// Initializes a new instance of the <see cref="Name" /> class.
/// Initializes a new instance of the <see cref="Name" />class. /// Initializes a new instance of the <see cref="Name" />class.
/// </summary> /// </summary>
/// <param name="_Name">_Name.</param> /// <param name="_Name">_Name (required).</param>
/// <param name="SnakeCase">SnakeCase.</param>
public Name(int? _Name = null, int? SnakeCase = null) public Name(int? _Name = null)
{ {
this._Name = _Name; // to ensure "_Name" is required (not null)
this.SnakeCase = SnakeCase; if (_Name == null)
{
throw new InvalidDataException("_Name is a required property for Name and cannot be null");
}
else
{
this._Name = _Name;
}
} }
@ -43,7 +49,7 @@ namespace IO.Swagger.Model
/// Gets or Sets SnakeCase /// Gets or Sets SnakeCase
/// </summary> /// </summary>
[DataMember(Name="snake_case", EmitDefaultValue=false)] [DataMember(Name="snake_case", EmitDefaultValue=false)]
public int? SnakeCase { get; set; } public int? SnakeCase { get; private set; }
/// <summary> /// <summary>
/// Returns the string presentation of the object /// Returns the string presentation of the object

View File

@ -66,6 +66,10 @@
<Compile Include="Lib\SwaggerClient\src\main\csharp\IO\Swagger\Model\Name.cs" /> <Compile Include="Lib\SwaggerClient\src\main\csharp\IO\Swagger\Model\Name.cs" />
<Compile Include="Lib\SwaggerClient\src\main\csharp\IO\Swagger\Model\Task.cs" /> <Compile Include="Lib\SwaggerClient\src\main\csharp\IO\Swagger\Model\Task.cs" />
<Compile Include="Lib\SwaggerClient\src\main\csharp\IO\Swagger\Model\Model200Response.cs" /> <Compile Include="Lib\SwaggerClient\src\main\csharp\IO\Swagger\Model\Model200Response.cs" />
<Compile Include="Lib\SwaggerClient\src\main\csharp\IO\Swagger\Model\Animal.cs" />
<Compile Include="Lib\SwaggerClient\src\main\csharp\IO\Swagger\Model\Cat.cs" />
<Compile Include="Lib\SwaggerClient\src\main\csharp\IO\Swagger\Model\Dog.cs" />
<Compile Include="Lib\SwaggerClient\src\main\csharp\IO\Swagger\Model\FormatTest.cs" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup> <ItemGroup>

View File

@ -1,9 +1,11 @@
<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="Lib/SwaggerClient/src/main/csharp/IO/Swagger/Model/Name.cs">
<Files> <Files>
<File FileName="TestPet.cs" Line="23" Column="17" /> <File FileName="TestPet.cs" Line="1" Column="1" />
<File FileName="TestOrder.cs" Line="1" Column="1" /> <File FileName="TestOrder.cs" Line="1" Column="1" />
<File FileName="Lib/SwaggerClient/src/main/csharp/IO/Swagger/Model/Cat.cs" Line="1" Column="1" />
<File FileName="Lib/SwaggerClient/src/main/csharp/IO/Swagger/Model/Name.cs" Line="18" Column="50" />
</Files> </Files>
</MonoDevelop.Ide.Workbench> </MonoDevelop.Ide.Workbench>
<MonoDevelop.Ide.DebuggingService.Breakpoints> <MonoDevelop.Ide.DebuggingService.Breakpoints>