From 8ce7bd171dd48de75f922cf3acb09d35477830ee Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Fri, 26 Apr 2019 07:54:27 +1200 Subject: [PATCH] Make RouteOptions.EndpointDataSources internal (#9708) --- ...rosoft.AspNetCore.Routing.netcoreapp3.0.cs | 1 - src/Http/Routing/src/RouteOptions.cs | 19 +++++++++-- .../test/UnitTests/RouteOptionsTests.cs | 32 +++++++++++++++++++ .../MvcApplicationBuilderExtensionsTest.cs | 6 ++-- 4 files changed, 51 insertions(+), 7 deletions(-) diff --git a/src/Http/Routing/ref/Microsoft.AspNetCore.Routing.netcoreapp3.0.cs b/src/Http/Routing/ref/Microsoft.AspNetCore.Routing.netcoreapp3.0.cs index 3f52fbc0b1..96d44ee48c 100644 --- a/src/Http/Routing/ref/Microsoft.AspNetCore.Routing.netcoreapp3.0.cs +++ b/src/Http/Routing/ref/Microsoft.AspNetCore.Routing.netcoreapp3.0.cs @@ -319,7 +319,6 @@ namespace Microsoft.AspNetCore.Routing public RouteOptions() { } public bool AppendTrailingSlash { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } } public System.Collections.Generic.IDictionary ConstraintMap { get { throw null; } set { } } - public System.Collections.Generic.ICollection EndpointDataSources { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } } public bool LowercaseQueryStrings { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } } public bool LowercaseUrls { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } } public bool SuppressCheckForUnhandledSecurityMetadata { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } } diff --git a/src/Http/Routing/src/RouteOptions.cs b/src/Http/Routing/src/RouteOptions.cs index d83ea590f0..3d06d5525b 100644 --- a/src/Http/Routing/src/RouteOptions.cs +++ b/src/Http/Routing/src/RouteOptions.cs @@ -3,13 +3,28 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using Microsoft.AspNetCore.Routing.Constraints; namespace Microsoft.AspNetCore.Routing { public class RouteOptions { - public ICollection EndpointDataSources { get; internal set; } + private IDictionary _constraintTypeMap = GetDefaultConstraintMap(); + private ICollection _endpointDataSources; + + /// + /// Gets a collection of instances configured with routing. + /// + internal ICollection EndpointDataSources + { + get + { + Debug.Assert(_endpointDataSources != null, "Endpoint data sources should have been set in DI."); + return _endpointDataSources; + } + set => _endpointDataSources = value; + } /// /// Gets or sets a value indicating whether all generated paths URLs are lower-case. @@ -48,8 +63,6 @@ namespace Microsoft.AspNetCore.Routing /// public bool SuppressCheckForUnhandledSecurityMetadata { get; set; } - private IDictionary _constraintTypeMap = GetDefaultConstraintMap(); - public IDictionary ConstraintMap { get diff --git a/src/Http/Routing/test/UnitTests/RouteOptionsTests.cs b/src/Http/Routing/test/UnitTests/RouteOptionsTests.cs index 6dc6ceb2ad..0a480b41ce 100644 --- a/src/Http/Routing/test/UnitTests/RouteOptionsTests.cs +++ b/src/Http/Routing/test/UnitTests/RouteOptionsTests.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; @@ -27,6 +28,37 @@ namespace Microsoft.AspNetCore.Routing.Tests Assert.Equal("TestRouteConstraint", accessor.Value.ConstraintMap["foo"].Name); } + [Fact] + public void EndpointDataSources_WithDependencyInjection_AddedDataSourcesAddedToEndpointDataSource() + { + // Arrange + var services = new ServiceCollection(); + services.AddOptions(); + services.AddRouting(); + var serviceProvider = services.BuildServiceProvider(); + + var endpoint1 = new Endpoint((c) => Task.CompletedTask, EndpointMetadataCollection.Empty, string.Empty); + var endpoint2 = new Endpoint((c) => Task.CompletedTask, EndpointMetadataCollection.Empty, string.Empty); + + var options = serviceProvider.GetRequiredService>().Value; + var endpointDataSource = serviceProvider.GetRequiredService(); + + // Act 1 + options.EndpointDataSources.Add(new DefaultEndpointDataSource(endpoint1)); + + // Assert 1 + var result = Assert.Single(endpointDataSource.Endpoints); + Assert.Same(endpoint1, result); + + // Act 2 + options.EndpointDataSources.Add(new DefaultEndpointDataSource(endpoint2)); + + // Assert 2 + Assert.Collection(endpointDataSource.Endpoints, + ep => Assert.Same(endpoint1, ep), + ep => Assert.Same(endpoint2, ep)); + } + private class TestRouteConstraint : IRouteConstraint { public TestRouteConstraint(string pattern) diff --git a/src/Mvc/Mvc.Core/test/Builder/MvcApplicationBuilderExtensionsTest.cs b/src/Mvc/Mvc.Core/test/Builder/MvcApplicationBuilderExtensionsTest.cs index 11dee90d69..fd9c38a8a0 100644 --- a/src/Mvc/Mvc.Core/test/Builder/MvcApplicationBuilderExtensionsTest.cs +++ b/src/Mvc/Mvc.Core/test/Builder/MvcApplicationBuilderExtensionsTest.cs @@ -55,10 +55,10 @@ namespace Microsoft.AspNetCore.Mvc.Core.Builder template: "{controller=Home}/{action=Index}/{id?}"); }); - var routeOptions = appBuilder.ApplicationServices - .GetRequiredService>(); + var endpointDataSource = appBuilder.ApplicationServices + .GetRequiredService(); - Assert.Empty(routeOptions.Value.EndpointDataSources); + Assert.Empty(endpointDataSource.Endpoints); } [Fact]