Skip to content

Commit 0e7033b

Browse files
Fixed Versioned OData Route Constraint (#9)
Updated to ensure that the matched metadata or service document routes also match the correct EDM and that controllers are implicitly matched when allowed. This fixes #7 and fixes #8.
1 parent fca6b08 commit 0e7033b

File tree

2 files changed

+48
-13
lines changed

2 files changed

+48
-13
lines changed

src/Microsoft.AspNet.OData.Versioning/Web.OData/Routing/VersionedODataPathRouteConstraint.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -153,19 +153,25 @@ public override bool Match( HttpRequestMessage request, IHttpRoute route, string
153153

154154
var requestedVersion = request.GetRequestedApiVersion() ?? GetApiVersionFromRoutePrefix( request, route );
155155

156-
if ( requestedVersion == null )
156+
if ( requestedVersion != null )
157157
{
158-
if ( IsServiceDocumentOrMetadataRoute( values ) )
159-
{
160-
var defaultApiVersion = request.GetConfiguration().GetDefaultApiVersion();
161-
request.SetRequestedApiVersion( defaultApiVersion );
162-
return base.Match( request, route, parameterName, values, routeDirection );
163-
}
158+
return ApiVersion == requestedVersion && base.Match( request, route, parameterName, values, routeDirection );
159+
}
160+
161+
var options = request.GetApiVersioningOptions();
164162

163+
if ( options.DefaultApiVersion != ApiVersion )
164+
{
165165
return false;
166166
}
167167

168-
return ApiVersion == requestedVersion && base.Match( request, route, parameterName, values, routeDirection );
168+
if ( options.AssumeDefaultVersionWhenUnspecified || IsServiceDocumentOrMetadataRoute( values ) )
169+
{
170+
request.SetRequestedApiVersion( ApiVersion );
171+
return base.Match( request, route, parameterName, values, routeDirection );
172+
}
173+
174+
return false;
169175
}
170176
}
171177
}

test/Microsoft.AspNet.OData.Versioning.Tests/Web.OData/Routing/VersionedODataPathRouteConstraintTest.cs

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,21 +92,50 @@ public void match_should_be_true_when_api_version_is_requested_in_query_string(
9292
}
9393

9494
[Theory]
95-
[InlineData( "http://localhost", null )]
96-
[InlineData( "http://localhost/$metadata", "$metadata" )]
97-
public void match_should_return_true_for_service_and_metadata_document( string requestUri, string odataPath )
95+
[InlineData( "http://localhost", null, "1.0", true )]
96+
[InlineData( "http://localhost", null, "2.0", false )]
97+
[InlineData( "http://localhost/$metadata", "$metadata", "1.0", true )]
98+
[InlineData( "http://localhost/$metadata", "$metadata", "2.0", false )]
99+
public void match_should_return_expected_result_for_service_and_metadata_document( string requestUri, string odataPath, string apiVersionValue, bool expected )
98100
{
99101
// arrange
102+
var apiVersion = Parse( apiVersionValue );
100103
var model = EmptyModel;
101104
var request = new HttpRequestMessage( Get, requestUri );
102105
var values = new Dictionary<string, object>() { { "odataPath", odataPath } };
103-
var constraint = NewVersionedODataPathRouteConstraint( request, model, Default );
106+
var constraint = NewVersionedODataPathRouteConstraint( request, model, apiVersion );
104107

105108
// act
106109
var result = constraint.Match( request, null, null, values, UriResolution );
107110

108111
// assert
109-
result.Should().BeTrue();
112+
result.Should().Be( expected );
113+
}
114+
115+
[Theory]
116+
[InlineData( true, true )]
117+
[InlineData( false, false )]
118+
public void match_should_return_expected_result_when_controller_is_implicitly_versioned( bool allowImplicitVersioning, bool expected )
119+
{
120+
// arrange
121+
var apiVersion = new ApiVersion( 2, 0 );
122+
var model = TestModel;
123+
var request = new HttpRequestMessage( Get, $"http://localhost/Tests(1)" );
124+
var values = new Dictionary<string, object>() { { "odataPath", "Tests(1)" } };
125+
var constraint = NewVersionedODataPathRouteConstraint( request, model, apiVersion );
126+
127+
request.GetConfiguration().AddApiVersioning(
128+
o =>
129+
{
130+
o.DefaultApiVersion = apiVersion;
131+
o.AssumeDefaultVersionWhenUnspecified = allowImplicitVersioning;
132+
} );
133+
134+
// act
135+
var result = constraint.Match( request, null, null, values, UriResolution );
136+
137+
// assert
138+
result.Should().Be( expected );
110139
}
111140

112141
[Theory]

0 commit comments

Comments
 (0)