fix: use case-insensitive comparer in IEnum GetHashCode#1797
fix: use case-insensitive comparer in IEnum GetHashCode#1797TabishRiazBajwa wants to merge 2 commits into
Conversation
Fixes Adyen#1679 `GetHashCode()` in both `modelEnum.mustache` and `modelInnerEnum.mustache` used the default case-sensitive string hash, while `Equals()` and `==` use `StringComparison.OrdinalIgnoreCase`. This violates the .NET hash/equality contract — objects that compare equal must produce equal hash codes. **Fix:** Replace `Value?.GetHashCode() ?? 0` with `StringComparer.OrdinalIgnoreCase.GetHashCode(Value ?? string.Empty)` in both templates. **Tests added:** Two new tests in `IEnumTest` cover the contract — one asserting equal hash codes for same-value-different-case instances, and one proving Dictionary lookup works correctly after the fix.
There was a problem hiding this comment.
Code Review
This pull request updates the GetHashCode implementation for custom string-based enums in C# templates and tests to use StringComparer.OrdinalIgnoreCase.GetHashCode. This ensures that case-insensitive equality matches the hash code contract, allowing the enums to be used correctly as dictionary keys. Additionally, unit tests were added to verify this behavior. The review feedback correctly points out that using Value ?? string.Empty inside StringComparer.OrdinalIgnoreCase.GetHashCode can lead to hash collisions between null and empty strings (which are treated as unequal by Equals). It is recommended to check if Value is null first to avoid this issue.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
Fixes #1679
GetHashCode()in bothmodelEnum.mustacheandmodelInnerEnum.mustacheused the default case-sensitive string hash, whileEquals()and==useStringComparison.OrdinalIgnoreCase. This violates the .NET hash/equality contract, objects that compare equal must produce equal hash codes.Description
GetHashCode()was returningValue?.GetHashCode() ?? 0, which is case-sensitive. SinceEquals()usesStringComparison.OrdinalIgnoreCase, two instances with the same value but different casing (e.g."valid"vs"VALID") would be considered equal but produce different hash codes — causing silent lookup failures when used as dictionary keys or in hash sets.Fixed by replacing with
StringComparer.OrdinalIgnoreCase.GetHashCode(Value ?? string.Empty)in both templates.Tested scenarios
Fixed issue: #1679