Skip to content

Commit

Permalink
Merge pull request #2210 from captainsafia/safia/tags-addition
Browse files Browse the repository at this point in the history
Add support for TagsMetadata in endpoints
  • Loading branch information
domaindrivendev committed Oct 15, 2021
2 parents 73610b9 + fd12883 commit 2c3c46d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ init:
- git config --global core.autocrlf true

environment:
DOTNET_VERSION: "6.0.100-rc.1.21463.6"
DOTNET_VERSION: "6.0.100-rc.2.21505.57"

build_script:
- ps: dotnet pack -c Release -o artifacts
Expand Down
6 changes: 3 additions & 3 deletions global.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"sdk": {
"version": "6.0.100-rc.1.21463.6"
"sdk": {
"version": "6.0.100-rc.2.21505.57"
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
#if NET6_0_OR_GREATER
using Microsoft.AspNetCore.Http.Metadata;
#endif
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.OpenApi.Models;
using Microsoft.AspNetCore.Routing;
Expand Down Expand Up @@ -79,7 +82,17 @@ private string DefaultOperationIdSelector(ApiDescription apiDescription)

private IList<string> DefaultTagsSelector(ApiDescription apiDescription)
{
#if (!NET6_0_OR_GREATER)
return new[] { apiDescription.ActionDescriptor.RouteValues["controller"] };
#else
var actionDescriptor = apiDescription.ActionDescriptor;
var tagsMetadata = actionDescriptor.EndpointMetadata?.LastOrDefault(m => m is ITagsMetadata) as ITagsMetadata;
if (tagsMetadata != null)
{
return new List<string>(tagsMetadata.Tags);
}
return new[] { apiDescription.ActionDescriptor.RouteValues["controller"] };
#endif
}

private string DefaultSortKeySelector(ApiDescription apiDescription)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
Expand Down Expand Up @@ -729,6 +730,30 @@ public void GetSwagger_SupportsOption_TagSelector()
Assert.Equal(new[] { "resource" }, document.Paths["/resource"].Operations[OperationType.Post].Tags.Select(t => t.Name));
}

[Fact]
public void GetSwagger_CanReadTagsFromMetadata()
{
var methodInfo = typeof(FakeController).GetMethod(nameof(FakeController.ActionWithParameter));
var actionDescriptor = new ActionDescriptor
{
EndpointMetadata = new List<object>() { new TagsAttribute("Some", "Tags", "Here") },
RouteValues = new Dictionary<string, string>
{
["controller"] = methodInfo.DeclaringType.Name.Replace("Controller", string.Empty)
}
};
var subject = Subject(
apiDescriptions: new[]
{
ApiDescriptionFactory.Create(actionDescriptor, methodInfo, groupName: "v1", httpMethod: "POST", relativePath: "resource"),
}
);

var document = subject.GetSwagger("v1");

Assert.Equal(new[] { "Some", "Tags", "Here" }, document.Paths["/resource"].Operations[OperationType.Post].Tags.Select(t => t.Name));
}

[Fact]
public void GetSwagger_SupportsOption_ConflictingActionsResolver()
{
Expand Down

0 comments on commit 2c3c46d

Please sign in to comment.