Skip to content

Only one tag is returned on operations when OpenApiTagReference is used #2319

Description

Describe the bug

Having updated Swashbuckle.AspNetCore to 2.0.0-preview.11 and ASP.NET Core 10 preview 3 in domaindrivendev/Swashbuckle.AspNetCore#3283, I've identified an issue where only one tag is returned when a larger set of tag referencess is assigned to an OpenAPI operation.

In creating a self-contained repro for the issue, I find that this is also an issue in 2.0.0-preview.16.

My guess is that there's an issue with how the custom OpenApiTagComparer class is implemented, which is used when assigning a value to the property here:

publicHashSet<OpenApiTagReference>?Tags
{
get
{
return_tags;
}
set
{
if(valueisnull)
{
return;
}
_tags=valueisHashSet<OpenApiTagReference>tags&&tags.ComparerisOpenApiTagComparer?
tags:
newHashSet<OpenApiTagReference>(value,OpenApiTagComparer.Instance);
}
}

Specifically, it looks like it doesn't cater for an OpenApiReference where Name resolves to null:

publicboolEquals(IOpenApiTag?x,IOpenApiTag?y)
{
if(xisnull&&yisnull)
{
returntrue;
}
if(xisnull||yisnull)
{
returnfalse;
}
if(ReferenceEquals(x,y))
{
returntrue;
}
returnStringComparer.Equals(x.Name,y.Name);
}

Code To Reproduce

usingMicrosoft.OpenApi.Models;usingMicrosoft.OpenApi.Models.References;vardocument=newOpenApiDocument();vartags=newHashSet<OpenApiTagReference>();tags.Add(newOpenApiTagReference("one",document));tags.Add(newOpenApiTagReference("two",document));tags.Add(newOpenApiTagReference("three",document));Console.WriteLine("There are {0} original tags.",tags.Count);Console.WriteLine("The original tags are: {0}",string.Join(", ",tags.Select(t =>t.Reference.Id)));varoperation=newOpenApiOperation(){Tags=tags};Console.WriteLine("There are {0} tags in the operation.",operation.Tags.Count);Console.WriteLine("The operation tags are: {0}",string.Join(", ",operation.Tags.Select(t =>t.Reference.Id)));
<ProjectSdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReferenceInclude="Microsoft.OpenApi"Version="2.0.0-preview.16" />
</ItemGroup>
</Project>

Expected behavior

There are 3 original tags.
The original tags are: one, two, three
There are 3 tags in the operation.
The operation tags are: one, two, three

Actual behaviour

There are 3 original tags.
The original tags are: one, two, three
There are 1 tags in the operation.
The operation tags are: one

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedpriority:p1High priority but not blocking. Causes major but not critical loss of functionality SLA <=7daystype:bugA broken experiencetype:regressionA bug from previous release

    Type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions