Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@

* adds support for OpenAPI 3.2.0 ([765a8dd](https://github.com/microsoft/OpenAPI.NET/commit/765a8dd4d6efd1a31b6a76d282ccffa5877a845a))

## [2.4.3](https://github.com/microsoft/OpenAPI.NET/compare/v2.4.2...v2.4.3) (2026-01-16)

## Bug Fixes

* Support custom tag ordering ([008576c](https://github.com/microsoft/OpenAPI.NET/commit/008576c31f8dcecf59363c9c2f85d691601faa73))
* Support custom tag ordering ([7610d07](https://github.com/microsoft/OpenAPI.NET/commit/7610d07bd11a99a4ce1cc31338d180dbd764d952))

## [2.4.2](https://github.com/microsoft/OpenAPI.NET/compare/v2.4.1...v2.4.2) (2025-12-22)


Expand Down
15 changes: 12 additions & 3 deletions src/Microsoft.OpenApi/Models/OpenApiDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

using System;
using System.Collections.Generic;
#if NET
using System.Collections.Immutable;
#endif
using System.IO;
using System.Linq;
using System.Security.Cryptography;
Expand Down Expand Up @@ -88,9 +91,15 @@
{
return;
}
_tags = value is HashSet<OpenApiTag> tags && tags.Comparer is OpenApiTagComparer ?
tags :
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.
SortedSet<OpenApiTag> => value,
#if NET
ImmutableSortedSet<OpenApiTag> => value,
#endif
_ => new HashSet<OpenApiTag>(value, OpenApiTagComparer.Instance),
};
}
}

Expand Down
76 changes: 76 additions & 0 deletions test/Microsoft.OpenApi.Tests/Models/OpenApiDocumentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using VerifyXunit;
Expand Down Expand Up @@ -2126,6 +2129,79 @@ public void DeduplicatesTags()
Assert.Contains(document.Tags, t => t.Name == "tag2");
}

[Fact]
public void TagsSupportsCustomComparer()
{
var document = new OpenApiDocument
{
Tags = new HashSet<OpenApiTag>(new CaseInsensitiveOpenApiTagEqualityComparer()),
};

Assert.True(document.Tags.Add(new OpenApiTag { Name = "Tag1" }));
Assert.False(document.Tags.Add(new OpenApiTag { Name = "tag1" }));
Assert.True(document.Tags.Add(new OpenApiTag { Name = "tag2" }));
Assert.False(document.Tags.Add(new OpenApiTag { Name = "TAG1" }));
Assert.Equal(2, document.Tags.Count);
}

private sealed class CaseInsensitiveOpenApiTagEqualityComparer : IEqualityComparer<OpenApiTag>
{
public bool Equals(OpenApiTag x, OpenApiTag y)
{
return string.Equals(x.Name, y.Name, StringComparison.OrdinalIgnoreCase);
}

public int GetHashCode([DisallowNull] OpenApiTag obj)
{
return obj.Name.GetHashCode(StringComparison.OrdinalIgnoreCase);
}
}

[Fact]
public void TagsSupportsSortedSets()
{
var document = new OpenApiDocument
{
Tags = new SortedSet<OpenApiTag>(new DescendingOpenApiTagComparer())
{
new OpenApiTag { Name = "tagB" },
new OpenApiTag { Name = "tagA" },
new OpenApiTag { Name = "tagC" },
}
};

var names = document.Tags.Select(t => t.Name);
Assert.Equal(["tagC", "tagB", "tagA"], names);
Assert.IsType<SortedSet<OpenApiTag>>(document.Tags);
}

private sealed class DescendingOpenApiTagComparer : IComparer<OpenApiTag>
{
public int Compare(OpenApiTag x, OpenApiTag y)
{
return string.Compare(y?.Name, x?.Name, StringComparison.Ordinal);
}
}

[Fact]
public void TagsSupportsImmutableSortedSets()
{
var document = new OpenApiDocument
{
Tags = ImmutableSortedSet.Create(
new DescendingOpenApiTagComparer(),
[
new OpenApiTag { Name = "tagB" },
new OpenApiTag { Name = "tagA" },
new OpenApiTag { Name = "tagC" },
]),
};

var names = document.Tags.Select(t => t.Name);
Assert.Equal(["tagC", "tagB", "tagA"], names);
Assert.IsType<ImmutableSortedSet<OpenApiTag>>(document.Tags);
}

public static TheoryData<OpenApiSpecVersion> OpenApiSpecVersions()
{
var values = new TheoryData<OpenApiSpecVersion>();
Expand Down
Loading