Skip to content

Commit 702ca46

Browse files
author
Chris Martinez
committed
Correctly build the route parameter for the "key" parameter when routing by convention. Fixes #226
1 parent 3ebb6c2 commit 702ca46

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,17 +151,37 @@ string GetEntityKeySegment()
151151

152152
convention.Append( ')' );
153153
}
154+
else
155+
{
156+
TryAddEntityKeySegmentByConvention( convention );
157+
}
154158
}
155159

156160
return convention.ToString();
157161
}
158162

159-
void ExpandParameterTemplate( StringBuilder template, IEdmStructuralProperty key )
163+
void TryAddEntityKeySegmentByConvention( StringBuilder convention )
164+
{
165+
var keys = Context.EntityKeys.ToArray();
166+
167+
if ( keys.Length != 1 || !Context.ParameterDescriptions.Any( p => ODataRouteConstants.Key.Equals( p.Name, OrdinalIgnoreCase ) ) )
168+
{
169+
return;
170+
}
171+
172+
convention.Append( '(' );
173+
ExpandParameterTemplate( convention, keys[0], ODataRouteConstants.Key );
174+
convention.Append( ')' );
175+
}
176+
177+
void ExpandParameterTemplate( StringBuilder template, IEdmStructuralProperty key ) => ExpandParameterTemplate( template, key, key?.Name );
178+
179+
void ExpandParameterTemplate( StringBuilder template, IEdmStructuralProperty key, string name )
160180
{
161181
Contract.Requires( template != null );
162182
Contract.Requires( key != null );
183+
Contract.Requires( !IsNullOrEmpty( name ) );
163184

164-
var name = key.Name;
165185
var typeDef = key.Type.Definition;
166186

167187
template.Append( "{" );

0 commit comments

Comments
 (0)