Skip to content

Using ApiExplorerSettingsAttribute together with ApiVersionAttribute produces unexpected number of ApiVersionDescriptions #1066

Closed
@folbo

Description

@folbo

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

In my application I'm grouping some controllers into ApiExplorer groups to produce multiple swagger files. For each group I'm trying to implement independent versioning using Asp.Versioning.

When I mix [ApiExplorer(GroupName = "WeatherForecastGroupName")] with [ApiVersion(1)] attributes my IApiVersionDescriptionProvider creates two ApiVersionDescriptions, both with version 1. Provider's output attached below:

provider.ApiVersionDescriptions.Select(x => x.GroupName).ToArray()
{string[2]}
    [0]: "1.0"
    [1]: "WeatherForecastGroupName_1.0"

Environment: AspNetCore 6 application with controllers.

My code used to reproduce the issue:

[ApiController]
[Route("api/v{version:apiVersion}/[controller]")]
[ApiVersion(1)]
[ApiExplorerSettings(GroupName = "WeatherForecastGroupName")]
public class WeatherForecastController : ControllerBase
{
    private static readonly string[] Summaries = new[]
    {
        "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
    };

    [HttpGet(Name = "GetWeatherForecast")]
    public IEnumerable<WeatherForecast> Get()
    {
        return Enumerable.Range(1, 5).Select(index => new WeatherForecast
        {
            Date = DateTime.Now.AddDays(index),
            TemperatureC = Random.Shared.Next(-20, 55),
            Summary = Summaries[Random.Shared.Next(Summaries.Length)]
        })
        .ToArray();
    }
}

builder.Services.AddApiVersioning(
        options =>
        {
            options.ApiVersionReader = new UrlSegmentApiVersionReader();
        })
    .AddMvc()
    .AddApiExplorer(
        options =>
        {
            options.SubstituteApiVersionInUrl = true;
            options.FormatGroupName = (name, version) => $"{name}_{version}";
        })

Expected Behavior

I'd expect that Asp.Versioning.ApiExplorer would use options.FormatGroupName as the only source of ApiVersionDescriptions if provided.

Steps To Reproduce

  1. Define aspnetcore 6 controller with sample action and with [AspVersion(1)] and [ApiExplorerSettings(GroupName = "name")] attributes
  2. Configure package with FormatGroupName
  3. Notice contents of IApiVersionDescriptionProvider

Exceptions (if any)

No response

.NET Version

7.0.403

Anything else?

No response

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions