File tree Expand file tree Collapse file tree 3 files changed +67
-1
lines changed
src/Microsoft.AspNet.OData.Versioning/System.Web.Http
test/Microsoft.AspNet.OData.Versioning.Tests/System.Web.OData Expand file tree Collapse file tree 3 files changed +67
-1
lines changed Original file line number Diff line number Diff line change @@ -768,5 +768,52 @@ static IContainerBuilder CreateContainerBuilderWithDefaultServices( this HttpCon
768
768
769
769
return builder ;
770
770
}
771
+
772
+ /// <summary>
773
+ /// Gets the configured entity data model (EDM) for the specified API version.
774
+ /// </summary>
775
+ /// <param name="configuration">The server configuration.</param>
776
+ /// <param name="apiVersion">The <see cref="ApiVersion">API version</see> to get the model for.</param>
777
+ /// <returns>The matching <see cref="IEdmModel">EDM model</see> or <c>null</c>.</returns>
778
+ public static IEdmModel GetEdmModel ( this HttpConfiguration configuration , ApiVersion apiVersion )
779
+ {
780
+ Arg . NotNull ( configuration , nameof ( configuration ) ) ;
781
+ Arg . NotNull ( apiVersion , nameof ( apiVersion ) ) ;
782
+
783
+ var allRoutes = configuration . Routes ;
784
+ var routes = new KeyValuePair < string , IHttpRoute > [ allRoutes . Count ] ;
785
+ var containers = configuration . GetRootContainerMappings ( ) ;
786
+
787
+ allRoutes . CopyTo ( routes , 0 ) ;
788
+
789
+ foreach ( var route in routes )
790
+ {
791
+ if ( ! ( route . Value is ODataRoute odataRoute ) )
792
+ {
793
+ continue ;
794
+ }
795
+
796
+ if ( ! containers . TryGetValue ( route . Key , out var serviceProvider ) )
797
+ {
798
+ continue ;
799
+ }
800
+
801
+ var model = serviceProvider . GetService < IEdmModel > ( ) ;
802
+
803
+ if ( model ? . EntityContainer == null )
804
+ {
805
+ continue ;
806
+ }
807
+
808
+ var modelApiVersion = model . GetAnnotationValue < ApiVersionAnnotation > ( model ) ? . ApiVersion ;
809
+
810
+ if ( modelApiVersion == apiVersion )
811
+ {
812
+ return model ;
813
+ }
814
+ }
815
+
816
+ return null ;
817
+ }
771
818
}
772
819
}
Original file line number Diff line number Diff line change 12
12
using System . Web . OData . Routing . Conventions ;
13
13
using static Microsoft . OData . ServiceLifetime ;
14
14
15
- internal static class IContainerBuilderExtensions
15
+ static class IContainerBuilderExtensions
16
16
{
17
17
internal static void InitializeAttributeRouting ( this IServiceProvider serviceProvider ) => serviceProvider . GetServices < IODataRoutingConvention > ( ) ;
18
18
Original file line number Diff line number Diff line change @@ -96,6 +96,25 @@ public void map_versioned_odata_routes_should_return_expected_results()
96
96
batchRoute . RouteTemplate . Should ( ) . Be ( "api/$batch" ) ;
97
97
}
98
98
99
+ [ Theory ]
100
+ [ InlineData ( 0 , "1.0" ) ]
101
+ [ InlineData ( 1 , "2.0" ) ]
102
+ public void get_edm_model_should_retrieve_configured_model_by_api_version ( int modelIndex , string apiVersionValue )
103
+ {
104
+ // arrange
105
+ var apiVersion = ApiVersion . Parse ( apiVersionValue ) ;
106
+ var configuration = new HttpConfiguration ( ) ;
107
+ var models = CreateModels ( configuration ) . ToArray ( ) ;
108
+
109
+ configuration . MapVersionedODataRoutes ( "odata" , "api" , models ) ;
110
+
111
+ // act
112
+ var model = configuration . GetEdmModel ( apiVersion ) ;
113
+
114
+ // assert
115
+ model . Should ( ) . BeSameAs ( models [ modelIndex ] ) ;
116
+ }
117
+
99
118
static IEnumerable < IEdmModel > CreateModels ( HttpConfiguration configuration )
100
119
{
101
120
var controllerTypeResolver = new Mock < IHttpControllerTypeResolver > ( ) ;
You can’t perform that action at this time.
0 commit comments