Skip to content

Commit 42572c0

Browse files
committed
- fixes a bug where action/methods for have a reserved name in java
1 parent 3a27116 commit 42572c0

3 files changed

Lines changed: 10 additions & 16 deletions

File tree

Templates/Android/BaseModel.template.tt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
}
4444

4545
public string MethodName(OdcmObject c) {
46-
return c.Name.Substring(c.Name.IndexOf(".") + 1).ToUpperFirstChar();
46+
return c.Name.Substring(c.Name.IndexOf(".") + 1).SanitizePropertyName(property).ToUpperFirstChar();
4747
}
4848

4949
public string MethodFullName(OdcmObject c) {

Templates/Java/BaseJavaModel.template.tt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
}
4444

4545
public string MethodName(OdcmObject c) {
46-
return c.Name.Substring(c.Name.IndexOf(".") + 1).ToUpperFirstChar();
46+
return c.Name.Substring(c.Name.IndexOf(".") + 1).SanitizePropertyName(c).ToUpperFirstChar();
4747
}
4848

4949
public string MethodFullName(OdcmObject c) {

src/GraphODataTemplateWriter/CodeHelpers/Java/TypeHelperJava.cs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,11 @@ namespace Microsoft.Graph.ODataTemplateWriter.CodeHelpers.Java
1010

1111
public static class TypeHelperJava
1212
{
13-
private static Logger logger = LogManager.GetCurrentClassLogger();
13+
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
1414
public const string ReservedPrefix = "msgraph";
15-
public static HashSet<string> ReservedNames
16-
{
17-
get
18-
{
19-
return new HashSet<string>(StringComparer.OrdinalIgnoreCase) {
15+
public static Lazy<HashSet<string>> ReservedNames { get; private set; } = new Lazy<HashSet<string>>(() => new HashSet<string>(StringComparer.OrdinalIgnoreCase) {
2016
"abstract", "continue", "for", "new", "switch", "assert", "default", "if", "package", "synchronized", "boolean", "do", "goto", "private", "this", "break", "double", "implements", "protected", "throw", "byte", "else", "import", "public", "throws", "case", "enum", "instanceof", "return", "transient", "catch", "extends", "int", "short", "try", "char", "final", "interface", "static", "void", "class", "finally", "long", "strictfp", "volatile", "const", "float", "native", "super", "while", "true", "false", "null", "import"
21-
};
22-
}
23-
}
17+
});
2418

2519
public static string GetReservedPrefix(this OdcmType @type)
2620
{
@@ -73,7 +67,7 @@ public static string GetTypeString(this OdcmProperty property)
7367
{
7468
var propertyType = property.Projection.Type;
7569
var typeString = GetTypeString(propertyType);
76-
if (propertyType.Namespace != OdcmNamespace.Edm && ReservedNames.Contains(typeString))
70+
if (propertyType.Namespace != OdcmNamespace.Edm && ReservedNames.Value.Contains(typeString))
7771
{
7872
typeString = "com.microsoft.graph.models.extensions." + typeString;
7973
}
@@ -95,24 +89,24 @@ public static string GetToLowerFirstCharName(this OdcmProperty property)
9589

9690
public static string SanitizePropertyName(this string property, OdcmObject odcmProperty = null)
9791
{
98-
if (ReservedNames.Contains(property))
92+
if (ReservedNames.Value.Contains(property))
9993
{
10094
logger.Info("Property \"{0}\" is a reserved word in Java. Converting to \"{1}{0}\"", property, ReservedPrefix);
101-
return ReservedPrefix + property;
95+
return ReservedPrefix + property.ToUpperFirstChar();
10296
}
10397

10498
if (odcmProperty != null && property == odcmProperty.Name.ToUpperFirstChar())
10599
{
106100
// Check whether the property type is the same as the class name.
107-
if (odcmProperty.Projection.Type.Name.ToUpperFirstChar() == odcmProperty.Name.ToUpperFirstChar())
101+
if (odcmProperty.Projection.Type?.Name?.ToUpperFirstChar() == odcmProperty.Name.ToUpperFirstChar())
108102
{
109103
// Name the property: {metadataName} + "Property"
110104
logger.Info("Property type \"{0}\" has the same name as the class. Converting to \"{0}Property\"", property);
111105
return string.Concat(property, "Property");
112106
}
113107

114108
// Name the property by its type. Sanitize it in case the type is a reserved name.
115-
return odcmProperty.Projection.Type.Name.ToUpperFirstChar().SanitizePropertyName(odcmProperty);
109+
return odcmProperty.Projection.Type?.Name?.ToUpperFirstChar()?.SanitizePropertyName(odcmProperty) ?? property.SanitizePropertyName();
116110
}
117111

118112
return property.Replace("@", string.Empty).Replace(".", string.Empty);

0 commit comments

Comments
 (0)