Skip to content

Commit 12ca27f

Browse files
author
Caitlin Bales (MSFT)
committed
Add catch around navigation property containment error
1 parent 3e9722e commit 12ca27f

File tree

4 files changed

+24
-11
lines changed

4 files changed

+24
-11
lines changed

Templates/Android/generated/BaseEntityCollectionReferenceRequest.java.tt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222

2323
public void post(final <#=TypeName(c)#> new<#=TypeName(c)#>, final ICallback<<#=TypeName(c)#>> callback) {
2424
<#
25-
String prop = c.AsOdcmProperty().GetServiceCollectionNavigationPropertyForPropertyType().Name;
25+
var navigationProperty = c.AsOdcmProperty().GetServiceCollectionNavigationPropertyForPropertyType();
26+
if (navigationProperty != null) {
27+
String prop = c.AsOdcmProperty().GetServiceCollectionNavigationPropertyForPropertyType().Name;
2628
#>
2729
final String requestUrl = getBaseRequest().getRequestUrl().toString();
2830
final ReferenceRequestBody body = new ReferenceRequestBody(getBaseRequest().getClient().getServiceRoot() + "/<#=prop#>/" + new<#=TypeName(c)#>.id);
@@ -38,7 +40,8 @@
3840
.buildRequest(getBaseRequest().getOptions())
3941
.post(new<#=TypeName(c)#>, body);
4042
}
41-
43+
<# }
44+
#>
4245
<# if (c.GetFeatures().CanExpand) { #>
4346
/**
4447
* Sets the expand clause for the request

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ public string GetPostAsyncMethodForReferencesRequest(OdcmProperty odcmProperty)
135135
var templateWriter = (CodeWriterCSharp)templateWriterHost.CodeWriter;
136136

137137
var serviceNavigationProperty = odcmProperty.GetServiceCollectionNavigationPropertyForPropertyType();
138+
if (serviceNavigationProperty == null)
139+
return string.Empty;
138140

139141
var stringBuilder = new StringBuilder();
140142

Templates/ObjC/Requests/EntityCollectionRequest.m.tt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ if (innerEntityType.Equals(propName))
8989
- (NSMutableURLRequest *)add<#=innerEntity.Name.ToUpperFirstChar()#>:(<#=innerEntityType#>*)<#=innerEntity.Name.ToLowerFirstChar()#>
9090
{
9191
<#
92-
if(collectionReference)
92+
if(collectionReference && prop.GetServiceCollectionNavigationPropertyForPropertyType() != null)
9393
{
9494
#>
9595

src/GraphODataTemplateWriter/Extensions/OdcmModelExtensions.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,22 @@ public static OdcmProperty GetServiceCollectionNavigationPropertyForPropertyType
140140
// class object. Use First() instead of FirstOrDefault() so template generation would fail if not found
141141
// instead of silently continuing. If an entity is used in a reference property a navigation collection
142142
// on the client for that type is required.
143-
return odcmProperty
144-
.Class
145-
.Namespace
146-
.Classes
147-
.Where(odcmClass => odcmClass.Kind == OdcmClassKind.Service)
148-
.SelectMany(service => (service as OdcmServiceClass).NavigationProperties())
149-
.Where(property => property.IsCollection && property.Projection.Type.FullName.Equals(odcmProperty.Projection.Type.FullName))
150-
.First();
143+
try
144+
{
145+
return odcmProperty
146+
.Class
147+
.Namespace
148+
.Classes
149+
.Where(odcmClass => odcmClass.Kind == OdcmClassKind.Service)
150+
.SelectMany(service => (service as OdcmServiceClass).NavigationProperties())
151+
.Where(property => property.IsCollection && property.Projection.Type.FullName.Equals(odcmProperty.Projection.Type.FullName))
152+
.First();
153+
} catch (Exception e)
154+
{
155+
//TODO: Pass the exception to the logger
156+
Console.WriteLine("Error: The navigation property \"{0}\" on class \"{1}\" does not specify it is self-contained nor is it defined in an EntitySet", odcmProperty.Name.ToString(), odcmProperty.Class.FullName.ToString());
157+
return null;
158+
}
151159
}
152160

153161
public static IEnumerable<OdcmProperty> NavigationProperties(this OdcmClass odcmClass)

0 commit comments

Comments
 (0)