Skip to content

Commit

Permalink
Use CompareNetObject for object comparison in C# client (refactor) (#…
Browse files Browse the repository at this point in the history
…1765)

* add pet, array of array test, use CompareNetObject for comparision

* add an option, should trigger ci failure

* update doc

* use useCompareNetObjects in csharp-refactor petstore

* fix project template
  • Loading branch information
wing328 authored Dec 29, 2018
1 parent 06a67ce commit 1db105b
Show file tree
Hide file tree
Showing 70 changed files with 731 additions and 1,053 deletions.
2 changes: 1 addition & 1 deletion bin/csharp-refactor-petstore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fi

# if you've executed sbt assembly previously it will use that instead.
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
ags="generate -t modules/openapi-generator/src/main/resources/csharp-refactor/ -i modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -g csharp-refactor -o samples/client/petstore/csharp-refactor/OpenAPIClient --additional-properties packageGuid={321C8C3F-0156-40C1-AE42-D59761FB9B6C} $@"
ags="generate -t modules/openapi-generator/src/main/resources/csharp-refactor/ -i modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -g csharp-refactor -o samples/client/petstore/csharp-refactor/OpenAPIClient --additional-properties packageGuid={321C8C3F-0156-40C1-AE42-D59761FB9B6C},useCompareNetObjects=true $@"

java $JAVA_OPTS -jar $executable $ags

Expand Down
3 changes: 3 additions & 0 deletions docs/generators/csharp.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,7 @@ CONFIG OPTIONS for csharp
validatable
Generates self-validatable models. (Default: true)

useCompareNetObjects
Use KellermanSoftware.CompareNetObjects for deep recursive object comparison. WARNING: this option incurs potential performance impact. (Default: false)

Back to the [generators list](README.md)
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,7 @@ public static enum ENUM_PROPERTY_NAMING_TYPE {camelCase, PascalCase, snake_case,
public static final String ENABLE_POST_PROCESS_FILE_DESC = "Enable post-processing file using environment variables.";

public static final String OPEN_API_SPEC_NAME = "openAPISpecName";

public static final String USE_COMPARE_NET_OBJECTS = "useCompareNetObjects";
public static final String USE_COMPARE_NET_OBJECTS_DESC = "Use KellermanSoftware.CompareNetObjects for deep recursive object comparison. WARNING: this option incurs potential performance impact.";
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ public class CSharpClientCodegen extends AbstractCSharpCodegen {
// By default, generated code is considered public
protected boolean nonPublicApi = Boolean.FALSE;

// use KellermanSoftware.CompareNetObjects for deep recursive object comparision
protected boolean useCompareNetObjects = Boolean.FALSE;

public CSharpClientCodegen() {
super();
supportsInheritance = true;
Expand Down Expand Up @@ -196,6 +199,10 @@ public CSharpClientCodegen() {
CodegenConstants.VALIDATABLE_DESC,
this.validatable);

addSwitch(CodegenConstants.USE_COMPARE_NET_OBJECTS,
CodegenConstants.USE_COMPARE_NET_OBJECTS_DESC,
this.useCompareNetObjects);

regexModifiers = new HashMap<Character, String>();
regexModifiers.put('i', "IgnoreCase");
regexModifiers.put('m', "Multiline");
Expand Down Expand Up @@ -798,6 +805,10 @@ public void setGeneratePropertyChanged(final Boolean generatePropertyChanged) {
this.generatePropertyChanged = generatePropertyChanged;
}

public void setUseCompareNetObjects(final Boolean useCompareNetObjects) {
this.useCompareNetObjects = useCompareNetObjects;
}

public boolean isNonPublicApi() {
return nonPublicApi;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
{{#useCompareNetObjects}}
using KellermanSoftware.CompareNetObjects;
{{/useCompareNetObjects}}

namespace {{packageName}}.Client
{
Expand All @@ -17,6 +20,21 @@ namespace {{packageName}}.Client
/// </summary>
public static class ClientUtils
{
{{#useCompareNetObjects}}
/// <summary>
/// An instance of CompareLogic.
/// </summary>
public static CompareLogic compareLogic;

/// <summary>
/// Static contstructor to initialise compareLogic.
/// </summary>
static ClientUtils()
{
compareLogic = new CompareLogic();
}

{{/useCompareNetObjects}}
/// <summary>
/// Sanitize filename by removing the path
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@
<HintPath Condition="Exists('..\..\packages')">..\..\packages\RestSharp.106.5.4\lib\{{#isNet40}}net4{{/isNet40}}{{^isNet40}}{{#isNet452}}net452{{/isNet452}}{{^isNet452}}{{targetFrameworkNuget}}{{/isNet452}}{{/isNet40}}\RestSharp.dll</HintPath>
<HintPath Condition="Exists('{{binRelativePath}}')">{{binRelativePath}}\RestSharp.106.5.4\lib\{{#isNet40}}net4{{/isNet40}}{{^isNet40}}{{#isNet452}}net452{{/isNet452}}{{^isNet452}}{{targetFrameworkNuget}}{{/isNet452}}{{/isNet40}}\RestSharp.dll</HintPath>
</Reference>
{{#useCompareNetObjects}}
<Reference Include="KellermanSoftware.Compare-NET-Objects">
<HintPath Condition="Exists('$(SolutionDir)\packages')">$(SolutionDir)\packages\CompareNETObjects.4.57.0\lib\{{#isNet40}}net4{{/isNet40}}{{^isNet40}}{{#isNet452}}net452{{/isNet452}}{{^isNet452}}{{targetFrameworkNuget}}{{/isNet452}}{{/isNet40}}\KellermanSoftware.Compare-NET-Objects.dll</HintPath>
<HintPath Condition="Exists('..\packages')">..\packages\CompareNETObjects.4.57.0\lib\{{#isNet40}}net4{{/isNet40}}{{^isNet40}}{{#isNet452}}net452{{/isNet452}}{{^isNet452}}{{targetFrameworkNuget}}{{/isNet452}}{{/isNet40}}\KellermanSoftware.Compare-NET-Objects.dll</HintPath>
<HintPath Condition="Exists('..\..\packages')">..\..\packages\CompareNETObjects.4.57.0\lib\{{#isNet40}}net4{{/isNet40}}{{^isNet40}}{{#isNet452}}net452{{/isNet452}}{{^isNet452}}{{targetFrameworkNuget}}{{/isNet452}}{{/isNet40}}\KellermanSoftware.Compare-NET-Objects.dll</HintPath>
<HintPath Condition="Exists('{{binRelativePath}}')">{{binRelativePath}}\CompareNETObjects.4.57.0\lib\{{#isNet40}}net4{{/isNet40}}{{^isNet40}}{{#isNet452}}net452{{/isNet452}}{{^isNet452}}{{targetFrameworkNuget}}{{/isNet452}}{{/isNet40}}\KellermanSoftware.Compare-NET-Objects.dll</HintPath>
</Reference>
{{/useCompareNetObjects}}
{{#generatePropertyChanged}}
<Reference Include="PropertyChanged">
<HintPath>..\..\packages\PropertyChanged.Fody.1.51.3\Lib\portable-net4+sl4+wp8+win8+wpa81+MonoAndroid16+MonoTouch40\PropertyChanged.dll</HintPath>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ ${nuget_cmd} install src/{{packageName}}/packages.config -o packages;

echo "[INFO] Copy DLLs to the 'bin' folder"
mkdir -p bin;
{{#useCompareNetObjects}}
cp packages/CompareNETObjects.4.57.0/lib/{{#isNet40}}net4{{/isNet40}}{{^isNet40}}{{#isNet452}}net452{{/isNet452}}{{^isNet452}}{{targetFrameworkNuget}}{{/isNet452}}{{/isNet40}}/KellermanSoftware.Compare-NET-Objects.dll bin/KellermanSoftware.Compare-NET-Objects.dll;
{{/useCompareNetObjects}}
cp packages/Newtonsoft.Json.12.0.1/lib/{{targetFrameworkNuget}}/Newtonsoft.Json.dll bin/Newtonsoft.Json.dll;
cp packages/RestSharp.106.5.4/lib/{{#isNet40}}net4{{/isNet40}}{{^isNet40}}{{#isNet452}}net452{{/isNet452}}{{^isNet452}}{{targetFrameworkNuget}}{{/isNet452}}{{/isNet40}}/RestSharp.dll bin/RestSharp.dll;
cp packages/JsonSubTypes.1.5.1/lib/{{targetFrameworkNuget}}/JsonSubTypes.dll bin/JsonSubTypes.dll
Expand All @@ -60,6 +63,9 @@ bin/Fody.dll,\
bin/PropertyChanged.Fody.dll,\
bin/PropertyChanged.dll,\
{{/generatePropertyChanged}}
{{#useCompareNetObjects}}
bin/KellermanSoftware.Compare-NET-Objects.dll,\
{{/useCompareNetObjects}}
bin/RestSharp.dll,\
System.ComponentModel.DataAnnotations.dll,\
System.Runtime.Serialization.dll \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ if not exist ".\nuget.exe" powershell -Command "(new-object System.Net.WebClient
.\nuget.exe install src\{{packageName}}\packages.config -o packages

if not exist ".\bin" mkdir bin

{{#CompareNetObjects}}
copy packages\CompareNETObjects.4.57.0\lib\{{#isNet40}}net4{{/isNet40}}{{^isNet40}}{{#isNet452}}net452{{/isNet452}}{{^isNet452}}{{targetFrameworkNuget}}{{/isNet452}}{{/isNet40}}\KellermanSoftware.Compare-NET-Objects.dll bin\KellermanSoftware.Compare-NET-Objects.dll
{{/CompareNetObjects}}
copy packages\Newtonsoft.Json.12.0.1\lib\{{targetFrameworkNuget}}\Newtonsoft.Json.dll bin\Newtonsoft.Json.dll
copy packages\JsonSubTypes.1.5.1\lib\{{targetFrameworkNuget}}\JsonSubTypes.dll bin\JsonSubTypes.dll
copy packages\RestSharp.106.5.4\lib\{{#isNet40}}net4{{/isNet40}}{{^isNet40}}{{#isNet452}}net452{{/isNet452}}{{^isNet452}}{{targetFrameworkNuget}}{{/isNet452}}{{/isNet40}}\RestSharp.dll bin\RestSharp.dll
Expand All @@ -23,5 +25,5 @@ copy packages\Fody.1.29.4\Fody.dll bin\Fody.dll
copy packages\PropertyChanged.Fody.1.51.3\PropertyChanged.Fody.dll bin\PropertyChanged.Fody.dll
copy packages\PropertyChanged.Fody.1.51.3\Lib\dotnet\PropertyChanged.dll bin\PropertyChanged.dll
{{/generatePropertyChanged}}
%CSCPATH%\csc /reference:bin\Newtonsoft.Json.dll;bin\JsonSubTypes.dll;bin\RestSharp.dll;System.ComponentModel.DataAnnotations.dll {{#generatePropertyChanged}}/r:bin\Fody.dll;bin\PropertyChanged.Fody.dll;bin\PropertyChanged.dll{{/generatePropertyChanged}} /target:library /out:bin\{{packageName}}.dll /recurse:src\{{packageName}}\*.cs /doc:bin\{{packageName}}.xml
%CSCPATH%\csc /reference:bin\Newtonsoft.Json.dll;bin\JsonSubTypes.dll;bin\RestSharp.dll;{{#CompareNetObjects}}bin\KellermanSoftware.Compare-NET-Objects.dll;{{/CompareNetObjects}}System.ComponentModel.DataAnnotations.dll {{#generatePropertyChanged}}/r:bin\Fody.dll;bin\PropertyChanged.Fody.dll;bin\PropertyChanged.dll{{/generatePropertyChanged}} /target:library /out:bin\{{packageName}}.dll /recurse:src\{{packageName}}\*.cs /doc:bin\{{packageName}}.xml

Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
{{/netStandard}}
using OpenAPIDateConverter = {{packageName}}.Client.OpenAPIDateConverter;
{{#useCompareNetObjects}}
using OpenAPIClientUtils = {{packageName}}.Client.ClientUtils;
{{/useCompareNetObjects}}

{{#models}}
{{#model}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,12 @@
/// <returns>Boolean</returns>
public override bool Equals(object input)
{
{{#useCompareNetObjects}}
return OpenAPIClientUtils.compareLogic.Compare(this, input as {{classname}}).AreEqual;
{{/useCompareNetObjects}}
{{^useCompareNetObjects}}
return this.Equals(input as {{classname}});
{{/useCompareNetObjects}}
}

/// <summary>
Expand All @@ -159,6 +164,10 @@
/// <returns>Boolean</returns>
public bool Equals({{classname}} input)
{
{{#useCompareNetObjects}}
return OpenAPIClientUtils.compareLogic.Compare(this, input).AreEqual;
{{/useCompareNetObjects}}
{{^useCompareNetObjects}}
if (input == null)
return false;

Expand All @@ -178,6 +187,7 @@
this.{{name}} != null &&
this.{{name}}.SequenceEqual(input.{{name}})
){{#hasMore}} && {{/hasMore}}{{/isContainer}}{{/vars}}{{^vars}}{{#parent}}base.Equals(input){{/parent}}{{^parent}}false{{/parent}}{{/vars}};
{{/useCompareNetObjects}}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
{{#useCompareNetObjects}}
<package id="CompareNETObjects" version="4.57.0" targetFramework="{{#isNet40}}net4{{/isNet40}}{{^isNet40}}{{#isNet452}}net452{{/isNet452}}{{^isNet452}}{{targetFrameworkNuget}}{{/isNet452}}{{/isNet40}}" developmentDependency="true" />
{{/useCompareNetObjects}}
<package id="RestSharp" version="106.5.4" targetFramework="{{#isNet40}}net4{{/isNet40}}{{^isNet40}}{{#isNet452}}net452{{/isNet452}}{{^isNet452}}{{targetFrameworkNuget}}{{/isNet452}}{{/isNet40}}" developmentDependency="true" />
<package id="Newtonsoft.Json" version="12.0.1" targetFramework="{{#isNet40}}net4{{/isNet40}}{{^isNet40}}{{#isNet452}}net452{{/isNet452}}{{^isNet452}}{{targetFrameworkNuget}}{{/isNet452}}{{/isNet40}}" developmentDependency="true" />
<package id="JsonSubTypes" version="1.5.1" targetFramework="{{#isNet40}}net4{{/isNet40}}{{^isNet40}}{{#isNet452}}net452{{/isNet452}}{{^isNet452}}{{targetFrameworkNuget}}{{/isNet452}}{{/isNet40}}" developmentDependency="true" />
<package id="Newtonsoft.Json" version="12.0.1" targetFramework="{{#isNet40}}net4{{/isNet40}}{{^isNet40}}{{#isNet452}}net45{{/isNet452}}{{^isNet452}}{{targetFrameworkNuget}}{{/isNet452}}{{/isNet40}}" developmentDependency="true" />
<package id="JsonSubTypes" version="1.5.1" targetFramework="{{#isNet40}}net4{{/isNet40}}{{^isNet40}}{{#isNet452}}net45{{/isNet452}}{{^isNet452}}{{targetFrameworkNuget}}{{/isNet452}}{{/isNet40}}" developmentDependency="true" />
{{#generatePropertyChanged}}
<package id="Fody" version="1.29.4" targetFramework="{{#isNet40}}net4{{/isNet40}}{{^isNet40}}{{#isNet452}}net452{{/isNet452}}{{^isNet452}}{{targetFrameworkNuget}}{{/isNet452}}{{/isNet40}}" developmentDependency="true" />
<package id="PropertyChanged.Fody" version="1.51.3" targetFramework="{{#isNet40}}net4{{/isNet40}}{{^isNet40}}{{#isNet452}}net452{{/isNet452}}{{^isNet452}}{{targetFrameworkNuget}}{{/isNet452}}{{/isNet40}}" developmentDependency="true" />
<package id="Fody" version="1.29.4" targetFramework="{{#isNet40}}net4{{/isNet40}}{{^isNet40}}{{#isNet452}}net45{{/isNet452}}{{^isNet452}}{{targetFrameworkNuget}}{{/isNet452}}{{/isNet40}}" developmentDependency="true" />
<package id="PropertyChanged.Fody" version="1.51.3" targetFramework="{{#isNet40}}net4{{/isNet40}}{{^isNet40}}{{#isNet452}}net45{{/isNet452}}{{^isNet452}}{{targetFrameworkNuget}}{{/isNet452}}{{/isNet40}}" developmentDependency="true" />
{{/generatePropertyChanged}}
</packages>
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
"FubarCoder.RestSharp.Portable.Core": "4.0.7",
"FubarCoder.RestSharp.Portable.HttpClient": "4.0.7",
"Newtonsoft.Json": "12.0.1",
{{#useCompareNetObjects}}
"KellermanSoftware.Compare-NET-Objects": "4.57.0",
{{/useCompareNetObjects}}
"JsonSubTypes": "1.5.1"
},
"frameworks": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<Properties StartupConfiguration="{19F1DEBC-DE5E-4517-8062-F000CD499087}|Unit Tests">
<MonoDevelop.Ide.Workbench ActiveDocument="src/Org.OpenAPITools.Test/Api/PetApiTests.cs">
<MonoDevelop.Ide.Workbench ActiveDocument="src/Org.OpenAPITools.Test/Model/PetTests.cs">
<Files>
<File FileName="src/Org.OpenAPITools.Test/Api/PetApiTests.cs" Line="302" Column="15" />
<File FileName="src/Org.OpenAPITools/Client/ApiClient.cs" Line="239" Column="82" />
<File FileName="src/Org.OpenAPITools.Test/packages.config" Line="1" Column="1" />
<File FileName="src/Org.OpenAPITools/Api/PetApi.cs" Line="1583" Column="1" />
<File FileName="src/Org.OpenAPITools/Client/ClientUtils.cs" Line="1" Column="1" />
<File FileName="src/Org.OpenAPITools.Test/Model/PetTests.cs" Line="159" Column="29" />
</Files>
<Pads>
<Pad Id="MonoDevelop.UnitTesting.TestPad">
<State name="__root__">
<Node name="Org.OpenAPITools" expanded="True" selected="True" />
</State>
</Pad>
</Pads>
</MonoDevelop.Ide.Workbench>
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" />
<MonoDevelop.Ide.DebuggingService.Breakpoints>
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ namespace Example
{

var apiInstance = new AnotherFakeApi();
var modelClient = new ModelClient(); // ModelClient | client model
var body = new ModelClient(); // ModelClient | client model
try
{
// To test special tags
ModelClient result = apiInstance.Call123TestSpecialTags(modelClient);
ModelClient result = apiInstance.Call123TestSpecialTags(body);
Debug.WriteLine(result);
}
catch (Exception e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ if not exist ".\nuget.exe" powershell -Command "(new-object System.Net.WebClient
.\nuget.exe install src\Org.OpenAPITools\packages.config -o packages

if not exist ".\bin" mkdir bin

copy packages\Newtonsoft.Json.12.0.1\lib\net45\Newtonsoft.Json.dll bin\Newtonsoft.Json.dll
copy packages\JsonSubTypes.1.5.1\lib\net45\JsonSubTypes.dll bin\JsonSubTypes.dll
copy packages\RestSharp.106.5.4\lib\net452\RestSharp.dll bin\RestSharp.dll
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ ${nuget_cmd} install src/Org.OpenAPITools/packages.config -o packages;

echo "[INFO] Copy DLLs to the 'bin' folder"
mkdir -p bin;
cp packages/CompareNETObjects.4.57.0/lib/net452/KellermanSoftware.Compare-NET-Objects.dll bin/KellermanSoftware.Compare-NET-Objects.dll;
cp packages/Newtonsoft.Json.12.0.1/lib/net45/Newtonsoft.Json.dll bin/Newtonsoft.Json.dll;
cp packages/RestSharp.106.5.4/lib/net452/RestSharp.dll bin/RestSharp.dll;
cp packages/JsonSubTypes.1.5.1/lib/net45/JsonSubTypes.dll bin/JsonSubTypes.dll

echo "[INFO] Run 'mcs' to build bin/Org.OpenAPITools.dll"
mcs -langversion:${langversion} -sdk:${sdk} -r:bin/Newtonsoft.Json.dll,bin/JsonSubTypes.dll,\
bin/KellermanSoftware.Compare-NET-Objects.dll,\
bin/RestSharp.dll,\
System.ComponentModel.DataAnnotations.dll,\
System.Runtime.Serialization.dll \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Method | HTTP request | Description

<a name="call123testspecialtags"></a>
# **Call123TestSpecialTags**
> ModelClient Call123TestSpecialTags (ModelClient modelClient)
> ModelClient Call123TestSpecialTags (ModelClient body)
To test special tags

Expand All @@ -30,12 +30,12 @@ namespace Example
public void main()
{
var apiInstance = new AnotherFakeApi();
var modelClient = new ModelClient(); // ModelClient | client model
var body = new ModelClient(); // ModelClient | client model
try
{
// To test special tags
ModelClient result = apiInstance.Call123TestSpecialTags(modelClient);
ModelClient result = apiInstance.Call123TestSpecialTags(body);
Debug.WriteLine(result);
}
catch (Exception e)
Expand All @@ -51,7 +51,7 @@ namespace Example

Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**modelClient** | [**ModelClient**](ModelClient.md)| client model |
**body** | [**ModelClient**](ModelClient.md)| client model |

### Return type

Expand Down
Loading

0 comments on commit 1db105b

Please sign in to comment.