-
Notifications
You must be signed in to change notification settings - Fork 280
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:
OpenAPI.NET/src/Microsoft.OpenApi/Models/OpenApiOperation.cs
Lines 29 to 45 in 63a8a34
| 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:
OpenAPI.NET/src/Microsoft.OpenApi/OpenApiTagComparer.cs
Lines 20 to 35 in 63a8a34
| 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