Skip to content

Commit 76b5c39

Browse files
author
Caitlin Bales (MSFT)
committed
Add support for reserved model words
This allows us to add reserved words that are used in our model classes (enums excluded). This fixes a bug where "Reserved" was previously defined by Newtonsoft
1 parent 53036e5 commit 76b5c39

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
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(null, true);
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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ namespace <#=entity.Namespace.GetNamespaceName()#>
7373
/// Gets or sets <#=property.Name.SplitCamelCase().ToLower()#>.
7474
/// </summary>
7575
<#=attributeDefinition#>
76-
public I<#=propertyCollectionPage#> <#=propertyName.GetSanitizedPropertyName()#> { get; set; }
76+
public I<#=propertyCollectionPage#> <#=propertyName.GetSanitizedPropertyName(null, true)#> { get; set; }
7777
<#
7878
}
7979
else
@@ -84,7 +84,7 @@ namespace <#=entity.Namespace.GetNamespaceName()#>
8484
/// Gets or sets <#=property.Name.SplitCamelCase().ToLower()#>.
8585
/// </summary>
8686
<#=attributeDefinition#>
87-
public IEnumerable<<#=propertyType#>> <#=propertyName.GetSanitizedPropertyName()#> { get; set; }
87+
public IEnumerable<<#=propertyType#>> <#=propertyName.GetSanitizedPropertyName(null, true)#> { get; set; }
8888
<#
8989
}
9090
}
@@ -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(null, true)#> { get; set; }
100100
<#
101101
}
102102
}

src/GraphODataTemplateWriter/CodeHelpers/CSharp/TypeHelperCSharp.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,14 @@ public static ICollection<string> GetReservedNames()
104104
};
105105
}
106106

107+
public static ICollection<string> GetReservedModelNames()
108+
{
109+
return new HashSet<string>(StringComparer.Ordinal)
110+
{
111+
"Required"
112+
};
113+
}
114+
107115
private static readonly ICollection<string> SimpleTypes =
108116
new HashSet<string> (StringComparer.OrdinalIgnoreCase)
109117
{
@@ -204,9 +212,9 @@ public static string GetSanitizedPropertyName(this OdcmProperty property)
204212
return GetSanitizedPropertyName(property.Name);
205213
}
206214

207-
public static string GetSanitizedPropertyName(this string property, string prefix = null)
215+
public static string GetSanitizedPropertyName(this string property, string prefix = null, bool hasReservedModelWords)
208216
{
209-
if (GetReservedNames().Contains(property))
217+
if (GetReservedNames().Contains(property) || (hasReservedModelWords && GetReservedModelNames().Contains(property)))
210218
{
211219
var reservedPrefix = string.IsNullOrEmpty(prefix) ? DefaultReservedPrefix : prefix;
212220

0 commit comments

Comments
 (0)