Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

AV0026: Remove unnecessary group name format

Value
Rule IDAV0026
CategoryUsage
Fix isNon-breaking

Cause

A group name format is configured for an application where no API has a group name.

Rule Description

FormatGroupName is only reached for an API that has a group name. An API without one is described by its API version alone, so the callback is never invoked and the format has no effect.

Consider the following code:

var builder = WebApplication.CreateBuilder( args );

builder.Services.AddApiVersioning().AddApiExplorer(
    options =>
    {
        options.FormatGroupName = ( group, version ) => $"{group}-{version}";
    } );

var app = builder.Build();

app.MapGet( "/order", () => Results.Ok() ).HasApiVersion( 1.0 );

app.Run();

No API in the application states a group name, so nothing is ever formatted.

How to Fix Violations

Either remove the format, or give the APIs it is meant for a group name.

var app = builder.Build();

app.MapGet( "/order", () => Results.Ok() ).HasApiVersion( 1.0 ).WithGroupName( "orders" );

app.Run();

A controller states its group name with [ApiExplorerSettings]:

[ApiController]
[ApiVersion( 1.0 )]
[ApiExplorerSettings( GroupName = "orders" )]
[Route( "[controller]" )]
public class OrderController : ControllerBase
{
    [HttpGet]
    public IActionResult Get() => Ok();
}

When to Suppress Warnings

It is safe to suppress this rule if the APIs carrying group names are declared outside the compilation, such as in a referenced library.