Skip to content

Commit c7a70ec

Browse files
mmainermmainer
authored andcommitted
Updated GetSanitizedPropertyName to sanitize property names where the property name is the same as the class.
1 parent 53036e5 commit c7a70ec

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

Templates/CSharp/Model/ComplexType.cs.tt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ namespace <#=complex.Namespace.GetNamespaceName()#>
4949
var propertyType = property.IsCollection ? string.Format("IEnumerable<{0}>", property.GetTypeString()) : property.GetTypeString();
5050
var propertyName = isMethodResponse
5151
? property.Name.Substring(property.Name.IndexOf('.') + 1).ToCheckedCase()
52-
: property.Name.ToCheckedCase().GetSanitizedPropertyName();
52+
: property.Name.ToCheckedCase().GetSanitizedPropertyName(property);
5353

5454
var attributeDefinition = string.Format("[JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = \"{0}\", Required = Required.Default)]", property.Name);
5555

Templates/CSharp/Model/EntityType.cs.tt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ namespace <#=entity.Namespace.GetNamespaceName()#>
9696
/// Gets or sets <#=property.Name.SplitCamelCase().ToLower()#>.
9797
/// </summary>
9898
<#=attributeDefinition#>
99-
public <#=propertyType#> <#=propertyName.GetSanitizedPropertyName()#> { get; set; }
99+
public <#=propertyType#> <#=propertyName.GetSanitizedPropertyName(property)#> { get; set; }
100100
<#
101101
}
102102
}

src/GraphODataTemplateWriter/CodeHelpers/CSharp/TypeHelperCSharp.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,22 @@ public static string GetSanitizedPropertyName(this OdcmProperty property)
205205
}
206206

207207
public static string GetSanitizedPropertyName(this string property, string prefix = null)
208+
{
209+
return GetSanitizedPropertyName(property, null, prefix);
210+
}
211+
212+
/// <summary>
213+
/// Sanitizes a property name for the following conditions:
214+
/// 1) a property has the same name as a C# keyword. Prefix @ to the property name to make it valid.
215+
/// 2) a property has the same name as the class. First we'll try to change the property name to the
216+
/// return type name. If the return type name is the same as the class name, then we'll append
217+
/// "Property" to the property name.
218+
/// </summary>
219+
/// <param name="property">The string that called this extension.</param>
220+
/// <param name="odcmProperty">An OdcmProperty. Use the property that you want to sanitize.</param>
221+
/// <param name="prefix">The prefix to use on this property.</param>
222+
/// <returns></returns>
223+
public static string GetSanitizedPropertyName(this string property, OdcmProperty odcmProperty, string prefix = null)
208224
{
209225
if (GetReservedNames().Contains(property))
210226
{
@@ -213,6 +229,22 @@ public static string GetSanitizedPropertyName(this string property, string prefi
213229
return string.Concat(reservedPrefix, property.ToUpperFirstChar());
214230
}
215231

232+
// Check whether the propertyObject is null (means they called the extension from a string).
233+
// Check whether the property name is the same as the class name.
234+
// Only constructor members may be named the same as the class name.
235+
if (odcmProperty != null && property == odcmProperty.Class.Name.ToUpperFirstChar())
236+
{
237+
// Check whether the property type is the same as the class name.
238+
if (odcmProperty.Projection.Type.Name.ToUpperFirstChar() == odcmProperty.Class.Name.ToUpperFirstChar())
239+
{
240+
// Name the property: {metadataName} + "Property"
241+
return string.Concat(property, "Property");
242+
}
243+
244+
// Name the property by its type. Sanitize it in case the type is a reserved name.
245+
return odcmProperty.Projection.Type.Name.ToUpperFirstChar().GetSanitizedPropertyName();
246+
}
247+
216248
return property;
217249
}
218250

0 commit comments

Comments
 (0)