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

Remove Equals and GetHashCode methods from AnyOf #1688

Merged
merged 1 commit into from Jul 2, 2019

Conversation

ob-stripe
Copy link
Contributor

r? @brandur-stripe
cc @stripe/api-libraries

Remove the Equals and GetHashCode methods from the AnyOf wrapper class.

In C#, Equals and GetHashCode should be overridden to give value semantics to a reference type (cf. https://docs.microsoft.com/en-us/dotnet/api/system.object.equals?view=netframework-4.8).

The type held by an AnyOf instance may be either a value type (e.g. int, bool, etc.) or a reference type (e.g. DateRangeOptions, CardCreateNestedOptions, etc.). By delegating Equals to the value held within the AnyOf instance, we're implying that the value has value semantics even when that's not the case.

This breaks assumptions in some testing frameworks like Fluent Assertions, as they use reflection to see if a type overrides Equals and assume that it has value semantics if that's the case (cf. https://www.continuousimprover.com/2018/02/fluent-assertions-50-best-unit-test.html).

The library itself doesn't rely on AnyOf overriding Equals anywhere, so let's just remove it.

I did leave the ToString method that delegates to the held value since that seems useful for debugging (e.g. to log the values of request parameters).

Copy link
Contributor

@brandur-stripe brandur-stripe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the detailed explanation! Very helpful.

@stripe-ci stripe-ci assigned ob-stripe and unassigned brandur-stripe Jul 2, 2019
@ob-stripe ob-stripe merged commit aa0c2a1 into master Jul 2, 2019
@ob-stripe ob-stripe deleted the ob-anyof-remove-equals-gethashcode branch July 2, 2019 20:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants