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/OpenApiOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,22 @@ public ISet<OpenApiTagReference>? Tags
}
set
{
if (value is null)
{
_tags = null;
return;
}
_tags = value switch
{
HashSet<OpenApiTagReference> tags when !ReferenceEquals(tags.Comparer, EqualityComparer<OpenApiTagReference>.Default) => value,
SortedSet<OpenApiTagReference> => value,
_tags = CopyTags(value);
}
}

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

/// <summary>
Expand Down Expand Up @@ -139,7 +140,7 @@ public OpenApiOperation() { }
public OpenApiOperation(OpenApiOperation operation)
{
Utils.CheckArgumentNull(operation);
Tags = operation.Tags != null ? new HashSet<OpenApiTagReference>(operation.Tags) : null;
Tags = CopyTags(operation.Tags, copy: true);
Summary = operation.Summary ?? Summary;
Description = operation.Description ?? Description;
ExternalDocs = operation.ExternalDocs != null ? new(operation.ExternalDocs) : null;
Expand Down
56 changes: 56 additions & 0 deletions test/Microsoft.OpenApi.Tests/Models/OpenApiOperationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -939,5 +939,61 @@ public void TagsSupportsImmutableSortedSets()
Assert.Equal(["tagC", "tagB", "tagA"], names);
Assert.IsType<ImmutableSortedSet<OpenApiTagReference>>(operation.Tags);
}

[Fact]
public void OpenApiOperationCopyConstructorPreservesCustomComparer()
{
var operation = new OpenApiOperation
{
Tags = new HashSet<OpenApiTagReference>(new CaseInsensitiveOpenApiTagReferenceEqualityComparer())
{
new("Tag1")
}
};

var copy = new OpenApiOperation(operation);
Assert.NotNull(copy.Tags);
Assert.False(copy.Tags.Add(new OpenApiTagReference("tag1")));
Assert.Single(copy.Tags);
}

[Fact]
public void OpenApiOperationCopyConstructorPreservesSortedSet()
{
var operation = new OpenApiOperation
{
Tags = new SortedSet<OpenApiTagReference>(new DescendingOpenApiTagReferenceComparer())
{
new("tagB"),
new("tagA"),
new("tagC"),
}
};

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

[Fact]
public void OpenApiOperationCopyConstructorPreservesImmutableSortedSet()
{
var operation = new OpenApiOperation
{
Tags = ImmutableSortedSet.Create(
new DescendingOpenApiTagReferenceComparer(),
[
new OpenApiTagReference("tagB"),
new OpenApiTagReference("tagA"),
new OpenApiTagReference("tagC"),
]),
};

var copy = new OpenApiOperation(operation);
Assert.IsType<ImmutableSortedSet<OpenApiTagReference>>(copy.Tags);
var copyNames = copy.Tags.Select(t => t.Name);
Assert.Equal(["tagC", "tagB", "tagA"], copyNames);
}
}
}
Loading