Skip to content

Commit 8504703

Browse files
author
Chris Martinez
committed
Support separate client and server OData route template generation. Fixes #502
1 parent 34ad4d3 commit 8504703

File tree

5 files changed

+46
-14
lines changed

5 files changed

+46
-14
lines changed

src/Common.OData.ApiExplorer/AspNet.OData/Routing/ODataRouteBuilder.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#endif
1919
using static Microsoft.OData.ODataUrlKeyDelimiter;
2020
using static ODataRouteActionType;
21+
using static ODataRouteTemplateGenerationKind;
2122
using static System.Linq.Enumerable;
2223
using static System.String;
2324
using static System.StringComparison;
@@ -298,6 +299,11 @@ void ExpandParameterTemplate( StringBuilder template, IEdmTypeReference typeRefe
298299
template.Append( name );
299300
template.Append( "}" );
300301

302+
if ( Context.RouteTemplateGeneration == Server )
303+
{
304+
return;
305+
}
306+
301307
if ( typeDef.TypeKind == EdmTypeKind.Enum )
302308
{
303309
var fullName = typeReference.FullName();

src/Common.OData.ApiExplorer/AspNet.OData/Routing/ODataRouteBuilderContext.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
using System.Web.Http.Dispatcher;
2323
using ControllerActionDescriptor = System.Web.Http.Controllers.HttpActionDescriptor;
2424
#endif
25+
using static ODataRouteTemplateGenerationKind;
2526

2627
sealed partial class ODataRouteBuilderContext
2728
{
@@ -34,8 +35,12 @@ sealed partial class ODataRouteBuilderContext
3435
internal ODataApiExplorerOptions Options { get; }
3536

3637
internal IList<ApiParameterDescription> ParameterDescriptions { get; }
38+
39+
internal ODataRouteTemplateGenerationKind RouteTemplateGeneration { get; } = Client;
3740
#else
3841
internal IList<ParameterDescriptor> ParameterDescriptions => ActionDescriptor.Parameters;
42+
43+
internal ODataRouteTemplateGenerationKind RouteTemplateGeneration { get; } = Server;
3944
#endif
4045

4146
internal IEdmModel EdmModel { get; }
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace Microsoft.AspNet.OData.Routing
2+
{
3+
using System;
4+
5+
internal enum ODataRouteTemplateGenerationKind
6+
{
7+
Client,
8+
Server,
9+
}
10+
}

src/Common.OData.ApiExplorer/Common.OData.ApiExplorer.projitems

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
<Compile Include="$(MSBuildThisFileDirectory)AspNet.OData\Routing\ODataRouteActionType.cs" />
4343
<Compile Include="$(MSBuildThisFileDirectory)AspNet.OData\Routing\ODataRouteBuilder.cs" />
4444
<Compile Include="$(MSBuildThisFileDirectory)AspNet.OData\Routing\ODataRouteBuilderContext.cs" />
45+
<Compile Include="$(MSBuildThisFileDirectory)AspNet.OData\Routing\ODataRouteTemplateGenerationKind.cs" />
4546
<Compile Include="$(MSBuildThisFileDirectory)AspNet.OData\StructuredTypeResolver.cs" />
4647
<Compile Include="$(MSBuildThisFileDirectory)AspNet.OData\TypeSubstitutionContext.cs" />
4748
<Compile Include="$(MSBuildThisFileDirectory)AspNet.OData\TypeExtensions.cs" />

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

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -497,36 +497,44 @@ void UpdateBindingInfo( ApiParameterContext context, ParameterDescriptor paramet
497497
var parameterType = parameter.ParameterType;
498498
var bindingInfo = parameter.BindingInfo;
499499

500-
if ( bindingInfo != null )
500+
bool IsSpecialBindingSource( BindingInfo info, Type type )
501501
{
502-
if ( ( parameterType.IsODataQueryOptions() || parameterType.IsODataPath() ) && bindingInfo.BindingSource == Custom )
502+
if ( info == null )
503503
{
504-
bindingInfo.BindingSource = Special;
504+
return false;
505505
}
506506

507-
return;
507+
if ( ( type.IsODataQueryOptions() || type.IsODataPath() ) && info.BindingSource == Custom )
508+
{
509+
info.BindingSource = Special;
510+
return true;
511+
}
512+
513+
return false;
508514
}
509515

510-
parameter.BindingInfo = bindingInfo = new BindingInfo() { BindingSource = metadata.BindingSource };
516+
if ( IsSpecialBindingSource( bindingInfo, parameterType ) )
517+
{
518+
return;
519+
}
511520

512-
if ( bindingInfo.BindingSource != null )
521+
if ( bindingInfo == null )
513522
{
514-
if ( ( parameterType.IsODataQueryOptions() || parameterType.IsODataPath() ) && bindingInfo.BindingSource == Custom )
523+
parameter.BindingInfo = bindingInfo = new BindingInfo() { BindingSource = metadata.BindingSource };
524+
525+
if ( IsSpecialBindingSource( bindingInfo, parameterType ) )
515526
{
516-
bindingInfo.BindingSource = Special;
527+
return;
517528
}
518-
519-
return;
520529
}
521530

522531
var key = default( IEdmNamedElement );
523532
var paramName = parameter.Name;
524-
var source = Query;
533+
var source = bindingInfo.BindingSource;
525534

526535
switch ( context.RouteContext.ActionType )
527536
{
528537
case EntitySet:
529-
530538
var keys = context.RouteContext.EntitySet.EntityType().Key().ToArray();
531539

532540
key = keys.FirstOrDefault( k => k.Name.Equals( paramName, OrdinalIgnoreCase ) );
@@ -553,7 +561,6 @@ void UpdateBindingInfo( ApiParameterContext context, ParameterDescriptor paramet
553561
break;
554562
case BoundOperation:
555563
case UnboundOperation:
556-
557564
var operation = context.RouteContext.Operation;
558565

559566
if ( operation == null )
@@ -575,10 +582,13 @@ void UpdateBindingInfo( ApiParameterContext context, ParameterDescriptor paramet
575582
source = Path;
576583
}
577584

585+
break;
586+
default:
587+
source = Query;
578588
break;
579589
}
580590

581-
bindingInfo.BindingSource = source;
591+
bindingInfo.BindingSource = source ?? Query;
582592
parameter.BindingInfo = bindingInfo;
583593
}
584594

0 commit comments

Comments
 (0)