Skip to content

Commit d918903

Browse files
Handle 'PostTo' navigation properties. Fixes #742
1 parent 0ccd38a commit d918903

File tree

1 file changed

+40
-8
lines changed

1 file changed

+40
-8
lines changed

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

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ static bool IsActionOrFunction( IEdmEntitySet? entitySet, IEdmSingleton? singlet
146146
}
147147

148148
const string ActionMethod = "Post";
149+
const string AddNavigationLink = ActionMethod + "To";
149150
const string FunctionMethod = "Get";
150151

151152
if ( ActionMethod.Equals( method, OrdinalIgnoreCase ) && actionName != ActionMethod )
@@ -156,7 +157,7 @@ static bool IsActionOrFunction( IEdmEntitySet? entitySet, IEdmSingleton? singlet
156157
return false;
157158
}
158159

159-
return !IsNavigationPropertyLink( entitySet, singleton, actionName, ActionMethod );
160+
return !IsNavigationPropertyLink( entitySet, singleton, actionName, ActionMethod, AddNavigationLink );
160161
}
161162
else if ( FunctionMethod.Equals( method, OrdinalIgnoreCase ) && actionName != FunctionMethod )
162163
{
@@ -172,7 +173,7 @@ static bool IsActionOrFunction( IEdmEntitySet? entitySet, IEdmSingleton? singlet
172173
return false;
173174
}
174175

175-
static bool IsNavigationPropertyLink( IEdmEntitySet? entitySet, IEdmSingleton? singleton, string actionName, string method )
176+
static bool IsNavigationPropertyLink( IEdmEntitySet? entitySet, IEdmSingleton? singleton, string actionName, params string[] methods )
176177
{
177178
var entities = new List<IEdmEntityType>( capacity: 2 );
178179

@@ -191,21 +192,52 @@ static bool IsNavigationPropertyLink( IEdmEntitySet? entitySet, IEdmSingleton? s
191192
}
192193
}
193194

195+
var propertyNames = default( List<string> );
196+
194197
for ( var i = 0; i < entities.Count; i++ )
195198
{
196199
var entity = entities[i];
197200

198-
if ( actionName == ( method + entity.Name ) )
201+
for ( var j = 0; j < methods.Length; j++ )
199202
{
200-
return true;
201-
}
203+
var method = methods[j];
202204

203-
foreach ( var property in entity.NavigationProperties() )
204-
{
205-
if ( actionName.StartsWith( method + property.Name, OrdinalIgnoreCase ) )
205+
if ( actionName == ( method + entity.Name ) )
206206
{
207207
return true;
208208
}
209+
210+
if ( j == 0 )
211+
{
212+
if ( propertyNames is null )
213+
{
214+
propertyNames = new();
215+
}
216+
else
217+
{
218+
propertyNames.Clear();
219+
}
220+
221+
foreach ( var property in entity.NavigationProperties() )
222+
{
223+
if ( actionName.StartsWith( method + property.Name, OrdinalIgnoreCase ) )
224+
{
225+
return true;
226+
}
227+
228+
propertyNames.Add( property.Name );
229+
}
230+
}
231+
else if ( propertyNames is not null )
232+
{
233+
for ( var k = 0; k < propertyNames.Count; k++ )
234+
{
235+
if ( actionName.StartsWith( method + propertyNames[k], OrdinalIgnoreCase ) )
236+
{
237+
return true;
238+
}
239+
}
240+
}
209241
}
210242
}
211243

0 commit comments

Comments
 (0)