Skip to content
Open
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
26 changes: 22 additions & 4 deletions src/Microsoft.OpenApi/Models/OpenApiOperation.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.Linq;

namespace Microsoft.OpenApi
Expand All @@ -27,11 +30,18 @@
{
if (value is null)
{
_tags = null;
return;
}
_tags = value is HashSet<OpenApiTagReference> tags && tags.Comparer is OpenApiTagComparer ?
tags :
new HashSet<OpenApiTagReference>(value, OpenApiTagComparer.Instance);
_tags = value switch
{
HashSet<OpenApiTagReference> tags when tags.Comparer != EqualityComparer<OpenApiTagReference>.Default => value,
SortedSet<OpenApiTagReference> => value,
#if NET
ImmutableSortedSet<OpenApiTagReference> => value,
#endif
_ => new HashSet<OpenApiTagReference>(value, OpenApiTagComparer.Instance),
};
}
}

Expand Down Expand Up @@ -129,7 +139,15 @@
public OpenApiOperation(OpenApiOperation operation)
{
Utils.CheckArgumentNull(operation);
Tags = operation.Tags != null ? new HashSet<OpenApiTagReference>(operation.Tags) : null;
Tags = operation.Tags switch
{
HashSet<OpenApiTagReference> tags when tags.Comparer != EqualityComparer<OpenApiTagReference>.Default => new HashSet<OpenApiTagReference>(tags, tags.Comparer),
SortedSet<OpenApiTagReference> sorted => new SortedSet<OpenApiTagReference>(sorted, sorted.Comparer),
#if NET
ImmutableSortedSet<OpenApiTagReference> => operation.Tags,
#endif
_ => operation.Tags != null ? new HashSet<OpenApiTagReference>(operation.Tags, OpenApiTagComparer.Instance) : null,
};
Summary = operation.Summary ?? Summary;
Description = operation.Description ?? Description;
ExternalDocs = operation.ExternalDocs != null ? new(operation.ExternalDocs) : null;
Expand Down
100 changes: 100 additions & 0 deletions test/Microsoft.OpenApi.Tests/Models/OpenApiOperationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -851,5 +851,105 @@ public void OpenApiOperationCopyConstructorWithAnnotationsSucceeds()

Assert.NotEqual(baseOperation.Metadata["key1"], actualOperation.Metadata["key1"]);
}

[Fact]
public void TagsCanBeReInitializedToNull()
{
var operation = new OpenApiOperation();
Assert.Null(operation.Tags);
operation.Tags = new HashSet<OpenApiTagReference>();
Assert.NotNull(operation.Tags);
operation.Tags = null;
Assert.Null(operation.Tags);
}

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

Assert.True(operation.Tags.Add(new OpenApiTagReference("Tag1")));
Assert.False(operation.Tags.Add(new OpenApiTagReference("tag1")));
Assert.True(operation.Tags.Add(new OpenApiTagReference("tag2")));
Assert.False(operation.Tags.Add(new OpenApiTagReference("TAG1")));
Assert.Equal(2, operation.Tags.Count);
}

[Fact]
public void TagsSupportsSortedSet()
{
var operation = new OpenApiOperation
{
Tags = new SortedSet<OpenApiTagReference>(new TagReferenceComparer()),
};

Assert.IsType<SortedSet<OpenApiTagReference>>(operation.Tags);
}

[Fact]
public void OpenApiOperationCopyConstructorPreservesCustomComparer()
{
var operation = new OpenApiOperation
{
Tags = new HashSet<OpenApiTagReference>(new CaseInsensitiveOpenApiTagReferenceEqualityComparer())
{
new OpenApiTagReference("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 comparer = Comparer<OpenApiTagReference>.Create((x, y) =>
string.Compare(y?.Name, x?.Name, System.StringComparison.Ordinal));
var operation = new OpenApiOperation
{
Tags = new SortedSet<OpenApiTagReference>(comparer)
{
new OpenApiTagReference("b"),
new OpenApiTagReference("a")
}
};

Assert.Collection(operation.Tags,
tag => Assert.Equal("b", tag.Name),
tag => Assert.Equal("a", tag.Name));

var copy = new OpenApiOperation(operation);
Assert.IsType<SortedSet<OpenApiTagReference>>(copy.Tags);
Assert.Collection(copy.Tags,
tag => Assert.Equal("b", tag.Name),
tag => Assert.Equal("a", tag.Name));
}

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

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

public int GetHashCode(OpenApiTagReference obj)
{
return obj?.Name == null ? 0 : System.StringComparer.OrdinalIgnoreCase.GetHashCode(obj.Name);
}
}
}
}
Loading