@@ -19,7 +19,7 @@ public IEnumerable<string> MethodArguments
1919 get
2020 {
2121 var methodArgs = CsharpMethod . Parts
22- . Select ( p => p . Name != "body" ? "p.RouteValues." + p . Name . ToPascalCase ( ) : "body" )
22+ . Select ( p => p . Name != "body" ? "p.RouteValues." + p . Name . ToPascalCase ( ) + ( p . Type == "enum" ? ".Value" : "" ) : "body" )
2323 . Concat ( new [ ] { "u => p.RequestParameters" } ) ;
2424 return methodArgs ;
2525 }
@@ -44,6 +44,8 @@ public string IfCheck
4444
4545 public class ApiEndpoint
4646 {
47+ private List < CsharpMethod > _csharpMethods ;
48+
4749 public string CsharpMethodName { get ; set ; }
4850 public string Documentation { get ; set ; }
4951 public IEnumerable < string > Methods { get ; set ; }
@@ -100,9 +102,20 @@ public IEnumerable<CsharpMethod> CsharpMethods
100102 {
101103 get
102104 {
103- foreach ( var method in this . Methods )
105+ if ( _csharpMethods != null )
104106 {
107+ foreach ( var csharpMethod in _csharpMethods )
108+ yield return csharpMethod ;
109+ yield break ;
110+ }
105111
112+ // enumerate once and cache
113+ _csharpMethods = new List < CsharpMethod > ( ) ;
114+
115+ this . PatchEndpoint ( ) ;
116+
117+ foreach ( var method in this . Methods )
118+ {
106119 var methodName = this . CsharpMethodName + this . OptionallyAppendHttpMethod ( this . Methods , method ) ;
107120 //the distinctby here catches aliases routes i.e
108121 // /_cluster/nodes/{node_id}/hotthreads vs /_cluster/nodes/{node_id}/hot_threads
@@ -117,25 +130,8 @@ public IEnumerable<CsharpMethod> CsharpMethods
117130 return p . Value ;
118131 } )
119132 . ToList ( ) ;
120- var args = parts . Select ( p =>
121- {
122- switch ( p . Type )
123- {
124- case "int" :
125- case "string" :
126- return p . Type + " " + p . Name ;
127- case "list" :
128- return "string " + p . Name ;
129- case "enum" :
130- return this . PascalCase ( p . Name ) + p . Name ;
131- case "number" :
132- return "string " + p . Name ;
133- default :
134- return p . Type + " " + p . Name ;
135- //return "string " + p.Name;
136133
137- }
138- } ) ;
134+ var args = parts . Select ( p => p . Argument ) ;
139135
140136 //.NET does not allow get requests to have a body payload.
141137 if ( method != "GET" && this . Body != null )
@@ -184,6 +180,7 @@ public IEnumerable<CsharpMethod> CsharpMethods
184180 "Func<" + apiMethod . QueryStringParamName + ", " + apiMethod . QueryStringParamName + "> requestParameters = null"
185181 } ) . ToList ( ) ;
186182 apiMethod . Arguments = string . Join ( ", " , args ) ;
183+ _csharpMethods . Add ( apiMethod ) ;
187184 yield return apiMethod ;
188185
189186 args = args . Concat ( new [ ]
@@ -208,6 +205,7 @@ public IEnumerable<CsharpMethod> CsharpMethods
208205 Url = this . Url
209206 } ;
210207 PatchMethod ( apiMethod ) ;
208+ _csharpMethods . Add ( apiMethod ) ;
211209 yield return apiMethod ;
212210
213211 //No need for deserialization state when returning dynamicdictionary
@@ -241,7 +239,9 @@ public IEnumerable<CsharpMethod> CsharpMethods
241239 Url = this . Url
242240 } ;
243241 PatchMethod ( apiMethod ) ;
242+ _csharpMethods . Add ( apiMethod ) ;
244243 yield return apiMethod ;
244+
245245 args = args . Concat ( new [ ]
246246 {
247247 "CancellationToken cancellationToken = default(CancellationToken)"
@@ -265,12 +265,38 @@ public IEnumerable<CsharpMethod> CsharpMethods
265265 Url = this . Url
266266 } ;
267267 PatchMethod ( apiMethod ) ;
268+ _csharpMethods . Add ( apiMethod ) ;
268269 yield return apiMethod ;
269270 }
270271 }
271272 }
272273 }
273274
275+ private void PatchEndpoint ( )
276+ {
277+ //rename the {metric} route param to something more specific on XpackWatcherStats
278+ // TODO: find a better place to do this
279+ if ( this . CsharpMethodName == "XpackWatcherStats" )
280+ {
281+ var metric = this . Url . Parts . First ( p => p . Key == "metric" ) ;
282+
283+ var apiUrlPart = metric . Value ;
284+ apiUrlPart . Name = "watcher_stats_metric" ;
285+
286+ if ( this . Url . Parts . Remove ( "metric" ) )
287+ {
288+ this . Url . Parts . Add ( "watcher_stats_metric" , apiUrlPart ) ;
289+ }
290+
291+ this . Url . Path = RenameMetricUrlPathParam ( this . Url . Path ) ;
292+ this . Url . Paths = this . Url . Paths . Select ( RenameMetricUrlPathParam ) ;
293+ }
294+ }
295+
296+ private static string RenameMetricUrlPathParam ( string path )
297+ {
298+ return path . Replace ( "{metric}" , "{watcher_stats_metric}" ) ;
299+ }
274300
275301 //Patches a method name for the exceptions (IndicesStats needs better unique names for all the url endpoints)
276302 //or to get rid of double verbs in an method name i,e ClusterGetSettingsGet > ClusterGetSettings
@@ -318,7 +344,7 @@ public static void PatchMethod(CsharpMethod method)
318344 try
319345 {
320346 IEnumerable < string > skipList = new List < string > ( ) ;
321- IDictionary < string , string > renameList = new Dictionary < string , string > ( ) ;
347+ IDictionary < string , string > queryStringParamsRenameList = new Dictionary < string , string > ( ) ;
322348
323349 var typeName = "ApiGenerator.Overrides.Descriptors." + method . DescriptorType + "Overrides" ;
324350 var type = CodeConfiguration . Assembly . GetType ( typeName ) ;
@@ -328,9 +354,8 @@ public static void PatchMethod(CsharpMethod method)
328354 if ( overrides != null )
329355 {
330356 skipList = overrides . SkipQueryStringParams ?? skipList ;
331- renameList = overrides . RenameQueryStringParams ?? renameList ;
332-
333- overrides . PatchMethod ( method ) ;
357+ queryStringParamsRenameList = overrides . RenameQueryStringParams ?? queryStringParamsRenameList ;
358+ method = overrides . PatchMethod ( method ) ;
334359 }
335360 }
336361
@@ -343,8 +368,8 @@ public static void PatchMethod(CsharpMethod method)
343368 } ;
344369
345370 foreach ( var kv in globalQueryStringRenames )
346- if ( ! renameList . ContainsKey ( kv . Key ) )
347- renameList [ kv . Key ] = kv . Value ;
371+ if ( ! queryStringParamsRenameList . ContainsKey ( kv . Key ) )
372+ queryStringParamsRenameList [ kv . Key ] = kv . Value ;
348373
349374 var patchedParams = new Dictionary < string , ApiQueryParameters > ( ) ;
350375 foreach ( var kv in method . Url . Params )
@@ -355,7 +380,7 @@ public static void PatchMethod(CsharpMethod method)
355380 continue ;
356381
357382 string newName ;
358- if ( ! renameList . TryGetValue ( kv . Key , out newName ) )
383+ if ( ! queryStringParamsRenameList . TryGetValue ( kv . Key , out newName ) )
359384 {
360385 patchedParams . Add ( kv . Key , kv . Value ) ;
361386 continue ;
0 commit comments