Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String enum types have broken GetHashCodes #49

Closed
stephentoub opened this issue Jun 12, 2024 · 1 comment
Closed

String enum types have broken GetHashCodes #49

stephentoub opened this issue Jun 12, 2024 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@stephentoub
Copy link
Contributor

Repro:

using OpenAI.Chat;

ChatToolCallKind value1 = new("value1");
ChatToolCallKind VALUE1 = new("VALUE1");

Console.WriteLine(value1.Equals(VALUE1)); // prints true as expected

Console.WriteLine(value1.GetHashCode());
Console.WriteLine(VALUE1.GetHashCode()); // should print same value as above, but doesn't

HashSet<ChatToolCallKind> set = [value1];
Console.WriteLine(set.Contains(VALUE1)); // should print true but prints false

There are over 100 types, most of them generated, with a definition of equality like this:

        [EditorBrowsable(EditorBrowsableState.Never)]
        public override bool Equals(object obj) => obj is VectorStoreBatchFileJobStatus other && Equals(other);
        public bool Equals(VectorStoreBatchFileJobStatus other) => string.Equals(_value, other._value, StringComparison.InvariantCultureIgnoreCase);

        [EditorBrowsable(EditorBrowsableState.Never)]
        public override int GetHashCode() => _value?.GetHashCode() ?? 0;

The rules for GetHashCode state:

If two objects compare as equal, the GetHashCode() method for each object must return the same value.

but here, equality is based on a case-insensitive comparison but hash code is being computed based on case-sensitive. That means two equal values can end up with different hashcodes. That in turn breaks things like dictionary/set lookups, which rely on hash codes for bucketing, and thus even if there's an equal value in the dictionary, it's likely not to be found because of differing hashcodes.

@joseharriaga
Copy link
Collaborator

joseharriaga commented Jul 12, 2024

The following PR addresses this issue: 🔗 #120

@joseharriaga joseharriaga self-assigned this Jul 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants