From 62156a2fb32180f526988cb07308d21525768928 Mon Sep 17 00:00:00 2001 From: Harjaap Singh Gill Date: Tue, 29 Jan 2019 22:24:35 -0800 Subject: [PATCH 1/4] Update GetSanitizedPropertyName method Now allows GetSanitizedPropertyName to accept an additional parameter, suffix, so that an additional string may be concatenated onto the end of the Property. Updated method logic to look for and concatenate suffix when specified. --- .../CodeHelpers/CSharp/TypeHelperCSharp.cs | 35 ++++++++++--------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/src/GraphODataTemplateWriter/CodeHelpers/CSharp/TypeHelperCSharp.cs b/src/GraphODataTemplateWriter/CodeHelpers/CSharp/TypeHelperCSharp.cs index 318a2a79c..24ec70297 100644 --- a/src/GraphODataTemplateWriter/CodeHelpers/CSharp/TypeHelperCSharp.cs +++ b/src/GraphODataTemplateWriter/CodeHelpers/CSharp/TypeHelperCSharp.cs @@ -222,9 +222,9 @@ public static string GetSanitizedLongDescription(this OdcmProperty property) return null; } - public static string GetSanitizedPropertyName(this OdcmProperty property) + public static string GetSanitizedPropertyName(this OdcmProperty property, string prefix = null, string suffix = null) { - return GetSanitizedPropertyName(property.Name); + return GetSanitizedPropertyName(property.Name, prefix, suffix); } public static string GetSanitizedClassName(this OdcmClass odcmClass) @@ -232,9 +232,9 @@ public static string GetSanitizedClassName(this OdcmClass odcmClass) return GetSanitizedClassName(odcmClass.Name, odcmClass); } - public static string GetSanitizedPropertyName(this string property, string prefix = null) + public static string GetSanitizedPropertyName(this string property, string prefix = null, string suffix = null) { - return GetSanitizedPropertyName(property, null, prefix); + return GetSanitizedPropertyName(property, null, prefix, suffix); } /// @@ -248,34 +248,37 @@ public static string GetSanitizedPropertyName(this string property, string prefi /// An OdcmProperty. Use the property that you want to sanitize. /// The prefix to use on this property. /// - public static string GetSanitizedPropertyName(this string property, OdcmProperty odcmProperty, string prefix = null) + public static string GetSanitizedPropertyName(this string property, OdcmProperty odcmProperty, string prefix = null, string suffix = null) { + string result = property; if (GetReservedNames().Contains(property)) { var reservedPrefix = string.IsNullOrEmpty(prefix) ? DefaultReservedPrefix : prefix; logger.Info("Property \"{0}\" is a reserved word in .NET. Converting to \"{1}{0}\"", property.ToUpperFirstChar(), reservedPrefix); - return string.Concat(reservedPrefix, property.ToUpperFirstChar()); + result = string.Concat(reservedPrefix, property.ToUpperFirstChar()); } - - // Check whether the propertyObject is null (means they called the extension from a string). - // Check whether the property name is the same as the class name. - // Only constructor members may be named the same as the class name. - if (odcmProperty != null && property == odcmProperty.Class.Name.ToUpperFirstChar()) + else if (odcmProperty != null && property == odcmProperty.Class.Name.ToUpperFirstChar()) { + // Check whether the propertyObject is null (means they called the extension from a string). + // Check whether the property name is the same as the class name. + // Only constructor members may be named the same as the class name. + // Check whether the property type is the same as the class name. if (odcmProperty.Projection.Type.Name.ToUpperFirstChar() == odcmProperty.Class.Name.ToUpperFirstChar()) { // Name the property: {metadataName} + "Property" logger.Info("Property type \"{0}\" has the same name as the class. Converting to \"{0}Property\"", property); - return string.Concat(property, "Property"); + result = string.Concat(property, "Property"); + } + else + { + // Name the property by its type. Sanitize it in case the type is a reserved name. + result = odcmProperty.Projection.Type.Name.ToUpperFirstChar().GetSanitizedPropertyName(); } - - // Name the property by its type. Sanitize it in case the type is a reserved name. - return odcmProperty.Projection.Type.Name.ToUpperFirstChar().GetSanitizedPropertyName(); } - return property; + return result+suffix; } public static string GetSanitizedClassName(this string className, OdcmClass odcmClass) From 02288c4d20f6ea26e340356b2e747902666984f1 Mon Sep 17 00:00:00 2001 From: Harjaap Singh Gill Date: Tue, 29 Jan 2019 22:26:55 -0800 Subject: [PATCH 2/4] Updates IEntityRequestBuilder.Base.template.tt Updates IEntityRequestBuilder.Base.template.tt so that it does not create properties with the same name as the methods within the class (specifically request), by concatenating a "_" to the end of the property name. --- .../CSharp/Base/IEntityRequestBuilder.Base.template.tt | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Templates/CSharp/Base/IEntityRequestBuilder.Base.template.tt b/Templates/CSharp/Base/IEntityRequestBuilder.Base.template.tt index f459c67b0..d199c9127 100644 --- a/Templates/CSharp/Base/IEntityRequestBuilder.Base.template.tt +++ b/Templates/CSharp/Base/IEntityRequestBuilder.Base.template.tt @@ -98,8 +98,9 @@ public string GetRequestBuilderProperty(string propertyName, string requestBuild } public string GetCollectionPropertyRequestBuilderProperty(string entityName, OdcmProperty odcmProperty) -{ - var propertyName = this.GetPropertyName(odcmProperty).GetSanitizedPropertyName(); +{ + var propertyName = this.GetPropertyName(odcmProperty); + propertyName = propertyName.GetSanitizedPropertyName(suffix: propertyName == "Request" ? "_" : null); var propertyPrefix = string.Concat(entityName, this.GetPropertyName(odcmProperty)); var collectionRequestBuilder = odcmProperty.IsReference() ? this.GetCollectionWithReferencesRequestBuilderString(propertyPrefix) @@ -110,7 +111,8 @@ public string GetCollectionPropertyRequestBuilderProperty(string entityName, Odc public string GetNonCollectionRequestBuilderProperty(OdcmProperty odcmProperty) { - var propertyName = this.GetPropertyName(odcmProperty).GetSanitizedPropertyName(); + var propertyName = this.GetPropertyName(odcmProperty); + propertyName = propertyName.GetSanitizedPropertyName(suffix: propertyName == "Request" ? "_" : null); var propertyPrefix = this.GetPropertyTypeName(odcmProperty); var propertyRequestBuilder = odcmProperty.IsReference() ? this.GetWithReferenceRequestBuilderString(propertyPrefix) From 455f177cacd8ca167029ff4f5fd759bae3eef130 Mon Sep 17 00:00:00 2001 From: Harjaap Singh Gill Date: Tue, 29 Jan 2019 22:27:55 -0800 Subject: [PATCH 3/4] Updates EntityRequestBuilder.Base.template.tt Updates EntityRequestBuilder.Base.template.tt so that it does not create properties with the same name as the methods within the class (specifically request), by concatenating a "_" to the end of the property name. --- Templates/CSharp/Base/EntityRequestBuilder.Base.template.tt | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Templates/CSharp/Base/EntityRequestBuilder.Base.template.tt b/Templates/CSharp/Base/EntityRequestBuilder.Base.template.tt index 2c94914d3..78a742c0e 100644 --- a/Templates/CSharp/Base/EntityRequestBuilder.Base.template.tt +++ b/Templates/CSharp/Base/EntityRequestBuilder.Base.template.tt @@ -142,7 +142,8 @@ public string GetRequestBuilderProperty(string propertyName, string urlValue, st public string GetCollectionPropertyRequestBuilderProperty(string entityName, OdcmProperty odcmProperty) { - var propertyName = this.GetPropertyName(odcmProperty).GetSanitizedPropertyName(); + var propertyName = this.GetPropertyName(odcmProperty); + propertyName = propertyName.GetSanitizedPropertyName(suffix: propertyName == "Request" ? "_" : null); var propertyPrefix = string.Concat(entityName, this.GetPropertyName(odcmProperty)); var collectionRequestBuilder = odcmProperty.IsReference() ? this.GetCollectionWithReferencesRequestBuilderString(propertyPrefix) @@ -153,7 +154,8 @@ public string GetCollectionPropertyRequestBuilderProperty(string entityName, Odc public string GetNonCollectionRequestBuilderProperty(OdcmProperty odcmProperty) { - var propertyName = this.GetPropertyName(odcmProperty).GetSanitizedPropertyName(); + var propertyName = this.GetPropertyName(odcmProperty); + propertyName = propertyName.GetSanitizedPropertyName(suffix: propertyName == "Request" ? "_" : null); var propertyPrefix = this.GetPropertyTypeName(odcmProperty); var propertyRequestBuilder = odcmProperty.IsReference() ? this.GetWithReferenceRequestBuilderString(propertyPrefix) From 85b03e08c9887c88568ce92ecf6c9dede1e68850 Mon Sep 17 00:00:00 2001 From: Harjaap Singh Gill Date: Fri, 1 Feb 2019 09:04:19 -0800 Subject: [PATCH 4/4] Update Tests to have relevant references Deletes invalid reference paths to avoid namespace errors --- .../GraphODataTemplateWriter.Test.csproj | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/GraphODataTemplateWriter.Test/GraphODataTemplateWriter.Test.csproj b/GraphODataTemplateWriter.Test/GraphODataTemplateWriter.Test.csproj index b50f71ea9..3520d7576 100644 --- a/GraphODataTemplateWriter.Test/GraphODataTemplateWriter.Test.csproj +++ b/GraphODataTemplateWriter.Test/GraphODataTemplateWriter.Test.csproj @@ -40,11 +40,9 @@ ..\submodules\vipr\src\Core\Vipr.Core\bin\Debug\Vipr.Core.dll - ..\submodules\vipr\src\Core\Vipr.Core\bin\Release\Vipr.Core.dll ..\submodules\vipr\src\Readers\Vipr.Reader.OData.v4\bin\Debug\Vipr.Reader.OData.v4.dll - ..\submodules\vipr\src\Readers\Vipr.Reader.OData.v4\bin\Release\Vipr.Reader.OData.v4.dll @@ -73,4 +71,4 @@ - \ No newline at end of file +