Achyut Manvar
Achyut Manvar

Reputation: 1

API Version not working for Scalar OpenAPI Documentation in .Net 9

I have an ASP.NET Core 9 Web API application. By default, .NET9 doesn't use Swashbuckle anymore. To generate API documentation I'm using Microsoft OpenAPI documents and Scalar. I have API versioning enabled using controllers. I have added open API documentation for all versions and the documents are generating fine.

I have created two Open API documentation, v1.json, and v2.json and both are accessible via the default route pattern /openapi/{documentName}.json. Still, the Scalar UI can only identify the endpoints in the /openapi/v1.json document since that is the default for Scalar.

What do I need to get all versions of documentation visible in Scalar under one single page like we can in SwaggerUI? Reference Image. I tried looking online but couldn't find anything concrete.

This is my Main method in the Program.cs:

public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

            builder.Configuration.AddEnvironmentVariables();

            var loggerSettings = builder.Configuration.GetSection("LoggerSettings").Get<LoggerSettings>();
            if (loggerSettings != null)
                builder.Services.AddCustomLogging(loggerSettings);

            var defaultConnection = builder.Configuration.GetValue<string>("DatabaseSettings:DefaultConnection");
            builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseLazyLoadingProxies().UseNpgsql(defaultConnection));

            builder.Services.Configure<DatabaseSettings>(builder.Configuration.GetSection("DatabaseSettings"));

            builder.Services.AddControllers();
            
            // Add API versioning
            builder.Services.AddApiVersioning(options =>
            {
                options.DefaultApiVersion = new ApiVersion(2, 0);
                options.ReportApiVersions = true;
                options.AssumeDefaultVersionWhenUnspecified = true;
                options.ApiVersionReader = ApiVersionReader.Combine(
                    new UrlSegmentApiVersionReader(),
                    new HeaderApiVersionReader("x-api-version"),
                    new QueryStringApiVersionReader("api-version")
                );
            })
            .AddApiExplorer(options =>
            {
                options.GroupNameFormat = "'v'VVV";
                // Replace the placeholder with the actual version
                options.SubstituteApiVersionInUrl = true;
            });

            builder.Services.AddOpenApi("v1");
            builder.Services.AddOpenApi("v2");

            builder.Services.AddScoped<ISampleService, SampleService>();

            var app = builder.Build();

            // Configure the HTTP request pipeline.
            if (app.Environment.IsDevelopment())
            {
                app.MapOpenApi();
                app.MapScalarApiReference();
            }

            app.MapControllers();

            app.Run();
        }

Upvotes: 0

Views: 126

Answers (0)

Related Questions