swagger-api / swagger-api/swagger-codegen
[CSharp] Bug In generated GetHashCode method
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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