Skip to content

Commit a71fd53

Browse files
Use 404 vs 400 when versioning only by URL and no requested versions. Fixes #911
1 parent 8832313 commit a71fd53

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

Diff for: src/AspNetCore/WebApi/src/Asp.Versioning.Http/Routing/ApiVersionPolicyJumpTable.cs

+11-6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace Asp.Versioning.Routing;
1111
internal sealed class ApiVersionPolicyJumpTable : PolicyJumpTable
1212
{
1313
private readonly bool versionsByUrl;
14+
private readonly bool versionsByUrlOnly;
1415
private readonly bool versionsByMediaTypeOnly;
1516
private readonly RouteDestination rejection;
1617
private readonly IReadOnlyDictionary<ApiVersion, int> destinations;
@@ -32,6 +33,7 @@ internal ApiVersionPolicyJumpTable(
3233
this.parser = parser;
3334
this.options = options;
3435
versionsByUrl = routePatterns.Count > 0;
36+
versionsByUrlOnly = source.VersionsByUrl( allowMultipleLocations: false );
3537
versionsByMediaTypeOnly = source.VersionsByMediaType( allowMultipleLocations: false );
3638
}
3739

@@ -61,15 +63,18 @@ public override int GetDestination( HttpContext httpContext )
6163
return destination;
6264
}
6365

64-
// 2. short-circuit if a default version cannot be assumed
65-
if ( !options.AssumeDefaultVersionWhenUnspecified )
66+
// 2. IApiVersionSelector cannot be used yet because there are no candidates that an
67+
// aggregated version model can be computed from to select the 'default' API version
68+
if ( options.AssumeDefaultVersionWhenUnspecified )
6669
{
67-
return rejection.Unspecified; // 400
70+
return rejection.AssumeDefault;
6871
}
6972

70-
// 3. IApiVersionSelector cannot be used yet because there are no candidates that an
71-
// aggregated version model can be computed from to select the 'default' API version
72-
return rejection.AssumeDefault;
73+
// 3. unspecified
74+
return versionsByUrlOnly
75+
/* 404 */ ? rejection.Exit
76+
/* 400 */ : rejection.Unspecified;
77+
7378
case 1:
7479
rawApiVersion = apiVersions[0];
7580

0 commit comments

Comments
 (0)