You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The DataClassificationSet class in the .NET Extensions library implements custom Equals and GetHashCode methods to support value-based equality. However, the current GetHashCode implementation may not ensure consistent hash codes for logically identical instances.
This inconsistency has implications for classes like RedactorProvider, which relies on FrozenDictionary<DataClassificationSet, Redactor> for mapping classifications to redactors. If two identical DataClassificationSet instances (by content) produce different hash codes, it undermines the reliability of key-based lookups in RedactorProvider, potentially leading to incorrect behavior such as failing to retrieve the correct Redactor for a given classification set.
Define multiple DataClassificationSet instances with identical contents but as separate instances.
Initialize a RedactorProvider instance with mappings that include one of the DataClassificationSet instances as a key.
Attempt to retrieve a Redactor using RedactorProvider.GetRedactor with a logically identical DataClassificationSet instance (not the same instance used in initialization). HashSet<DataClassification> will have its hash code calculated based on the HashSet instance's runtime identity, not the contents of the HashSet. Thus, two DataClassificationSet instances containing identical items may return different hash codes.
Observe potential inconsistencies in the retrieval outcome due to differing hash codes or failed equality checks.
// Identical DataClassificationSet instances (by content, different instances)varclassificationSet1=newDataClassificationSet(newDataClassification("TaxonomyName","Value"));varclassificationSet2=newDataClassificationSet(newDataClassification("TaxonomyName","Value"));// NOTE: classificationSet1 and classificationSet2 will have different hash codes// Retrieve a redactor based on the classification setvarredactor=redactorProvider.GetRedactor(classificationSet1);varredactor=redactorProvider.GetRedactor(classificationSet2;// Above call to redactorProvider will have inconsistent results
Expected behavior
The RedactorProvider.GetRedactor method should consistently return the correct Redactor for any DataClassificationSet instance that is logically identical to a key in the underlying FrozenDictionary, ensuring that DataClassificationSet's value-based equality is effectively utilized in key-based lookups.
Actual behavior
There may be instances where RedactorProvider.GetRedactor fails to return the correct Redactor for logically identical DataClassificationSet instances. This failure can occur due to the current GetHashCode implementation in DataClassificationSet not guaranteeing consistent hash codes for identical set contents, leading to potential lookup failures in FrozenDictionary.
Regression?
No response
Known Workarounds
No response
Configuration
No response
Other information
Suggested fix:
Revise the GetHashCode and possibly Equals implementations in DataClassificationSet to ensure they account for the contents of the set in a consistent and order-independent manner. This might involve aggregating the hash codes of contained DataClassification instances in a way that is unaffected by their order. Additionally, ensure that RedactorProvider and any similar components relying on DataClassificationSet instances as keys in dictionaries or sets properly handle these instances to maintain lookup reliability and correctness.
The text was updated successfully, but these errors were encountered:
Description
The
DataClassificationSet
class in the .NET Extensions library implements customEquals
andGetHashCode
methods to support value-based equality. However, the currentGetHashCode
implementation may not ensure consistent hash codes for logically identical instances.See: https://github.com/dotnet/extensions/blob/main/src/Libraries/Microsoft.Extensions.Compliance.Abstractions/Classification/DataClassificationSet.cs#L86
This inconsistency has implications for classes like
RedactorProvider
, which relies onFrozenDictionary<DataClassificationSet, Redactor>
for mapping classifications to redactors. If two identicalDataClassificationSet
instances (by content) produce different hash codes, it undermines the reliability of key-based lookups inRedactorProvider
, potentially leading to incorrect behavior such as failing to retrieve the correctRedactor
for a given classification set.Refer to RedactorProvider implementation for Redactor lookup:
https://github.com/dotnet/extensions/blob/main/src/Libraries/Microsoft.Extensions.Compliance.Redaction/RedactorProvider.cs#L28
Reproduction Steps
DataClassificationSet
instances with identical contents but as separate instances.RedactorProvider
instance with mappings that include one of theDataClassificationSet
instances as a key.RedactorProvider.GetRedactor
with a logically identicalDataClassificationSet
instance (not the same instance used in initialization).HashSet<DataClassification>
will have its hash code calculated based on the HashSet instance's runtime identity, not the contents of the HashSet. Thus, two DataClassificationSet instances containing identical items may return different hash codes.Expected behavior
The RedactorProvider.GetRedactor method should consistently return the correct Redactor for any DataClassificationSet instance that is logically identical to a key in the underlying FrozenDictionary, ensuring that DataClassificationSet's value-based equality is effectively utilized in key-based lookups.
Actual behavior
There may be instances where RedactorProvider.GetRedactor fails to return the correct Redactor for logically identical DataClassificationSet instances. This failure can occur due to the current GetHashCode implementation in DataClassificationSet not guaranteeing consistent hash codes for identical set contents, leading to potential lookup failures in FrozenDictionary.
Regression?
No response
Known Workarounds
No response
Configuration
No response
Other information
Suggested fix:
Revise the GetHashCode and possibly Equals implementations in DataClassificationSet to ensure they account for the contents of the set in a consistent and order-independent manner. This might involve aggregating the hash codes of contained DataClassification instances in a way that is unaffected by their order. Additionally, ensure that RedactorProvider and any similar components relying on DataClassificationSet instances as keys in dictionaries or sets properly handle these instances to maintain lookup reliability and correctness.
The text was updated successfully, but these errors were encountered: