AV0022: Missing AddOData
| Value | |
|---|---|
| Rule ID | AV0022 |
| Category | Usage |
| Fix is | Breaking |
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.