Skip to content

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

@martincostello

Description

@martincostello

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:

public HashSet<OpenApiTagReference>? Tags
{
get
{
return _tags;
}
set
{
if (value is null)
{
return;
}
_tags = value is HashSet<OpenApiTagReference> tags && tags.Comparer is OpenApiTagComparer ?
tags :
new HashSet<OpenApiTagReference>(value, OpenApiTagComparer.Instance);
}
}

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

public bool Equals(IOpenApiTag? x, IOpenApiTag? y)
{
if (x is null && y is null)
{
return true;
}
if (x is null || y is null)
{
return false;
}
if (ReferenceEquals(x, y))
{
return true;
}
return StringComparer.Equals(x.Name, y.Name);
}

Code To Reproduce

using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Models.References;

var document = new OpenApiDocument();

var tags = new HashSet<OpenApiTagReference>();

tags.Add(new OpenApiTagReference("one", document));
tags.Add(new OpenApiTagReference("two", document));
tags.Add(new OpenApiTagReference("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)));

var operation = new OpenApiOperation() { 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)));
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net9.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="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