Skip to content

Commit

Permalink
fix: switch to Object.Equals for C# Equals override (#1865)
Browse files Browse the repository at this point in the history
Co-authored-by: Jonas Lagoni <jonas-lt@live.dk>
  • Loading branch information
Louis-PhilippeGentile and jonaslagoni authored Mar 8, 2024
1 parent c42758b commit 38d764c
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 10 deletions.
6 changes: 4 additions & 2 deletions docs/languages/Csharp.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/generators/csharp/presets/CommonPreset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ${
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 38d764c

Please sign in to comment.