From e3644b4a7e0526f246db1fc50af671c3bbfe69a1 Mon Sep 17 00:00:00 2001 From: Chris Martinez Date: Wed, 16 Nov 2022 16:51:27 -0800 Subject: [PATCH] Use 404 vs 400 when versioning only by URL and no requested versions. Fixes #911 --- .../Routing/ApiVersionPolicyJumpTable.cs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/AspNetCore/WebApi/src/Asp.Versioning.Http/Routing/ApiVersionPolicyJumpTable.cs b/src/AspNetCore/WebApi/src/Asp.Versioning.Http/Routing/ApiVersionPolicyJumpTable.cs index 950c6cd8..ed8db5bd 100644 --- a/src/AspNetCore/WebApi/src/Asp.Versioning.Http/Routing/ApiVersionPolicyJumpTable.cs +++ b/src/AspNetCore/WebApi/src/Asp.Versioning.Http/Routing/ApiVersionPolicyJumpTable.cs @@ -11,6 +11,7 @@ namespace Asp.Versioning.Routing; internal sealed class ApiVersionPolicyJumpTable : PolicyJumpTable { private readonly bool versionsByUrl; + private readonly bool versionsByUrlOnly; private readonly bool versionsByMediaTypeOnly; private readonly RouteDestination rejection; private readonly IReadOnlyDictionary destinations; @@ -32,6 +33,7 @@ internal ApiVersionPolicyJumpTable( this.parser = parser; this.options = options; versionsByUrl = routePatterns.Count > 0; + versionsByUrlOnly = source.VersionsByUrl( allowMultipleLocations: false ); versionsByMediaTypeOnly = source.VersionsByMediaType( allowMultipleLocations: false ); } @@ -61,15 +63,18 @@ public override int GetDestination( HttpContext httpContext ) return destination; } - // 2. short-circuit if a default version cannot be assumed - if ( !options.AssumeDefaultVersionWhenUnspecified ) + // 2. IApiVersionSelector cannot be used yet because there are no candidates that an + // aggregated version model can be computed from to select the 'default' API version + if ( options.AssumeDefaultVersionWhenUnspecified ) { - return rejection.Unspecified; // 400 + return rejection.AssumeDefault; } - // 3. IApiVersionSelector cannot be used yet because there are no candidates that an - // aggregated version model can be computed from to select the 'default' API version - return rejection.AssumeDefault; + // 3. unspecified + return versionsByUrlOnly + /* 404 */ ? rejection.Exit + /* 400 */ : rejection.Unspecified; + case 1: rawApiVersion = apiVersions[0];