Tuesday, March 23, 2021

ASP.NET Core and Swagger: Add Operations Programmatically

In this post, let's see how we can programmatically add Operations to Swagger document in an ASP.NET Core Application.

I had a requirement where an Angular application uses DevExpress Report Viewer and the reports are being rendered through an ASP.NET Core API. Front-end communicates with BE through an Azure API Gateway. So basically all the back-end services endpoints need to be exposed via Azure API Gateway and it's getting updated by discovering the endpoints in back-end services Swagger documents. But unfortunately Swagger is failing to discover the DevExpress default reporting endpoints.

So I have overridden the default endpoints and applied [ApiExplorerSettings(IgnoreApi = true)] attribute. Now those endpoints will be ignored from getting generated into the Swagger Document, but still, I needed to programmatically add those.

It's actually quite simple. I just needed to create a new IDocumentFilterIDocumentFilters are useful when we need to modify Swagger Documents after they're initially generated.

using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using System.Collections.Generic;

public class AddReportViewerOperationsFilter : IDocumentFilter
{
    public void Apply(OpenApiDocument swaggerDocDocumentFilterContext context)
    {
        // Tags are for group the operations
        var openApiTags = new List<OpenApiTag> { new OpenApiTag { Name = "ReportViewer" } };

        // whatever the path
        swaggerDoc.Paths.Add($"/api/{swaggerDoc.Info.Version}/Reports/Viewer"new OpenApiPathItem
        {
            Operations = new Dictionary<OperationTypeOpenApiOperation>
            {
                // GET: /api/v1/Reports/Viewer
                {
                    OperationType.Get,
                    new OpenApiOperation
                    {
                        Tags = openApiTags,
                        Responses = new OpenApiResponses
                        {
                            { "200"new OpenApiResponse { Description = "Success" } }
                        }
                    }
                },
                // POST: /api/v1/Reports/Viewer
                {
                    OperationType.Post,
                    new OpenApiOperation
                    {
                        Tags = openApiTags,
                        Responses = new OpenApiResponses
                        {
                            { "200"new OpenApiResponse { Description = "Success" } }
                        }
                    }
                }
            }
        });
    }
}

So here I have just added 2 Operations, but you get the idea. You can define the Parameters for Operations if you want to, in my case, I didn't want to.

Finally, we need to register the AddReportViewerOperationsFilter when setting up Swagger. 

public void ConfigureServices(IServiceCollection services)
{
    // some code

    services.AddSwaggerGen(config =>
    {
        // some code

        config.DocumentFilter<AddReportViewerOperationsFilter>();
    }); // some code }

And now, when we spin up the service, we can see the new operations that got added in the Swagger document.

Swagger: Programmatically Added Operations

Hope this helps.

Happy Coding.

Regards,
Jaliya

No comments:

Post a Comment