Skip to content

Commit 3ef8233

Browse files
Fix default visibility of OData endpoints
1 parent 8a7e238 commit 3ef8233

3 files changed

Lines changed: 51 additions & 0 deletions

File tree

src/AspNetCore/OData/src/Asp.Versioning.OData.ApiExplorer/ApiExplorer/ODataApiDescriptionProvider.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ public virtual void OnProvidersExecuted( ApiDescriptionProviderContext context )
9797
ArgumentNullException.ThrowIfNull( context );
9898

9999
var results = context.Results;
100+
101+
if ( results.Count == 0 )
102+
{
103+
return;
104+
}
105+
100106
var visited = new HashSet<ApiDescription>( capacity: results.Count, new ApiDescriptionComparer() );
101107

102108
for ( var i = results.Count - 1; i >= 0; i-- )
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright (c) .NET Foundation and contributors. All rights reserved.
2+
3+
#pragma warning disable CA1812
4+
5+
namespace Asp.Versioning.ApiExplorer;
6+
7+
using Asp.Versioning.ApplicationModels;
8+
using Microsoft.AspNetCore.Mvc.ApplicationModels;
9+
10+
internal sealed class ODataApplicationModelProvider : IApplicationModelProvider
11+
{
12+
public int Order => 0;
13+
14+
public void OnProvidersExecuted( ApplicationModelProviderContext context ) { }
15+
16+
public void OnProvidersExecuting( ApplicationModelProviderContext context )
17+
{
18+
ArgumentNullException.ThrowIfNull( context );
19+
20+
var application = context.Result;
21+
var controllers = application.Controllers;
22+
var odata = new ODataControllerSpecification();
23+
var convention = new ApiVisibilityConvention();
24+
25+
for ( var i = 0; i < controllers.Count; i++ )
26+
{
27+
var controller = controllers[i];
28+
29+
if ( !odata.IsSatisfiedBy( controller ) )
30+
{
31+
continue;
32+
}
33+
34+
var actions = controller.Actions;
35+
36+
for ( var j = 0; j < actions.Count; j++ )
37+
{
38+
convention.Apply( actions[j] );
39+
}
40+
}
41+
}
42+
}

src/AspNetCore/OData/src/Asp.Versioning.OData.ApiExplorer/DependencyInjection/IApiVersioningBuilderExtensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ namespace Microsoft.Extensions.DependencyInjection;
88
using Asp.Versioning.ApiExplorer;
99
using Asp.Versioning.Conventions;
1010
using Asp.Versioning.OData;
11+
using Microsoft.AspNetCore.Mvc;
1112
using Microsoft.AspNetCore.Mvc.ApiExplorer;
13+
using Microsoft.AspNetCore.Mvc.ApplicationModels;
1214
using Microsoft.Extensions.DependencyInjection.Extensions;
1315
using Microsoft.Extensions.Options;
1416
using static Microsoft.Extensions.DependencyInjection.ServiceDescriptor;
@@ -59,6 +61,7 @@ private static void AddApiExplorerServices( IApiVersioningBuilder builder )
5961
builder.Services.AddModelConfigurationsAsServices();
6062
services.TryAddSingleton<IModelTypeBuilder, DefaultModelTypeBuilder>();
6163
services.TryAddSingleton<IOptionsFactory<ODataApiExplorerOptions>, ODataApiExplorerOptionsFactory>();
64+
services.TryAddEnumerable( Transient<IApplicationModelProvider, Asp.Versioning.ApiExplorer.ODataApplicationModelProvider>() );
6265
services.TryAddEnumerable( Transient<IApiDescriptionProvider, PartialODataDescriptionProvider>() );
6366
services.TryAddEnumerable( Transient<IApiDescriptionProvider, ODataApiDescriptionProvider>() );
6467
services.TryAddEnumerable( Transient<IModelConfiguration, ImplicitModelBoundSettingsConvention>() );

0 commit comments

Comments
 (0)