Skip to content

Support multiple calls to WithTags in WithOpenApi #41779

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 24, 2022
Merged
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
22 changes: 18 additions & 4 deletions src/OpenApi/src/OpenApiGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,23 @@ private static void GenerateDefaultResponses(Dictionary<int, (Type?, MediaTypeCo

private List<OpenApiTag> GetOperationTags(MethodInfo methodInfo, EndpointMetadataCollection metadata)
{
var tags = metadata.GetMetadata<ITagsMetadata>();
var metadataList = metadata.GetOrderedMetadata<ITagsMetadata>();

if (metadataList.Count > 0)
{
var tags = new List<OpenApiTag>();

foreach (var metadataItem in metadataList)
{
foreach (var tag in metadataItem.Tags)
{
tags.Add(new OpenApiTag() { Name = tag });
}
}

return tags;
}

string controllerName;

if (methodInfo.DeclaringType is not null && !TypeHelper.IsCompilerGeneratedType(methodInfo.DeclaringType))
Expand All @@ -342,9 +358,7 @@ private List<OpenApiTag> GetOperationTags(MethodInfo methodInfo, EndpointMetadat
controllerName = _environment?.ApplicationName ?? string.Empty;
}

return tags is not null
? tags.Tags.Select(tag => new OpenApiTag() { Name = tag }).ToList()
: new List<OpenApiTag>() { new OpenApiTag() { Name = controllerName } };
return new List<OpenApiTag>() { new OpenApiTag() { Name = controllerName } };
}

private List<OpenApiParameter> GetOpenApiParameters(MethodInfo methodInfo, EndpointMetadataCollection metadata, RoutePattern pattern, bool disableInferredBody)
Expand Down
31 changes: 31 additions & 0 deletions src/OpenApi/test/OpenApiGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq.Expressions;
using System.Reflection;
using System.Security.Claims;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Metadata;
using Microsoft.AspNetCore.Mvc;
Expand Down Expand Up @@ -50,6 +51,23 @@ public void UsesApplicationNameAsOperationTagsIfNoDeclaringType()
Assert.Equal(declaringTypeName, tag.Name);
}

[Fact]
public void UsesTagsFromMultipleCallsToWithTags()
{
var testBuilder = new TestEndpointConventionBuilder();
var routeHandlerBuilder = new RouteHandlerBuilder(new[] { testBuilder });

routeHandlerBuilder
.WithTags("A")
.WithTags("B");

var operation = GetOpenApiOperation(() => { }, additionalMetadata: testBuilder.Metadata.ToArray());

Assert.Collection(operation.Tags,
tag => Assert.Equal("A", tag.Name),
tag => Assert.Equal("B", tag.Name));
}

[Fact]
public void ThrowsInvalidOperationExceptionGivenUnnamedParameter()
{
Expand Down Expand Up @@ -923,4 +941,17 @@ private struct ArgumentListStruct
public InferredJsonClass FromBody { get; set; }
public HttpContext Context { get; set; }
}

private class TestEndpointConventionBuilder : EndpointBuilder, IEndpointConventionBuilder
{
public void Add(Action<EndpointBuilder> convention)
{
convention(this);
}

public override Endpoint Build()
{
throw new NotImplementedException();
}
}
}