Skip to content

Commit 8815723

Browse files
Chris Martinezcommonsensesoftware
authored andcommitted
Update convention-based link generation. Fixes #265
1 parent b437b75 commit 8815723

File tree

2 files changed

+35
-19
lines changed

2 files changed

+35
-19
lines changed

src/Microsoft.AspNet.OData.Versioning.ApiExplorer/Description/ODataRouteBuilder.cs

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ sealed class ODataRouteBuilder
2121
{
2222
static readonly Type ODataQueryOptionsType = typeof( ODataQueryOptions );
2323
static readonly Type ODataActionParametersType = typeof( ODataActionParameters );
24-
static readonly Type GeographyType = typeof( Spatial.Geography );
25-
static readonly Type GeometryType = typeof( Spatial.Geometry );
26-
static readonly Dictionary<Type, string> quotedTypes = new Dictionary<Type, string>()
24+
static readonly Dictionary<Type, string> quotedTypes = new Dictionary<Type, string>( new TypeComparer() )
2725
{
2826
[typeof( string )] = string.Empty,
2927
[typeof( TimeSpan )] = "duration",
3028
[typeof( byte[] )] = "binary",
29+
[typeof( Spatial.Geography )] = "geography",
30+
[typeof( Spatial.Geometry )] = "geometry",
3131
};
3232

3333
internal ODataRouteBuilder( ODataRouteBuilderContext context )
@@ -220,20 +220,23 @@ void ExpandParameterTemplate( StringBuilder template, IEdmTypeReference typeRefe
220220
Contract.Requires( !IsNullOrEmpty( name ) );
221221

222222
var typeDef = typeReference.Definition;
223+
var offset = template.Length;
223224

224225
template.Append( "{" );
225226
template.Append( name );
226227
template.Append( "}" );
227228

228229
if ( typeDef.TypeKind == EdmTypeKind.Enum )
229230
{
230-
template.Insert( 0, '\'' );
231+
var fullName = typeReference.FullName();
231232

232233
if ( !Context.AllowUnqualifiedEnum )
233234
{
234-
template.Insert( 0, typeReference.FullName() );
235+
template.Insert( offset, fullName );
236+
offset += fullName.Length;
235237
}
236238

239+
template.Insert( offset, '\'' );
237240
template.Append( '\'' );
238241
return;
239242
}
@@ -242,18 +245,9 @@ void ExpandParameterTemplate( StringBuilder template, IEdmTypeReference typeRefe
242245

243246
if ( quotedTypes.TryGetValue( type, out var prefix ) )
244247
{
245-
template.Insert( 0, '\'' );
246-
template.Insert( 0, prefix );
247-
template.Append( '\'' );
248-
}
249-
else if ( GeographyType.IsAssignableFrom( type ) )
250-
{
251-
template.Insert( 0, "geography'" );
252-
template.Append( '\'' );
253-
}
254-
else if ( GeometryType.IsAssignableFrom( type ) )
255-
{
256-
template.Insert( 0, "geometry'" );
248+
template.Insert( offset, prefix );
249+
offset += prefix.Length;
250+
template.Insert( offset, '\'' );
257251
template.Append( '\'' );
258252
}
259253
}
@@ -364,5 +358,27 @@ static bool IsFunctionParameter( IEdmOperation operation, ApiParameterDescriptio
364358

365359
return operation.Parameters.Any( p => p.Name.Equals( name, OrdinalIgnoreCase ) );
366360
}
361+
362+
sealed class TypeComparer : IEqualityComparer<Type>
363+
{
364+
public bool Equals( Type x, Type y ) => x.IsAssignableFrom( y );
365+
366+
public int GetHashCode( Type obj )
367+
{
368+
if ( obj.BaseType.Equals( typeof( ValueType ) ) || obj.BaseType.Equals( typeof( Array ) ) )
369+
{
370+
return obj.GetHashCode();
371+
}
372+
373+
var baseType = typeof( object );
374+
375+
while ( !obj.BaseType.Equals( baseType ) )
376+
{
377+
obj = obj.BaseType;
378+
}
379+
380+
return obj.GetHashCode();
381+
}
382+
}
367383
}
368384
}

src/Microsoft.AspNet.OData.Versioning.ApiExplorer/Microsoft.AspNet.OData.Versioning.ApiExplorer.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<VersionPrefix>1.2.0</VersionPrefix>
4+
<VersionPrefix>1.2.1</VersionPrefix>
55
<AssemblyVersion>1.2.0.0</AssemblyVersion>
66
<TargetFramework>net45</TargetFramework>
77
<AssemblyTitle>Microsoft ASP.NET Web API Versioned API Explorer for OData v4.0</AssemblyTitle>
@@ -12,7 +12,7 @@
1212
</PropertyGroup>
1313

1414
<ItemGroup>
15-
<ReleaseNotes Include="Support substituting API version parameter in route templates (Issue 247)" />
15+
<ReleaseNotes Include="Fix convention-based link generation (Issue 265)" />
1616
</ItemGroup>
1717

1818
<ItemGroup>

0 commit comments

Comments
 (0)