@@ -6,6 +6,7 @@ namespace Asp.Versioning.OData;
6
6
using Microsoft . AspNetCore . OData . Routing . Template ;
7
7
using Microsoft . OData . Edm ;
8
8
using Microsoft . OData . UriParser ;
9
+ using System . Runtime . CompilerServices ;
9
10
10
11
/// <summary>
11
12
/// Represents a versioned <see cref="IODataTemplateTranslator">OData template translator</see>.
@@ -30,9 +31,7 @@ public sealed class VersionedODataTemplateTranslator : IODataTemplateTranslator
30
31
31
32
if ( apiVersion == null )
32
33
{
33
- var metadata = context . Endpoint . Metadata . GetMetadata < ApiVersionMetadata > ( ) ;
34
-
35
- if ( metadata == null || ! metadata . IsApiVersionNeutral )
34
+ if ( ! IsVersionNeutral ( context ) )
36
35
{
37
36
return default ;
38
37
}
@@ -42,7 +41,13 @@ public sealed class VersionedODataTemplateTranslator : IODataTemplateTranslator
42
41
var model = context . Model ;
43
42
var otherApiVersion = model . GetAnnotationValue < ApiVersionAnnotation > ( model ) ? . ApiVersion ;
44
43
45
- if ( ! apiVersion . Equals ( otherApiVersion ) )
44
+ // HACK: a version-neutral endpoint can fail to match here because odata tries to match the
45
+ // first endpoint metadata when there could be multiple. such an endpoint is expected to be
46
+ // the same in all versions so allow it to flow through. revisit if/when odata fixes this.
47
+ //
48
+ // REF: https://github.com/OData/AspNetCoreOData/issues/753
49
+ // REF: https://github.com/OData/AspNetCoreOData/blob/main/src/Microsoft.AspNetCore.OData/Routing/ODataRoutingMatcherPolicy.cs#L86
50
+ if ( ! apiVersion . Equals ( otherApiVersion ) && ! IsVersionNeutral ( context ) )
46
51
{
47
52
return default ;
48
53
}
@@ -58,4 +63,9 @@ public sealed class VersionedODataTemplateTranslator : IODataTemplateTranslator
58
63
59
64
return new ( context . Segments ) ;
60
65
}
66
+
67
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
68
+ private static bool IsVersionNeutral ( ODataTemplateTranslateContext context ) =>
69
+ context . Endpoint . Metadata . GetMetadata < ApiVersionMetadata > ( ) is ApiVersionMetadata metadata
70
+ && metadata . IsApiVersionNeutral ;
61
71
}
0 commit comments