Skip to content

Commit 26f78c2

Browse files
authored
Add mechanism to escape C# keywords for path parts (#5563)
1 parent 358b766 commit 26f78c2

File tree

4 files changed

+15
-17
lines changed

4 files changed

+15
-17
lines changed

src/ApiGenerator/Domain/Code/LowLevel/LowLevelClientMethod.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@ public string UrlInCode
3333
{
3434
string Evaluator(Match m)
3535
{
36-
3736
var arg = m.Groups[^1].Value.ToCamelCase();
38-
return $"{{{arg}:{arg}}}";
37+
return $"{{{arg.ReservedKeywordReplacer()}:{arg}}}";
3938
}
4039

4140
var url = Path.TrimStart('/');

src/ApiGenerator/Domain/Specification/UrlPart.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,17 +139,14 @@ public string InterfaceName
139139
}
140140

141141
public string Name { get; set; }
142-
public string NameAsArgument => Name.ToCamelCase();
142+
public string NameAsArgument => Name.ToCamelCase().ReservedKeywordReplacer();
143143
public IEnumerable<string> Options { get; set; }
144144
public bool Required { get; set; }
145145
public bool Deprecated { get; set; }
146146
public string Type { get; set; }
147147

148-
private string CleanUpDescription(string value)
149-
{
150-
if (string.IsNullOrWhiteSpace(value)) return value;
151148

152-
return value.Replace("use `_all` or empty string", "use the special string `_all` or Indices.All");
153-
}
149+
150+
private static string CleanUpDescription(string value) => string.IsNullOrWhiteSpace(value) ? value : value.Replace("use `_all` or empty string", "use the special string `_all` or Indices.All");
154151
}
155152
}

src/ApiGenerator/Domain/Specification/UrlPath.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,25 +55,24 @@ orderby path.IndexOf($"{{{name}}}", StringComparison.Ordinal)
5555

5656
public string TypedSubClassBaseArguments => string.Join(", ", Parts.Select(p => p.NameAsArgument));
5757

58-
private static string[] ResolvabeFromT = { "index"};
58+
private static readonly string[] ResolvableFromT = { "index"};
5959

60+
public bool HasResolvableArguments => Parts.Any(p => ResolvableFromT.Contains(p.Name));
61+
public string AutoResolveConstructorArguments => string.Join(", ", Parts.Where(p => !ResolvableFromT.Contains(p.Name)).Select(p => $"{p.HighLevelTypeName} {p.NameAsArgument}"));
6062

61-
public bool HasResolvableArguments => Parts.Any(p => ResolvabeFromT.Contains(p.Name));
62-
public string AutoResolveConstructorArguments => string.Join(", ", Parts.Where(p => !ResolvabeFromT.Contains(p.Name)).Select(p => $"{p.HighLevelTypeName} {p.NameAsArgument}"));
63-
64-
public string AutoResolveBaseArguments(string generic) => string.Join(", ", Parts.Select(p => !ResolvabeFromT.Contains(p.Name) ? p.Name : $"typeof({generic})"));
63+
public string AutoResolveBaseArguments(string generic) => string.Join(", ", Parts.Select(p => !ResolvableFromT.Contains(p.Name) ? p.Name : $"typeof({generic})"));
6564

6665
public string DocumentPathBaseArgument(string generic) => string.Join(", ",
6766
_additionalPartsForConstructor.Select(p => p.Name =="id" ? $"id ?? Nest.Id.From(documentWithId)"
68-
: ResolvabeFromT.Contains(p.Name) ? $"{p.Name} ?? typeof({generic})" : p.Name));
67+
: ResolvableFromT.Contains(p.Name) ? $"{p.Name} ?? typeof({generic})" : p.Name));
6968

7069
public string DocumentPathConstructorArgument(string generic) => string.Join(", ",
7170
new [] { $"{generic} documentWithId" }.Concat(_additionalPartsForConstructor.Select(p => $"{p.HighLevelTypeName} {p.NameAsArgument} = null")));
7271

7372
public string GetXmlDocs(string indent, bool skipResolvable = false, bool documentConstructor = false)
7473
{
7574
var doc = $@"///<summary>{Path}</summary>";
76-
var parts = Parts.Where(p => !skipResolvable || !ResolvabeFromT.Contains(p.Name)).ToList();
75+
var parts = Parts.Where(p => !skipResolvable || !ResolvableFromT.Contains(p.Name)).ToList();
7776
if (!parts.Any()) return doc;
7877

7978
doc += indent;

src/ApiGenerator/Extensions.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ public static string ToCamelCase(this string s)
4343
return pascal[0].ToLower() + pascal.Substring(1);
4444
}
4545

46-
public static string SplitPascalCase(this string s) =>
47-
Regex.Replace(s, "([A-Z]+[a-z]*)", " $1").Trim();
46+
private static readonly Dictionary<string, string> ReservedNames = new() { { "namespace", "@namespace" } };
47+
48+
public static string ReservedKeywordReplacer(this string name) => ReservedNames.ContainsKey(name) ? ReservedNames[name] : name;
49+
50+
public static string SplitPascalCase(this string s) => Regex.Replace(s, "([A-Z]+[a-z]*)", " $1").Trim();
4851
}
4952
}

0 commit comments

Comments
 (0)