Skip to content

Commit

Permalink
Add "AppendActionCountToTagSummaryDocumentFilter" to append action co…
Browse files Browse the repository at this point in the history
…unt into the SwaggetTag's descriptions.
  • Loading branch information
unchase committed May 13, 2019
1 parent d9aba97 commit 0fcbffc
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

These are the changes to each version that has been released on the [nuget](https://www.nuget.org/packages/Unchase.Swashbuckle.AspNetCore.Extensions/).

## v1.1.1 `(2019-05-14)`

- [x] Add `AppendActionCountToTagSummaryDocumentFilter` to append action count into the SwaggetTag's descriptions

## v1.1.0 `(2019-05-07)`

- [x] Add extension-method `HidePathItemsWithoutAcceptedRoles` for hiding all SwaggerDocument PathItems with added Security information for OAuth2 without accepted roles
Expand Down
42 changes: 40 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Install-Package Unchase.Swashbuckle.AspNetCore.Extensions

## Extensions (Filters) use

1. **For fix enums**:
1. **Fix enums**:
- In the _ConfigureServices_ method of _Startup.cs_, inside your `AddSwaggerGen` call, enable whichever extensions (filters) you need:

```csharp
Expand Down Expand Up @@ -48,7 +48,7 @@ public void ConfigureServices(IServiceCollection services)
}
```

2. **For hiding SwaggerDocumentation PathItems** without accepted roles:
2. **Hide SwaggerDocumentation PathItems** without accepted roles:
- In the _ConfigureServices_ method of _Startup.cs_, inside your `AddSwaggerGen` call, enable whichever extensions (filters) you need:

```csharp
Expand Down Expand Up @@ -97,6 +97,30 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
}
```

3. **Append action count into the SwaggetTag's descriptions**:
- In the _ConfigureServices_ method of _Startup.cs_, inside your `AddSwaggerGen` call, enable whichever extensions (filters) you need:

```csharp
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
...

services.AddSwaggerGen(options =>
{
...

// enable swagger Annotations
options.EnableAnnotations();

// add action count into the SwaggerTag's descriptions
options.DocumentFilter<AppendActionCountToTagSummaryDocumentFilter>();

...
});
}
```

## Builds status

|Status|Value|
Expand Down Expand Up @@ -238,6 +262,20 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
}
```

### Append action count into the SwaggetTag's descriptions

![Append action count](assets/appendActionCountIntoSwaggerTag.png)

You should use `SwaggerTagAttribute` for controllers:

```csharp
[SwaggerTag("SamplePerson description")]
public class SamplePersonController : ControllerBase
{
...
}
```

## HowTos

- [ ] Add HowTos in a future
Expand Down
1 change: 1 addition & 0 deletions Unchase.Swashbuckle.AspNetCore.Extensions.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "assets", "assets", "{145A03
assets\enumDescriptionInSchemaDefinitions.png = assets\enumDescriptionInSchemaDefinitions.png
assets\enumDescriptionInSchemaParameters.png = assets\enumDescriptionInSchemaParameters.png
assets\enumDescriptionInSwaggerUI.png = assets\enumDescriptionInSwaggerUI.png
assets\hideSwaggerDocumentPathItems.png = assets\hideSwaggerDocumentPathItems.png
assets\icon.png = assets\icon.png
LICENSE = LICENSE
assets\logo.png = assets\logo.png
Expand Down
Binary file added assets/appendActionCountIntoSwaggerTag.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System.Collections.Generic;
using System.Linq;
using Swashbuckle.AspNetCore.Swagger;
using Swashbuckle.AspNetCore.SwaggerGen;

namespace Unchase.Swashbuckle.AspNetCore.Extensions.Filters
{
public class AppendActionCountToTagSummaryDocumentFilter : IDocumentFilter
{
public void Apply(SwaggerDocument swaggerDoc, DocumentFilterContext context)
{
if (swaggerDoc.Tags == null)
return;

var tagActionCount = new Dictionary<string, int>();
foreach (var path in swaggerDoc.Paths)
{
var possibleParameterizedOperations = new List<Operation> {path.Value.Get, path.Value.Post, path.Value.Put, path.Value.Delete, path.Value.Patch};
possibleParameterizedOperations.Where(o => o?.Tags != null).ToList().ForEach(o =>
{
foreach (var tag in o.Tags)
{
if (!tagActionCount.ContainsKey(tag))
tagActionCount.Add(tag, 1);
else
tagActionCount[tag]++;
}
});
}

foreach (var tagActionCountKey in tagActionCount.Keys)
{
foreach (var tag in swaggerDoc.Tags)
{
if (tag.Name == tagActionCountKey)
tag.Description += $" (action count: {tagActionCount[tagActionCountKey]})";
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using System.Net;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;
using WebApi2._0_Swashbuckle.Models;

namespace WebApi2._0_Swashbuckle.Controllers
{
/// <summary>
/// Sample Person Controller.
/// </summary>
[SwaggerTag("SamplePerson description")]
[Route("api/[controller]")]
[Produces("application/json")]
[ApiController]
Expand Down
5 changes: 5 additions & 0 deletions test/WebApi2.0-Swashbuckle/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ public void ConfigureServices(IServiceCollection services)
// add Security information to each operation for OAuth2
options.OperationFilter<SecurityRequirementsOperationFilter>();

// enable swagger Annotations
options.EnableAnnotations();
// add action count into the SwaggerTag's descriptions
options.DocumentFilter<AppendActionCountToTagSummaryDocumentFilter>();

// if you're using the SecurityRequirementsOperationFilter, you also need to tell Swashbuckle you're using OAuth2
options.AddSecurityDefinition("oauth2", new ApiKeyScheme
{
Expand Down

0 comments on commit 0fcbffc

Please sign in to comment.