Merge pull request #3187 from swagger-api/model_propert_same_name

[C#] Fix model with only read-only attributes and model & property using the same name
This commit is contained in:
wing328
2016-06-21 21:59:36 +08:00
committed by GitHub
14 changed files with 293 additions and 11 deletions

View File

@@ -37,6 +37,7 @@ public class CodegenModel {
public Set<String> imports = new TreeSet<String>();
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<String, Object> vendorExtensions;

View File

@@ -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

View File

@@ -259,7 +259,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;
}
}
@@ -615,8 +615,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 {

View File

@@ -25,11 +25,13 @@
{{/isEnum}}
{{/vars}}
{{#hasRequired}}
{{^hasOnlyReadOnly}}
/// <summary>
/// Initializes a new instance of the <see cref="{{classname}}" /> class.
/// </summary>
[JsonConstructorAttribute]
protected {{classname}}() { }
{{/hasOnlyReadOnly}}
{{/hasRequired}}
/// <summary>
/// Initializes a new instance of the <see cref="{{classname}}" /> class.
@@ -39,6 +41,9 @@
/// <param name="{{name}}">{{#description}}{{description}}{{/description}}{{^description}}{{name}}{{/description}}{{#required}} (required){{/required}}{{#defaultValue}} (default to {{defaultValue}}){{/defaultValue}}.</param>
{{/isReadOnly}}
{{/vars}}
{{#hasOnlyReadOnly}}
[JsonConstructorAttribute]
{{/hasOnlyReadOnly}}
public {{classname}}({{#readWriteVars}}{{{datatypeWithEnum}}}{{#isEnum}}?{{/isEnum}} {{name}} = null{{^-last}}, {{/-last}}{{/readWriteVars}})
{
{{#vars}}

View File

@@ -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:

View File

@@ -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

View File

@@ -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)

View File

@@ -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)

View File

@@ -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)

View File

@@ -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
{
/// <summary>
/// Class for testing HasOnlyReadOnly
/// </summary>
/// <remarks>
/// This file is automatically generated by Swagger Codegen.
/// Please update the test case below to test the model.
/// </remarks>
[TestFixture]
public class HasOnlyReadOnlyTests
{
// TODO uncomment below to declare an instance variable for HasOnlyReadOnly
//private HasOnlyReadOnly instance;
/// <summary>
/// Setup before each test
/// </summary>
[SetUp]
public void Init()
{
// TODO uncomment below to create an instance of HasOnlyReadOnly
//instance = new HasOnlyReadOnly();
}
/// <summary>
/// Clean up after each test
/// </summary>
[TearDown]
public void Cleanup()
{
}
/// <summary>
/// Test an instance of HasOnlyReadOnly
/// </summary>
[Test]
public void HasOnlyReadOnlyInstanceTest()
{
// TODO uncomment below to test "IsInstanceOfType" HasOnlyReadOnly
//Assert.IsInstanceOfType<HasOnlyReadOnly> (instance, "variable 'instance' is a HasOnlyReadOnly");
}
/// <summary>
/// Test the property 'Bar'
/// </summary>
[Test]
public void BarTest()
{
// TODO unit test for the property 'Bar'
}
/// <summary>
/// Test the property 'Foo'
/// </summary>
[Test]
public void FooTest()
{
// TODO unit test for the property 'Foo'
}
}
}

View File

@@ -24,7 +24,7 @@ limitations under the License.
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{BF42B49D-37A0-49C4-A405-24CD946ADAA7}</ProjectGuid>
<ProjectGuid>{EC130B5E-75A2-4FA3-BEE2-634A4FFF231C}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Swagger Library</RootNamespace>

View File

@@ -42,6 +42,7 @@ namespace IO.Swagger.Model
/// <summary>
/// Initializes a new instance of the <see cref="AnimalFarm" /> class.
/// </summary>
[JsonConstructorAttribute]
public AnimalFarm()
{
}

View File

@@ -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
{
/// <summary>
/// HasOnlyReadOnly
/// </summary>
[DataContract]
public partial class HasOnlyReadOnly : IEquatable<HasOnlyReadOnly>
{
/// <summary>
/// Initializes a new instance of the <see cref="HasOnlyReadOnly" /> class.
/// </summary>
[JsonConstructorAttribute]
public HasOnlyReadOnly()
{
}
/// <summary>
/// Gets or Sets Bar
/// </summary>
[DataMember(Name="bar", EmitDefaultValue=false)]
public string Bar { get; private set; }
/// <summary>
/// Gets or Sets Foo
/// </summary>
[DataMember(Name="foo", EmitDefaultValue=false)]
public string Foo { get; private 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 HasOnlyReadOnly {\n");
sb.Append(" Bar: ").Append(Bar).Append("\n");
sb.Append(" Foo: ").Append(Foo).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 HasOnlyReadOnly);
}
/// <summary>
/// Returns true if HasOnlyReadOnly instances are equal
/// </summary>
/// <param name="other">Instance of HasOnlyReadOnly to be compared</param>
/// <returns>Boolean</returns>
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)
);
}
/// <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.Bar != null)
hash = hash * 59 + this.Bar.GetHashCode();
if (this.Foo != null)
hash = hash * 59 + this.Foo.GetHashCode();
return hash;
}
}
}
}

View File

@@ -43,9 +43,11 @@ namespace IO.Swagger.Model
/// Initializes a new instance of the <see cref="Model200Response" /> class.
/// </summary>
/// <param name="Name">Name.</param>
public Model200Response(int? Name = null)
/// <param name="_Class">_Class.</param>
public Model200Response(int? Name = null, string _Class = null)
{
this.Name = Name;
this._Class = _Class;
}
/// <summary>
@@ -54,6 +56,11 @@ namespace IO.Swagger.Model
[DataMember(Name="name", EmitDefaultValue=false)]
public int? Name { get; set; }
/// <summary>
/// Gets or Sets _Class
/// </summary>
[DataMember(Name="class", EmitDefaultValue=false)]
public string _Class { get; set; }
/// <summary>
/// Returns the string presentation of the object
/// </summary>
/// <returns>String presentation of the object</returns>
@@ -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;
}
}