From 3e302918653f2ff1658ac0e46332e6b6d5184286 Mon Sep 17 00:00:00 2001 From: wing328 Date: Tue, 21 Jun 2016 16:16:05 +0800 Subject: [PATCH] fix #3184 --- .../java/io/swagger/codegen/CodegenModel.java | 1 + .../io/swagger/codegen/DefaultCodegen.java | 5 + .../languages/AbstractCSharpCodegen.java | 4 +- .../resources/csharp/modelGeneric.mustache | 5 + ...ith-fake-endpoints-models-for-testing.yaml | 9 ++ .../csharp/SwaggerClient/IO.Swagger.sln | 10 +- .../petstore/csharp/SwaggerClient/README.md | 3 +- .../SwaggerClient/docs/HasOnlyReadOnly.md | 10 ++ .../SwaggerClient/docs/Model200Response.md | 1 + .../Model/HasOnlyReadOnlyTests.cs | 98 +++++++++++++ .../src/IO.Swagger/IO.Swagger.csproj | 2 +- .../src/IO.Swagger/Model/AnimalFarm.cs | 1 + .../src/IO.Swagger/Model/HasOnlyReadOnly.cs | 138 ++++++++++++++++++ .../src/IO.Swagger/Model/Model200Response.cs | 17 ++- 14 files changed, 293 insertions(+), 11 deletions(-) create mode 100644 samples/client/petstore/csharp/SwaggerClient/docs/HasOnlyReadOnly.md create mode 100644 samples/client/petstore/csharp/SwaggerClient/src/IO.Swagger.Test/Model/HasOnlyReadOnlyTests.cs create mode 100644 samples/client/petstore/csharp/SwaggerClient/src/IO.Swagger/Model/HasOnlyReadOnly.cs diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenModel.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenModel.java index 3752f69dca5c..0f4608a133ad 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenModel.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenModel.java @@ -37,6 +37,7 @@ public class CodegenModel { public Set imports = new TreeSet(); public Boolean hasVars, emptyVars, hasMoreModels, hasEnums, isEnum, hasRequired, isArrayModel; + public Boolean hasOnlyReadOnly = true; // true if all properties are read-only public ExternalDocs externalDocs; public Map vendorExtensions; diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java index 0bd0c08cccc2..297632d83e6b 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java @@ -2596,6 +2596,11 @@ public class DefaultCodegen { m.hasEnums = true; } + // set model's hasOnlyReadOnly to false if the property is read-only + if (!Boolean.TRUE.equals(cp.isReadOnly)) { + m.hasOnlyReadOnly = false; + } + if (i+1 != totalCount) { cp.hasMore = true; // check the next entry to see if it's read only diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractCSharpCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractCSharpCodegen.java index 386e37e3f0df..cf5a5c181f3e 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractCSharpCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractCSharpCodegen.java @@ -219,7 +219,7 @@ public abstract class AbstractCSharpCodegen extends DefaultCodegen implements Co // check to see if model name is same as the property name // which will result in compilation error // if found, prepend with _ to workaround the limitation - if (var.name.equals(cm.name)) { + if (var.name.equalsIgnoreCase(cm.name)) { var.name = "_" + var.name; } } @@ -555,8 +555,6 @@ public abstract class AbstractCSharpCodegen extends DefaultCodegen implements Co enumName = camelize(enumName) + "Enum"; - LOGGER.info("toEnumVarName = " + enumName); - if (enumName.matches("\\d.*")) { // starts with number return "_" + enumName; } else { diff --git a/modules/swagger-codegen/src/main/resources/csharp/modelGeneric.mustache b/modules/swagger-codegen/src/main/resources/csharp/modelGeneric.mustache index c8a00373ff2c..c8f0511673a9 100644 --- a/modules/swagger-codegen/src/main/resources/csharp/modelGeneric.mustache +++ b/modules/swagger-codegen/src/main/resources/csharp/modelGeneric.mustache @@ -25,11 +25,13 @@ {{/isEnum}} {{/vars}} {{#hasRequired}} + {{^hasOnlyReadOnly}} /// /// Initializes a new instance of the class. /// [JsonConstructorAttribute] protected {{classname}}() { } + {{/hasOnlyReadOnly}} {{/hasRequired}} /// /// Initializes a new instance of the class. @@ -39,6 +41,9 @@ /// {{#description}}{{description}}{{/description}}{{^description}}{{name}}{{/description}}{{#required}} (required){{/required}}{{#defaultValue}} (default to {{defaultValue}}){{/defaultValue}}. {{/isReadOnly}} {{/vars}} + {{#hasOnlyReadOnly}} + [JsonConstructorAttribute] + {{/hasOnlyReadOnly}} public {{classname}}({{#readWriteVars}}{{{datatypeWithEnum}}}{{#isEnum}}?{{/isEnum}} {{name}} = null{{^-last}}, {{/-last}}{{/readWriteVars}}) { {{#vars}} diff --git a/modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml b/modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml index 615cb5a10fe8..07b97000e814 100644 --- a/modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml +++ b/modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml @@ -985,6 +985,15 @@ definitions: readOnly: true baz: type: string + hasOnlyReadOnly: + type: object + properties: + bar: + type: string + readOnly: true + foo: + type: string + readOnly: true ArrayTest: type: object properties: diff --git a/samples/client/petstore/csharp/SwaggerClient/IO.Swagger.sln b/samples/client/petstore/csharp/SwaggerClient/IO.Swagger.sln index 143ffeb1629f..e3f3ebc3d954 100644 --- a/samples/client/petstore/csharp/SwaggerClient/IO.Swagger.sln +++ b/samples/client/petstore/csharp/SwaggerClient/IO.Swagger.sln @@ -2,7 +2,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 2012 VisualStudioVersion = 12.0.0.0 MinimumVisualStudioVersion = 10.0.0.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IO.Swagger", "src\IO.Swagger\IO.Swagger.csproj", "{BF42B49D-37A0-49C4-A405-24CD946ADAA7}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IO.Swagger", "src\IO.Swagger\IO.Swagger.csproj", "{EC130B5E-75A2-4FA3-BEE2-634A4FFF231C}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IO.Swagger.Test", "src\IO.Swagger.Test\IO.Swagger.Test.csproj", "{19F1DEBC-DE5E-4517-8062-F000CD499087}" EndProject @@ -12,10 +12,10 @@ Debug|Any CPU = Debug|Any CPU Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution -{BF42B49D-37A0-49C4-A405-24CD946ADAA7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU -{BF42B49D-37A0-49C4-A405-24CD946ADAA7}.Debug|Any CPU.Build.0 = Debug|Any CPU -{BF42B49D-37A0-49C4-A405-24CD946ADAA7}.Release|Any CPU.ActiveCfg = Release|Any CPU -{BF42B49D-37A0-49C4-A405-24CD946ADAA7}.Release|Any CPU.Build.0 = Release|Any CPU +{EC130B5E-75A2-4FA3-BEE2-634A4FFF231C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU +{EC130B5E-75A2-4FA3-BEE2-634A4FFF231C}.Debug|Any CPU.Build.0 = Debug|Any CPU +{EC130B5E-75A2-4FA3-BEE2-634A4FFF231C}.Release|Any CPU.ActiveCfg = Release|Any CPU +{EC130B5E-75A2-4FA3-BEE2-634A4FFF231C}.Release|Any CPU.Build.0 = Release|Any CPU {19F1DEBC-DE5E-4517-8062-F000CD499087}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {19F1DEBC-DE5E-4517-8062-F000CD499087}.Debug|Any CPU.Build.0 = Debug|Any CPU {19F1DEBC-DE5E-4517-8062-F000CD499087}.Release|Any CPU.ActiveCfg = Release|Any CPU diff --git a/samples/client/petstore/csharp/SwaggerClient/README.md b/samples/client/petstore/csharp/SwaggerClient/README.md index c5a53af127dc..9d4eeddd49e8 100644 --- a/samples/client/petstore/csharp/SwaggerClient/README.md +++ b/samples/client/petstore/csharp/SwaggerClient/README.md @@ -6,7 +6,7 @@ This C# SDK is automatically generated by the [Swagger Codegen](https://github.c - API version: 1.0.0 - SDK version: 1.0.0 -- Build date: 2016-06-12T16:29:47.553+08:00 +- Build date: 2016-06-21T16:06:28.848+08:00 - Build package: class io.swagger.codegen.languages.CSharpClientCodegen ## Frameworks supported @@ -123,6 +123,7 @@ Class | Method | HTTP request | Description - [Model.EnumClass](docs/EnumClass.md) - [Model.EnumTest](docs/EnumTest.md) - [Model.FormatTest](docs/FormatTest.md) + - [Model.HasOnlyReadOnly](docs/HasOnlyReadOnly.md) - [Model.MixedPropertiesAndAdditionalPropertiesClass](docs/MixedPropertiesAndAdditionalPropertiesClass.md) - [Model.Model200Response](docs/Model200Response.md) - [Model.ModelReturn](docs/ModelReturn.md) diff --git a/samples/client/petstore/csharp/SwaggerClient/docs/HasOnlyReadOnly.md b/samples/client/petstore/csharp/SwaggerClient/docs/HasOnlyReadOnly.md new file mode 100644 index 000000000000..cf0190498b96 --- /dev/null +++ b/samples/client/petstore/csharp/SwaggerClient/docs/HasOnlyReadOnly.md @@ -0,0 +1,10 @@ +# IO.Swagger.Model.HasOnlyReadOnly +## Properties + +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- +**Bar** | **string** | | [optional] +**Foo** | **string** | | [optional] + +[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md) + diff --git a/samples/client/petstore/csharp/SwaggerClient/docs/Model200Response.md b/samples/client/petstore/csharp/SwaggerClient/docs/Model200Response.md index 5cd7e66ea7ea..cfaddb67027e 100644 --- a/samples/client/petstore/csharp/SwaggerClient/docs/Model200Response.md +++ b/samples/client/petstore/csharp/SwaggerClient/docs/Model200Response.md @@ -4,6 +4,7 @@ Name | Type | Description | Notes ------------ | ------------- | ------------- | ------------- **Name** | **int?** | | [optional] +**_Class** | **string** | | [optional] [[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md) diff --git a/samples/client/petstore/csharp/SwaggerClient/src/IO.Swagger.Test/Model/HasOnlyReadOnlyTests.cs b/samples/client/petstore/csharp/SwaggerClient/src/IO.Swagger.Test/Model/HasOnlyReadOnlyTests.cs new file mode 100644 index 000000000000..a107844bc821 --- /dev/null +++ b/samples/client/petstore/csharp/SwaggerClient/src/IO.Swagger.Test/Model/HasOnlyReadOnlyTests.cs @@ -0,0 +1,98 @@ +/* + * Swagger Petstore + * + * This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ + * + * OpenAPI spec version: 1.0.0 + * Contact: apiteam@swagger.io + * Generated by: https://github.com/swagger-api/swagger-codegen.git + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +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; + +namespace IO.Swagger.Test +{ + /// + /// Class for testing HasOnlyReadOnly + /// + /// + /// This file is automatically generated by Swagger Codegen. + /// Please update the test case below to test the model. + /// + [TestFixture] + public class HasOnlyReadOnlyTests + { + // TODO uncomment below to declare an instance variable for HasOnlyReadOnly + //private HasOnlyReadOnly instance; + + /// + /// Setup before each test + /// + [SetUp] + public void Init() + { + // TODO uncomment below to create an instance of HasOnlyReadOnly + //instance = new HasOnlyReadOnly(); + } + + /// + /// Clean up after each test + /// + [TearDown] + public void Cleanup() + { + + } + + /// + /// Test an instance of HasOnlyReadOnly + /// + [Test] + public void HasOnlyReadOnlyInstanceTest() + { + // TODO uncomment below to test "IsInstanceOfType" HasOnlyReadOnly + //Assert.IsInstanceOfType (instance, "variable 'instance' is a HasOnlyReadOnly"); + } + + /// + /// Test the property 'Bar' + /// + [Test] + public void BarTest() + { + // TODO unit test for the property 'Bar' + } + /// + /// Test the property 'Foo' + /// + [Test] + public void FooTest() + { + // TODO unit test for the property 'Foo' + } + + } + +} diff --git a/samples/client/petstore/csharp/SwaggerClient/src/IO.Swagger/IO.Swagger.csproj b/samples/client/petstore/csharp/SwaggerClient/src/IO.Swagger/IO.Swagger.csproj index 7a78c9e7e693..5ad82368d710 100644 --- a/samples/client/petstore/csharp/SwaggerClient/src/IO.Swagger/IO.Swagger.csproj +++ b/samples/client/petstore/csharp/SwaggerClient/src/IO.Swagger/IO.Swagger.csproj @@ -24,7 +24,7 @@ limitations under the License. Debug AnyCPU - {BF42B49D-37A0-49C4-A405-24CD946ADAA7} + {EC130B5E-75A2-4FA3-BEE2-634A4FFF231C} Library Properties Swagger Library diff --git a/samples/client/petstore/csharp/SwaggerClient/src/IO.Swagger/Model/AnimalFarm.cs b/samples/client/petstore/csharp/SwaggerClient/src/IO.Swagger/Model/AnimalFarm.cs index 267ab5f40179..bc2443d5327f 100644 --- a/samples/client/petstore/csharp/SwaggerClient/src/IO.Swagger/Model/AnimalFarm.cs +++ b/samples/client/petstore/csharp/SwaggerClient/src/IO.Swagger/Model/AnimalFarm.cs @@ -42,6 +42,7 @@ namespace IO.Swagger.Model /// /// Initializes a new instance of the class. /// + [JsonConstructorAttribute] public AnimalFarm() { } diff --git a/samples/client/petstore/csharp/SwaggerClient/src/IO.Swagger/Model/HasOnlyReadOnly.cs b/samples/client/petstore/csharp/SwaggerClient/src/IO.Swagger/Model/HasOnlyReadOnly.cs new file mode 100644 index 000000000000..96d150565a5f --- /dev/null +++ b/samples/client/petstore/csharp/SwaggerClient/src/IO.Swagger/Model/HasOnlyReadOnly.cs @@ -0,0 +1,138 @@ +/* + * Swagger Petstore + * + * This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ + * + * OpenAPI spec version: 1.0.0 + * Contact: apiteam@swagger.io + * Generated by: https://github.com/swagger-api/swagger-codegen.git + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +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 +{ + /// + /// HasOnlyReadOnly + /// + [DataContract] + public partial class HasOnlyReadOnly : IEquatable + { + /// + /// Initializes a new instance of the class. + /// + [JsonConstructorAttribute] + public HasOnlyReadOnly() + { + } + + /// + /// Gets or Sets Bar + /// + [DataMember(Name="bar", EmitDefaultValue=false)] + public string Bar { get; private set; } + /// + /// Gets or Sets Foo + /// + [DataMember(Name="foo", EmitDefaultValue=false)] + public string Foo { get; private set; } + /// + /// Returns the string presentation of the object + /// + /// String presentation of the object + public override string ToString() + { + var sb = new StringBuilder(); + sb.Append("class HasOnlyReadOnly {\n"); + sb.Append(" Bar: ").Append(Bar).Append("\n"); + sb.Append(" Foo: ").Append(Foo).Append("\n"); + sb.Append("}\n"); + return sb.ToString(); + } + + /// + /// Returns the JSON string presentation of the object + /// + /// JSON string presentation of the object + public string ToJson() + { + return JsonConvert.SerializeObject(this, Formatting.Indented); + } + + /// + /// Returns true if objects are equal + /// + /// Object to be compared + /// Boolean + public override bool Equals(object obj) + { + // credit: http://stackoverflow.com/a/10454552/677735 + return this.Equals(obj as HasOnlyReadOnly); + } + + /// + /// Returns true if HasOnlyReadOnly instances are equal + /// + /// Instance of HasOnlyReadOnly to be compared + /// Boolean + public bool Equals(HasOnlyReadOnly other) + { + // credit: http://stackoverflow.com/a/10454552/677735 + if (other == null) + return false; + + return + ( + this.Bar == other.Bar || + this.Bar != null && + this.Bar.Equals(other.Bar) + ) && + ( + this.Foo == other.Foo || + this.Foo != null && + this.Foo.Equals(other.Foo) + ); + } + + /// + /// Gets the hash code + /// + /// Hash code + 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.Bar != null) + hash = hash * 59 + this.Bar.GetHashCode(); + if (this.Foo != null) + hash = hash * 59 + this.Foo.GetHashCode(); + return hash; + } + } + } + +} diff --git a/samples/client/petstore/csharp/SwaggerClient/src/IO.Swagger/Model/Model200Response.cs b/samples/client/petstore/csharp/SwaggerClient/src/IO.Swagger/Model/Model200Response.cs index 7e6d1c79a16e..695c75096b47 100644 --- a/samples/client/petstore/csharp/SwaggerClient/src/IO.Swagger/Model/Model200Response.cs +++ b/samples/client/petstore/csharp/SwaggerClient/src/IO.Swagger/Model/Model200Response.cs @@ -43,9 +43,11 @@ namespace IO.Swagger.Model /// Initializes a new instance of the class. /// /// Name. - public Model200Response(int? Name = null) + /// _Class. + public Model200Response(int? Name = null, string _Class = null) { this.Name = Name; + this._Class = _Class; } /// @@ -54,6 +56,11 @@ namespace IO.Swagger.Model [DataMember(Name="name", EmitDefaultValue=false)] public int? Name { get; set; } /// + /// Gets or Sets _Class + /// + [DataMember(Name="class", EmitDefaultValue=false)] + public string _Class { get; set; } + /// /// Returns the string presentation of the object /// /// String presentation of the object @@ -62,6 +69,7 @@ namespace IO.Swagger.Model var sb = new StringBuilder(); sb.Append("class Model200Response {\n"); sb.Append(" Name: ").Append(Name).Append("\n"); + sb.Append(" _Class: ").Append(_Class).Append("\n"); sb.Append("}\n"); return sb.ToString(); } @@ -102,6 +110,11 @@ namespace IO.Swagger.Model this.Name == other.Name || this.Name != null && this.Name.Equals(other.Name) + ) && + ( + this._Class == other._Class || + this._Class != null && + this._Class.Equals(other._Class) ); } @@ -118,6 +131,8 @@ namespace IO.Swagger.Model // Suitable nullity checks etc, of course :) if (this.Name != null) hash = hash * 59 + this.Name.GetHashCode(); + if (this._Class != null) + hash = hash * 59 + this._Class.GetHashCode(); return hash; } }