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

AV0022: Missing AddOData

Value
Rule IDAV0022
CategoryUsage
Fix isBreaking

Cause

An application uses OData and API versioning, but never opted into versioning OData.

Rule Description

OData routes by its own conventions rather than by the routes API versioning otherwise observes, so versioning an OData API takes an explicit opt in. Without it, the versioning metadata declared on an OData controller is never applied.

Consider the following code:

var builder = WebApplication.CreateBuilder( args );

builder.Services.AddControllers().AddOData();
builder.Services.AddApiVersioning();

The AddOData() above belongs to OData itself and opts into OData. It does not version it.

How to Fix Violations

Call AddOData() on the builder returned by AddApiVersioning().

var builder = WebApplication.CreateBuilder( args );

builder.Services.AddControllers().AddOData();
builder.Services.AddApiVersioning().AddOData(
    options => options.AddRouteComponents( "api" ) );

AddODataApiExplorer() registers the versioned OData services it needs on its own, which is a supported way to describe a versioned OData API without taking on the rest of them.

When to Suppress Warnings

It is safe to suppress this rule only if the OData APIs in the application are deliberately left unversioned. Versioning OData changes how its routes are resolved, so applying the fix to an existing service is a breaking change for clients that do not send a version.