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

AV0024: Remove unnecessary API explorer option

Value
Rule IDAV0024
CategoryUsage
Fix isNon-breaking

Cause

An API explorer option restates a value it already inherits from the API versioning options.

Rule Description

The API explorer takes the options it shares with API versioning before its own configuration runs. Stating one of those shared values again only repeats what it was already given.

The shared values are AssumeDefaultVersionWhenUnspecified, DefaultApiVersion, RouteConstraintName, ApiVersionSelector, and ApiVersionParameterSource, which the API explorer takes from ApiVersionReader.

Consider the following code:

builder.Services.AddApiVersioning(
    options =>
    {
        options.DefaultApiVersion = new ApiVersion( 2.0 );
    } )
    .AddApiExplorer(
    options =>
    {
        options.DefaultApiVersion = new ApiVersion( 2.0 );
    } );

The API explorer was already given 2.0 from the API versioning options.

A value that differs from the one configured for API versioning is a deliberate departure and is not reported.

How to Fix Violations

Remove the assignment and let the value be inherited.

builder.Services.AddApiVersioning(
    options =>
    {
        options.DefaultApiVersion = new ApiVersion( 2.0 );
    } )
    .AddApiExplorer();

When to Suppress Warnings

It is safe to suppress this rule if you prefer the API explorer options to be stated in full so that a later change to the API versioning options does not silently change what is described.