NSwaggerMerge 0.5.1

dotnet add package NSwaggerMerge --version 0.5.1
                    
NuGet\Install-Package NSwaggerMerge -Version 0.5.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="NSwaggerMerge" Version="0.5.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="NSwaggerMerge" Version="0.5.1" />
                    
Directory.Packages.props
<PackageReference Include="NSwaggerMerge" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add NSwaggerMerge --version 0.5.1
                    
#r "nuget: NSwaggerMerge, 0.5.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package NSwaggerMerge@0.5.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=NSwaggerMerge&version=0.5.1
                    
Install as a Cake Addin
#tool nuget:?package=NSwaggerMerge&version=0.5.1
                    
Install as a Cake Tool

NSwaggerMerge

Merge OpenApi 3.0 documents from different services under the microservice architecture.

Motivation

In a Microservices architecture, using an API Gateway, the various services are hidden behind the gateway and each individual swagger doc is not exposed. Front-end devs using an OpenApi codegen tool, need a single unified OpenApi doc for all services behind the gateway.

Example appsettings.json configuration
{
  ...
  "SwaggerOutputConfig-v1": {
    "Info": {
      "Title": "Admin Portal API",
      "Version": "1.0"
    },
    "Schemes": [
      "https"
    ],
    "Security": [ {} ]
  },
  "SwaggerInputConfig-v1": [
    {
      "File": "https://localhost:5001/swagger/v1/swagger.json"
    },
    {
      "File": "https://localhost:5002/swagger/v1/swagger.json"
    },
    {
      "File": "https://localhost:5003/swagger/v1/swagger.json"
    },
    {
      "File": "https://localhost:5004/swagger/v1/swagger.json"
    },
    {
      "File": "https://localhost:5005/swagger/v1/swagger.json"
    }
  ]
  ...
}
Registations
...
builder.Services.RegisterCommonServices(configuration);

var swaggerInputConfig = builder.Configuration
    .GetSection("SwaggerInputConfig-v1")
    .Get<SwaggerInputConfiguration[]>();

builder.Services.AddSingleton(swaggerInputConfig);

builder.Services.AddOptions<SwaggerOutputConfiguration>()
    .BindConfiguration("SwaggerOutputConfig-v1")
    .ValidateDataAnnotations()
    .ValidateOnStart();
...
How To Use
...
if (app.Environment.IsDevelopment() || app.Environment.IsStaging())
{
    app.MapOpenApi();

    app.UseSwagger();
    app.UseSwaggerUI(options =>
    {
        options.DocumentTitle = "Admin Portal API";
        options.SwaggerEndpoint("/admin-portal-api/v1/swagger.json", "Admin Portal API v1");

        options.RoutePrefix = string.Empty;
        options.EnableTryItOutByDefault();
        options.DisplayRequestDuration();
        options.ShowCommonExtensions();
        options.ShowExtensions();
    });

    app.MapGet("/admin-portal-api/v1/swagger.json", async (SwaggerInputConfiguration[] swaggerInputConfig, IOptions<SwaggerOutputConfiguration> swaggerOutputOptions) =>
    {
        SwaggerOutputConfiguration swaggerOutputConfig = swaggerOutputOptions.Value;

        SwaggerMergeConfiguration config = new()
        {
            Inputs = swaggerInputConfig,
            Output = swaggerOutputConfig
        };

        SwaggerMerger.ValidateConfiguration(config);

        return await SwaggerMerger.MergeAsync(config);
    }).AllowAnonymous().Produces<ActionResult>(contentType: System.Net.Mime.MediaTypeNames.Application.Json);
}
...

The end result looks like this:

Merge Swagger Endpoint

Upon sending a request to this endpoint, NSwaggerMerge will merge into a single doc all OpenApi docs from services configured in appsettings (services must be running).

Credits

Building upon the following 2 tools:

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.5.1 11 8/13/2025
0.5.0 11 8/13/2025

Update Project Dependencies