Skip to content

Commit 2112d82

Browse files
committed
Fix generation for non-containment navigation with NavigationPropertyBinding generation hints.
1 parent 953deb2 commit 2112d82

File tree

2 files changed

+16
-47
lines changed

2 files changed

+16
-47
lines changed

Templates/CSharp/Base/CollectionRequest.Base.template.tt

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -135,48 +135,6 @@ public string GetPostAsyncMethod(OdcmProperty odcmProperty, string requestBody =
135135
return string.Empty;
136136
}
137137

138-
public string GetImplicitNavigationPropertyUrlSegment(OdcmSingleton singleton, OdcmProperty odcmProperty)
139-
{
140-
/****
141-
To help the future dev in figuring this out, put a break point on the next executable line
142-
with the condition odcmProperty.Name == "teachers". The CSDL in and entities mentioned in these
143-
comments are an example of the use of this scenario.
144-
****/
145-
146-
// (1) Check that there are NavigationPropertyBindings. If there aren't any, then we know this segment is
147-
// the canonical path to the reference.
148-
if (singleton.NavigationPropertyBindings.Count == 0)
149-
{
150-
return "/" + odcmProperty.Name;
151-
}
152-
153-
// (2) Get all of the keys that contain our target path name. The keys come from the Path attribute on a
154-
// NavigationPropertyBinding. So if our target property (OdcmProperty.Name) is named 'teachers', then we need to only consider
155-
// NavigationPropertyBinding paths that end with 'teachers'. The NavigationPropertyBindings are found in the Singleton that
156-
// defines the EntitySet that contains this property. The NavigationPropertyBindings provide generation hints for how to
157-
// reference non-contained entities that are defined within the same Singleton (this statement is conjecture). This generation
158-
// hints is used to specify a reference URL for $ref call.
159-
// Example: in this case, "teachers". This should be the end part of the BindingPath.
160-
161-
IEnumerable<string> keys = singleton.NavigationPropertyBindings.Where(kvp => kvp.Key.EndsWith(odcmProperty.Name)).Select(kvp => kvp.Key);
162-
163-
string bindingTarget;
164-
165-
// The first assumption is that CSDL is added with a convention where the last path segment in NavigationPropertyBinding.Path
166-
// is named consistently in a singleton so it doesn't matter what the preceding segments are.
167-
// The second assumption is that if the last path is named consistently, the target will be consistent as well.
168-
// These may turn out to be a weak assumption. We use this assumption since we can't determine a unique path
169-
// for three or more segments since *RequestBuilder and *Request objects are generated per entity+navigation,
170-
// and not [entity+navigation]*n --> across multiple navigations.
171-
if (keys.Count() > 0 && singleton.NavigationPropertyBindings.TryGetValue(keys.First(), out bindingTarget))
172-
{
173-
// We found the target
174-
return "/" + bindingTarget;
175-
}
176-
else
177-
return "/" + odcmProperty.Name;
178-
}
179-
180138
public string GetPostAsyncMethodForReferencesRequest(OdcmProperty odcmProperty)
181139
{
182140
var sanitizedPropertyName = odcmProperty.Projection.Type.Name.GetSanitizedPropertyName(odcmProperty.Name);
@@ -195,11 +153,7 @@ public string GetPostAsyncMethodForReferencesRequest(OdcmProperty odcmProperty)
195153
// NavigationPropertyBinding generation hint. If there is, then we need to use it for
196154
// creating the URL of a reference entity in a POST body.
197155
if (serviceNavigationProperty.GetType() == typeof(OdcmSingleton))
198-
{
199-
var singleton = (OdcmSingleton)serviceNavigationProperty;
200-
201-
implicitNavigationProperty = GetImplicitNavigationPropertyUrlSegment(singleton, odcmProperty);
202-
}
156+
implicitNavigationProperty = "/" + odcmProperty.GetImplicitPropertyName((OdcmSingleton)serviceNavigationProperty);
203157

204158
var stringBuilder = new StringBuilder();
205159

src/GraphODataTemplateWriter/Extensions/OdcmModelExtensions.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,21 @@ public static OdcmProperty GetServiceCollectionNavigationPropertyForPropertyType
208208
}
209209
}
210210

211+
public static string GetImplicitPropertyName(this OdcmProperty property, OdcmSingleton singleton)
212+
{
213+
var implicitPropertyName = property.Name;
214+
// Default behavior
215+
if (singleton.NavigationPropertyBindings.Count() > 0)
216+
{
217+
var target = singleton.NavigationPropertyBindings.Where(kv => kv.Key.EndsWith(property.Name)).Select(kvp => kvp.Value).FirstOrDefault();
218+
if (target != null)
219+
{
220+
implicitPropertyName = target;
221+
}
222+
}
223+
return implicitPropertyName;
224+
}
225+
211226
public static IEnumerable<OdcmProperty> NavigationProperties(this OdcmClass odcmClass)
212227
{
213228
return odcmClass.Properties.Where(prop => prop.IsNavigation());

0 commit comments

Comments
 (0)