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
29 changes: 15 additions & 14 deletions src/Microsoft.OpenApi/Models/OpenApiDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,21 +82,22 @@ public ISet<OpenApiTag>? Tags
}
set
{
if (value is null)
{
_tags = null;
return;
}
_tags = value switch
{
HashSet<OpenApiTag> tags when tags.Comparer != EqualityComparer<OpenApiTag>.Default => value,
SortedSet<OpenApiTag> => value,
_tags = CopyTags(value);
}
}

private static ISet<OpenApiTag>? CopyTags(ISet<OpenApiTag>? tags, bool copy = false)
{
return tags switch
{
null => null,
HashSet<OpenApiTag> hashSet when !ReferenceEquals(hashSet.Comparer, EqualityComparer<OpenApiTag>.Default) => copy ? new HashSet<OpenApiTag>(hashSet, hashSet.Comparer) : hashSet,
SortedSet<OpenApiTag> sorted => copy ? new SortedSet<OpenApiTag>(sorted, sorted.Comparer) : sorted,
#if NET
ImmutableSortedSet<OpenApiTag> => value,
ImmutableSortedSet<OpenApiTag> => tags,
#endif
_ => new HashSet<OpenApiTag>(value, OpenApiTagComparer.Instance),
};
}
_ => new HashSet<OpenApiTag>(tags, OpenApiTagComparer.Instance),
};
}

/// <summary>
Expand Down Expand Up @@ -141,7 +142,7 @@ public OpenApiDocument(OpenApiDocument? document)
Webhooks = document?.Webhooks != null ? new Dictionary<string, IOpenApiPathItem>(document.Webhooks) : null;
Components = document?.Components != null ? new(document?.Components) : null;
Security = document?.Security != null ? [.. document.Security] : null;
Tags = document?.Tags != null ? new HashSet<OpenApiTag>(document.Tags, OpenApiTagComparer.Instance) : null;
Tags = CopyTags(document?.Tags, copy: true);
ExternalDocs = document?.ExternalDocs != null ? new(document.ExternalDocs) : null;
Extensions = document?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(document.Extensions) : null;
Metadata = document?.Metadata != null ? new Dictionary<string, object>(document.Metadata) : null;
Expand Down
56 changes: 56 additions & 0 deletions test/Microsoft.OpenApi.Tests/Models/OpenApiDocumentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2214,6 +2214,62 @@ public void TagsSupportsImmutableSortedSets()
Assert.IsType<ImmutableSortedSet<OpenApiTag>>(document.Tags);
}

[Fact]
public void OpenApiDocumentCopyConstructorPreservesCustomComparer()
{
var document = new OpenApiDocument
{
Tags = new HashSet<OpenApiTag>(new CaseInsensitiveOpenApiTagEqualityComparer())
{
new() { Name = "Tag1" }
}
};

var copy = new OpenApiDocument(document);
Assert.NotNull(copy.Tags);
Assert.False(copy.Tags.Add(new OpenApiTag { Name = "tag1" }));
Assert.Single(copy.Tags);
}

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

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

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

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

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