From 38d764c66cc7bdfa40a410e59f2b656a025da88c Mon Sep 17 00:00:00 2001 From: Louis-PhilippeGentile Date: Fri, 8 Mar 2024 04:10:29 -0500 Subject: [PATCH] fix: switch to Object.Equals for C# Equals override (#1865) Co-authored-by: Jonas Lagoni --- docs/languages/Csharp.md | 6 ++++-- .../__snapshots__/index.spec.ts.snap | 2 +- src/generators/csharp/presets/CommonPreset.ts | 2 +- .../presets/__snapshots__/CommonPreset.spec.ts.snap | 12 ++++++------ 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/docs/languages/Csharp.md b/docs/languages/Csharp.md index 965735b8c1..bf48c37e9d 100644 --- a/docs/languages/Csharp.md +++ b/docs/languages/Csharp.md @@ -61,13 +61,15 @@ Currently not supported, [let everyone know you need it](https://github.com/asyn ## Generate models with equals and GetHashCode methods -To overwrite the `Equal` and `GetHashCode` methods, use the preset `CSHARP_COMMON_PRESET` and provide the options `equal: true` and `hashCode: true` +To overwrite the `Equals` and `GetHashCode` methods, use the preset `CSHARP_COMMON_PRESET` and provide the options `equal: true` and `hashCode: true` Check out this [example for a live demonstration](../../examples/csharp-generate-equals-and-hashcode). +Note that the `Equals` method uses `Object.Equals(objA, objB)`. This means that [reference types properties](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/reference-types) that do not override their default `Equals` method will perform a reference equality check to determine equality, returning `false` if they do not reference the same object. + ## Generate models with auto-implemented properties -To generate auto-implemented properties (the ones with with `{ get; set; }` accessors), use the preset `CSHARP_COMMON_PRESET` and provide the option `autoImplementedProperties: true` +To generate auto-implemented properties (the ones with `{ get; set; }` accessors), use the preset `CSHARP_COMMON_PRESET` and provide the option `autoImplementedProperties: true` Check out this [example for a live demonstration](../../examples/csharp-auto-implemented-properties). diff --git a/examples/csharp-generate-equals-and-hashcode/__snapshots__/index.spec.ts.snap b/examples/csharp-generate-equals-and-hashcode/__snapshots__/index.spec.ts.snap index ba1e2ddf0b..b051f6d96b 100644 --- a/examples/csharp-generate-equals-and-hashcode/__snapshots__/index.spec.ts.snap +++ b/examples/csharp-generate-equals-and-hashcode/__snapshots__/index.spec.ts.snap @@ -17,7 +17,7 @@ Array [ if(obj is Root model) { if(ReferenceEquals(this, model)) { return true; } - return Email == model.Email; + return Object.Equals(Email, model.Email); } return false; diff --git a/src/generators/csharp/presets/CommonPreset.ts b/src/generators/csharp/presets/CommonPreset.ts index c1002306d2..2c23eb22d1 100644 --- a/src/generators/csharp/presets/CommonPreset.ts +++ b/src/generators/csharp/presets/CommonPreset.ts @@ -23,7 +23,7 @@ function renderEqual({ let equalProperties = propertyKeys .map((propertyName) => { const accessorMethodProp = FormatHelpers.upperFirst(propertyName); - return `${accessorMethodProp} == model.${accessorMethodProp}`; + return `Object.Equals(${accessorMethodProp}, model.${accessorMethodProp})`; }) .join(' && \n'); equalProperties = `return ${ diff --git a/test/generators/csharp/presets/__snapshots__/CommonPreset.spec.ts.snap b/test/generators/csharp/presets/__snapshots__/CommonPreset.spec.ts.snap index 633fa9fe7c..f62f475f4a 100644 --- a/test/generators/csharp/presets/__snapshots__/CommonPreset.spec.ts.snap +++ b/test/generators/csharp/presets/__snapshots__/CommonPreset.spec.ts.snap @@ -37,10 +37,10 @@ exports[`CSHARP_COMMON_PRESET should render Equals support function 1`] = ` if(obj is Test model) { if(ReferenceEquals(this, model)) { return true; } - return StringProp == model.StringProp && - NumberProp == model.NumberProp && - ObjectProp == model.ObjectProp && - AdditionalProperties == model.AdditionalProperties; + return Object.Equals(StringProp, model.StringProp) && + Object.Equals(NumberProp, model.NumberProp) && + Object.Equals(ObjectProp, model.ObjectProp) && + Object.Equals(AdditionalProperties, model.AdditionalProperties); } return false; @@ -81,8 +81,8 @@ exports[`CSHARP_COMMON_PRESET should render Equals support function 2`] = ` if(obj is NestedTest model) { if(ReferenceEquals(this, model)) { return true; } - return StringProp == model.StringProp && - AdditionalProperties == model.AdditionalProperties; + return Object.Equals(StringProp, model.StringProp) && + Object.Equals(AdditionalProperties, model.AdditionalProperties); } return false;