Skip to content

Conversation

@baywet
Copy link
Member

@baywet baywet commented Jan 16, 2026

fixes #2678 port of #2679 to v3

spanglerco and others added 5 commits January 15, 2026 15:35
fix: Support custom tag ordering
…port/v2--components--Microsoft.OpenApi

chore(support/v2): release 2.4.3
Signed-off-by: Vincent Biret <vibiret@microsoft.com>
@baywet baywet requested a review from a team as a code owner January 16, 2026 17:50
@baywet baywet enabled auto-merge January 16, 2026 17:50
@sonarqubecloud
Copy link

new HashSet<OpenApiTag>(value, OpenApiTagComparer.Instance);
_tags = value switch
{
HashSet<OpenApiTag> tags when tags.Comparer != EqualityComparer<OpenApiTag>.Default => value,

Check warning

Code scanning / CodeQL

Reference equality test on System.Object Warning

Reference equality for System.Object comparisons (
this
argument has type IEqualityComparer).

Copilot Autofix

AI 1 day ago

In general, to fix this issue you should avoid using ==/!= between variables of type object or interface when the intent is value/semantic equality. Either cast to a more specific type that has an appropriate Equals/== implementation or call Equals explicitly. If the intent is to check identity, then use ReferenceEquals(a, b).

Here, the intent is: “if the existing HashSet’s comparer is not the default comparer, keep the existing set; otherwise, wrap the incoming set in a new HashSet with OpenApiTagComparer.Instance.” This logic should not depend on two comparer instances being the same object. The minimal behavioural change is to replace the reference comparison with a semantic equality comparison using Equals, handling null-safety. We can do this by using !EqualityComparer<OpenApiTag>.Default.Equals(tags.Comparer) instead of tags.Comparer != EqualityComparer<OpenApiTag>.Default. This preserves the branch condition’s meaning (“comparer is different from the default”) but uses proper equality semantics instead of object identity. All other parts of the snippet remain unchanged, and no new imports or types are required.

Concretely, in src/Microsoft.OpenApi/Models/OpenApiDocument.cs, in the Tags property setter’s switch expression, update the HashSet<OpenApiTag> pattern clause to use !EqualityComparer<OpenApiTag>.Default.Equals(tags.Comparer).

Suggested changeset 1
src/Microsoft.OpenApi/Models/OpenApiDocument.cs

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/Microsoft.OpenApi/Models/OpenApiDocument.cs b/src/Microsoft.OpenApi/Models/OpenApiDocument.cs
--- a/src/Microsoft.OpenApi/Models/OpenApiDocument.cs
+++ b/src/Microsoft.OpenApi/Models/OpenApiDocument.cs
@@ -93,7 +93,7 @@
                 }
                 _tags = value switch
                 {
-                    HashSet<OpenApiTag> tags when tags.Comparer != EqualityComparer<OpenApiTag>.Default => value,
+                    HashSet<OpenApiTag> tags when !EqualityComparer<OpenApiTag>.Default.Equals(tags.Comparer) => value,
                     SortedSet<OpenApiTag> => value,
 #if NET
                     ImmutableSortedSet<OpenApiTag> => value,
EOF
@@ -93,7 +93,7 @@
}
_tags = value switch
{
HashSet<OpenApiTag> tags when tags.Comparer != EqualityComparer<OpenApiTag>.Default => value,
HashSet<OpenApiTag> tags when !EqualityComparer<OpenApiTag>.Default.Equals(tags.Comparer) => value,
SortedSet<OpenApiTag> => value,
#if NET
ImmutableSortedSet<OpenApiTag> => value,
Copilot is powered by AI and may make mistakes. Always verify output.
@baywet baywet merged commit c290369 into main Jan 16, 2026
21 of 23 checks passed
@baywet baywet deleted the fix/tags-order-to-v3 branch January 16, 2026 17:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot use a custom sort for OpenApiDocument.Tags in v2

4 participants