Skip to content

Commit 93aa313

Browse files
Chris Martinezcommonsensesoftware
Chris Martinez
authored andcommitted
Map OData-specific route name property. Fixes #670
1 parent c2bdc60 commit 93aa313

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

src/Microsoft.AspNetCore.OData.Versioning.ApiExplorer/AspNetCore.Mvc/ApiExplorer/ODataApiDescriptionProvider.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,8 @@ protected virtual void ExploreQueryOptions( IEnumerable<ApiDescription> apiDescr
249249
}
250250

251251
static bool IsMappedTo( ControllerActionDescriptor action, ODataRouteMapping mapping ) =>
252-
action.AttributeRouteInfo != null &&
253-
StringComparer.OrdinalIgnoreCase.Equals( action.AttributeRouteInfo.Name, mapping.RouteName );
252+
action.AttributeRouteInfo is ODataAttributeRouteInfo routeInfo &&
253+
StringComparer.OrdinalIgnoreCase.Equals( routeInfo.RouteName, mapping.RouteName );
254254

255255
static Type? GetDeclaredReturnType( ControllerActionDescriptor action )
256256
{
@@ -346,7 +346,7 @@ IEnumerable<ApiDescription> NewODataApiDescriptions(
346346
var responseMetadataAttributes = GetResponseMetadataAttributes( action );
347347
var declaredReturnType = GetDeclaredReturnType( action );
348348
var runtimeReturnType = GetRuntimeReturnType( declaredReturnType! );
349-
var apiResponseTypes = GetApiResponseTypes( responseMetadataAttributes!, runtimeReturnType!, mapping.Services );
349+
var apiResponseTypes = GetApiResponseTypes( responseMetadataAttributes!, runtimeReturnType!, mapping.Services, version );
350350
var routeContext = new ODataRouteBuilderContext( mapping, version, action, Options ) { ModelMetadataProvider = MetadataProvider };
351351

352352
if ( routeContext.IsRouteExcluded )
@@ -640,7 +640,8 @@ static void UpdateBindingInfo( ApiParameterContext context, ParameterDescriptor
640640
IReadOnlyList<ApiResponseType> GetApiResponseTypes(
641641
IReadOnlyList<IApiResponseMetadataProvider> responseMetadataAttributes,
642642
Type responseType,
643-
IServiceProvider serviceProvider )
643+
IServiceProvider serviceProvider,
644+
ApiVersion version )
644645
{
645646
var results = new List<ApiResponseType>();
646647
var objectTypes = new Dictionary<int, Type>();
@@ -682,7 +683,7 @@ IReadOnlyList<ApiResponseType> GetApiResponseTypes(
682683

683684
if ( objectType.Value == typeof( void ) )
684685
{
685-
type = objectType.Value.SubstituteIfNecessary( new TypeSubstitutionContext( serviceProvider, ModelTypeBuilder ) );
686+
type = objectType.Value.SubstituteIfNecessary( new TypeSubstitutionContext( serviceProvider, ModelTypeBuilder, version ) );
686687

687688
results.Add( new ApiResponseType()
688689
{
@@ -694,7 +695,7 @@ IReadOnlyList<ApiResponseType> GetApiResponseTypes(
694695
continue;
695696
}
696697

697-
type = objectType.Value.SubstituteIfNecessary( new TypeSubstitutionContext( serviceProvider, ModelTypeBuilder ) );
698+
type = objectType.Value.SubstituteIfNecessary( new TypeSubstitutionContext( serviceProvider, ModelTypeBuilder, version ) );
698699

699700
var apiResponseType = new ApiResponseType()
700701
{

src/Microsoft.AspNetCore.OData.Versioning/AspNet.OData/Routing/ODataAttributeRouteInfo.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ public ODataAttributeRouteInfo()
2525
/// <value>The <see cref="ODataPathTemplate">OData path template</see> for the action.</value>
2626
public ODataPathTemplate? ODataTemplate { get; set; }
2727

28+
/// <summary>
29+
/// Gets or sets the corresponding route name.
30+
/// </summary>
31+
/// <value>The associated route name.</value>
32+
/// <remarks>This property is different from <see cref="AttributeRouteInfo.Name"/> in that
33+
/// attribute routes may have a unique key per route definition. OData routes, on the other
34+
/// hand, use OData-specific conventions on a single registered route.</remarks>
35+
public string? RouteName { get; set; }
36+
2837
/// <summary>
2938
/// Gets or sets the corresponding route prefix.
3039
/// </summary>

src/Microsoft.AspNetCore.OData.Versioning/AspNet.OData/Routing/ODataRouteBindingInfoConvention.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,9 @@ static void UpdateBindingInfo(
147147
var handler = mapping.Services.GetRequiredService<IODataPathTemplateHandler>();
148148
var routeInfo = new ODataAttributeRouteInfo()
149149
{
150-
Name = mapping.RouteName,
151150
Template = template,
152151
ODataTemplate = handler.ParseTemplate( path, mapping.Services ),
152+
RouteName = mapping.RouteName,
153153
RoutePrefix = mapping.RoutePrefix,
154154
};
155155

@@ -189,9 +189,9 @@ void UpdateBindingInfo(
189189
var template = templates[i];
190190
var routeInfo = new ODataAttributeRouteInfo()
191191
{
192-
Name = mapping.RouteName,
193192
Template = template.RouteTemplate,
194193
ODataTemplate = template.PathTemplate,
194+
RouteName = mapping.RouteName,
195195
RoutePrefix = mapping.RoutePrefix,
196196
};
197197

@@ -368,7 +368,7 @@ public bool Equals( ODataAttributeRouteInfo? x, ODataAttributeRouteInfo? y )
368368
var comparer = StringComparer.OrdinalIgnoreCase;
369369

370370
return comparer.Equals( x.Template, y.Template ) &&
371-
comparer.Equals( x.Name, y.Name );
371+
comparer.Equals( x.RouteName, y.RouteName );
372372
}
373373

374374
public int GetHashCode( ODataAttributeRouteInfo obj ) =>

0 commit comments

Comments
 (0)