swagger-api / swagger-api/swagger-codegen

[CSharp] Bug In generated GetHashCode method

Open
#8,686 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

From MSDN (link),

If you override the GetHashCode method, you should also override Equals, and vice versa. If your overridden Equals method returns true when two objects are tested for equality, your overridden GetHashCode method must return the same value for the two objects.

The generated model class is not following this rule when the model contains a list member. As every list object will return different hash code even if their contents are same, due to the default Object.GetHashCode implementation.

Suggest a fix/enhancement

I think GetHashCode method should not consider list members when generating hash code. This will fix the issue of list objects generating different hash code even when they are equal (link). Also, it is OK if we get equal hash code for different objects as specified in the MSDN document.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at modules/swagger-codegen/src/main/resources/csharp/modelGeneric.mustache around the linked GetHashCode template. Inspect how list members are included in generated C# models, then verify generated output for a model containing a list. Done means list members no longer cause unequal hash codes when the corresponding objects are equal.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.