Remove Equals and GetHashCode methods from AnyOf #1688
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
r? @brandur-stripe
cc @stripe/api-libraries
Remove the
Equals
andGetHashCode
methods from theAnyOf
wrapper class.In C#,
Equals
andGetHashCode
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 delegatingEquals
to the value held within theAnyOf
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
overridingEquals
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).