Skip to content

Commit 28b65dd

Browse files
committed
test(models): verify copy constructors preserve custom tag collections
1 parent 2499925 commit 28b65dd

2 files changed

Lines changed: 116 additions & 0 deletions

File tree

‎test/Microsoft.OpenApi.Tests/Models/OpenApiDocumentTests.cs‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2214,6 +2214,64 @@ public void TagsSupportsImmutableSortedSets()
22142214
Assert.IsType<ImmutableSortedSet<OpenApiTag>>(document.Tags);
22152215
}
22162216

2217+
[Fact]
2218+
public void EnsureOpenApiDocumentCopyConstructorPreservesCustomTagCollections()
2219+
{
2220+
var sortedDocument = new OpenApiDocument
2221+
{
2222+
Tags = new SortedSet<OpenApiTag>(new DescendingOpenApiTagComparer())
2223+
{
2224+
new() { Name = "tagB" },
2225+
new() { Name = "tagA" },
2226+
new() { Name = "tagC" },
2227+
}
2228+
};
2229+
2230+
var copiedSorted = new OpenApiDocument(sortedDocument);
2231+
Assert.NotNull(copiedSorted.Tags);
2232+
Assert.IsType<SortedSet<OpenApiTag>>(copiedSorted.Tags);
2233+
Assert.Equal(["tagC", "tagB", "tagA"], copiedSorted.Tags.Select(t => t.Name));
2234+
2235+
// Ensure modifying copied set does not affect original
2236+
copiedSorted.Tags.Add(new OpenApiTag { Name = "tagD" });
2237+
Assert.Equal(3, sortedDocument.Tags.Count);
2238+
Assert.Equal(4, copiedSorted.Tags.Count);
2239+
2240+
var customHashDocument = new OpenApiDocument
2241+
{
2242+
Tags = new HashSet<OpenApiTag>(new CaseInsensitiveOpenApiTagEqualityComparer())
2243+
{
2244+
new() { Name = "tag1" },
2245+
}
2246+
};
2247+
2248+
var copiedHash = new OpenApiDocument(customHashDocument);
2249+
Assert.NotNull(copiedHash.Tags);
2250+
Assert.IsType<HashSet<OpenApiTag>>(copiedHash.Tags);
2251+
Assert.False(copiedHash.Tags.Add(new OpenApiTag { Name = "TAG1" }));
2252+
Assert.True(copiedHash.Tags.Add(new OpenApiTag { Name = "tag2" }));
2253+
Assert.Equal(1, customHashDocument.Tags.Count);
2254+
Assert.Equal(2, copiedHash.Tags.Count);
2255+
2256+
#if NET
2257+
var immutableDocument = new OpenApiDocument
2258+
{
2259+
Tags = ImmutableSortedSet.Create(
2260+
new DescendingOpenApiTagComparer(),
2261+
[
2262+
new OpenApiTag { Name = "tagB" },
2263+
new OpenApiTag { Name = "tagA" },
2264+
new OpenApiTag { Name = "tagC" },
2265+
]),
2266+
};
2267+
2268+
var copiedImmutable = new OpenApiDocument(immutableDocument);
2269+
Assert.NotNull(copiedImmutable.Tags);
2270+
Assert.IsType<ImmutableSortedSet<OpenApiTag>>(copiedImmutable.Tags);
2271+
Assert.Equal(["tagC", "tagB", "tagA"], copiedImmutable.Tags.Select(t => t.Name));
2272+
#endif
2273+
}
2274+
22172275
public static TheoryData<OpenApiSpecVersion> OpenApiSpecVersions()
22182276
{
22192277
var values = new TheoryData<OpenApiSpecVersion>();

‎test/Microsoft.OpenApi.Tests/Models/OpenApiOperationTests.cs‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -939,5 +939,63 @@ public void TagsSupportsImmutableSortedSets()
939939
Assert.Equal(["tagC", "tagB", "tagA"], names);
940940
Assert.IsType<ImmutableSortedSet<OpenApiTagReference>>(operation.Tags);
941941
}
942+
943+
[Fact]
944+
public void EnsureOpenApiOperationCopyConstructorPreservesCustomTagCollections()
945+
{
946+
var sortedOperation = new OpenApiOperation
947+
{
948+
Tags = new SortedSet<OpenApiTagReference>(new DescendingOpenApiTagReferenceComparer())
949+
{
950+
new("tagB"),
951+
new("tagA"),
952+
new("tagC"),
953+
}
954+
};
955+
956+
var copiedSorted = new OpenApiOperation(sortedOperation);
957+
Assert.NotNull(copiedSorted.Tags);
958+
Assert.IsType<SortedSet<OpenApiTagReference>>(copiedSorted.Tags);
959+
Assert.Equal(["tagC", "tagB", "tagA"], copiedSorted.Tags.Select(t => t.Name));
960+
961+
// Ensure modifying copied set does not affect original
962+
copiedSorted.Tags.Add(new OpenApiTagReference("tagD"));
963+
Assert.Equal(3, sortedOperation.Tags.Count);
964+
Assert.Equal(4, copiedSorted.Tags.Count);
965+
966+
var customHashOperation = new OpenApiOperation
967+
{
968+
Tags = new HashSet<OpenApiTagReference>(new CaseInsensitiveOpenApiTagReferenceEqualityComparer())
969+
{
970+
new("tag1"),
971+
}
972+
};
973+
974+
var copiedHash = new OpenApiOperation(customHashOperation);
975+
Assert.NotNull(copiedHash.Tags);
976+
Assert.IsType<HashSet<OpenApiTagReference>>(copiedHash.Tags);
977+
Assert.False(copiedHash.Tags.Add(new OpenApiTagReference("TAG1")));
978+
Assert.True(copiedHash.Tags.Add(new OpenApiTagReference("tag2")));
979+
Assert.Equal(1, customHashOperation.Tags.Count);
980+
Assert.Equal(2, copiedHash.Tags.Count);
981+
982+
#if NET
983+
var immutableOperation = new OpenApiOperation
984+
{
985+
Tags = ImmutableSortedSet.Create(
986+
new DescendingOpenApiTagReferenceComparer(),
987+
[
988+
new OpenApiTagReference("tagB"),
989+
new OpenApiTagReference("tagA"),
990+
new OpenApiTagReference("tagC"),
991+
]),
992+
};
993+
994+
var copiedImmutable = new OpenApiOperation(immutableOperation);
995+
Assert.NotNull(copiedImmutable.Tags);
996+
Assert.IsType<ImmutableSortedSet<OpenApiTagReference>>(copiedImmutable.Tags);
997+
Assert.Equal(["tagC", "tagB", "tagA"], copiedImmutable.Tags.Select(t => t.Name));
998+
#endif
999+
}
9421000
}
9431001
}

0 commit comments

Comments
 (0)