@@ -76,24 +76,24 @@ public IEnumerable<Constructor> RequestConstructors()
7676 var m = this . RequestType ;
7777 foreach ( var url in this . Url . Paths )
7878 {
79- var cp = this . Url . Parts
79+ var urlRouteParameters = this . Url . Parts
8080 . Where ( p => ! ApiUrl . BlackListRouteValues . Contains ( p . Key ) )
8181 . Where ( p => url . Contains ( $ "{{{p.Value.Name}}}") )
8282 . OrderBy ( kv => url . IndexOf ( $ "{{{kv.Value.Name}}}", StringComparison . Ordinal ) ) ;
83- var par = string . Join ( ", " , cp . Select ( p => $ "{ ClrParamType ( p . Value . ClrTypeName ) } { p . Key } ") ) ;
83+ var par = string . Join ( ", " , urlRouteParameters . Select ( p => $ "{ ClrParamType ( p . Value . ClrTypeName ) } { p . Key } ") ) ;
8484 var routing = string . Empty ;
8585
8686 //Routes that take {indices}/{types} and both are optional
8787 //we rather not generate a parameterless constructor and force folks to call Indices.All
88- if ( ! cp . Any ( ) && IndicesAndTypes )
88+ if ( ! urlRouteParameters . Any ( ) && IndicesAndTypes )
8989 {
9090 ParameterlessIndicesTypesConstructor ( ctors , m ) ;
9191 continue ;
9292 }
9393
94- if ( cp . Any ( ) )
94+ if ( urlRouteParameters . Any ( ) )
9595 {
96- routing = "r=>r." + string . Join ( "." , cp
96+ routing = "r=>r." + string . Join ( "." , urlRouteParameters
9797 . Select ( p => new
9898 {
9999 route = p . Key ,
@@ -109,17 +109,24 @@ public IEnumerable<Constructor> RequestConstructors()
109109 }
110110
111111 var doc = $@ "/// <summary>{ url } </summary>";
112- if ( cp . Any ( ) )
112+ if ( urlRouteParameters . Any ( ) )
113113 {
114- doc += "\r \n " + string . Join ( "\t \t \r \n " , cp . Select ( p => $ "///<param name=\" { p . Key } \" >{ ( p . Value . Required ? "this parameter is required" : "Optional, accepts null" ) } </param>") ) ;
114+ doc += "\r \n " + string . Join ( "\t \t \r \n " , urlRouteParameters . Select ( p => $ "///<param name=\" { p . Key } \" >{ ( p . Value . Required ? "this parameter is required" : "Optional, accepts null" ) } </param>") ) ;
115115 }
116116 var generated = $ "public { m } ({ par } ) : base({ routing } ){{}}";
117117
118118 // special case SearchRequest<T> to pass the type of T as the type, when only the index is specified.
119- if ( m == "SearchRequest" && cp . Count ( ) == 1 && ! string . IsNullOrEmpty ( this . RequestTypeGeneric ) )
119+ if ( m == "SearchRequest" && urlRouteParameters . Count ( ) == 1 && ! string . IsNullOrEmpty ( this . RequestTypeGeneric ) )
120120 {
121121 var generic = this . RequestTypeGeneric . Replace ( "<" , "" ) . Replace ( ">" , "" ) ;
122- generated = $ "public { m } ({ par } ) : this({ cp . First ( ) . Key } , typeof({ generic } )){{}}";
122+ generated = $ "public { m } ({ par } ) : this({ urlRouteParameters . First ( ) . Key } , typeof({ generic } )){{}}";
123+ }
124+
125+ if ( ( m == "SuggestRequest" ) && string . IsNullOrEmpty ( par ) && ! string . IsNullOrEmpty ( this . RequestTypeGeneric ) )
126+ {
127+ var generic = this . RequestTypeGeneric . Replace ( "<" , "" ) . Replace ( ">" , "" ) ;
128+ doc = AppendToSummary ( doc , ". Will infer the index from the generic type" ) ;
129+ generated = $ "public { m } ({ par } ) : this(typeof({ generic } )){{}}";
123130 }
124131
125132 var c = new Constructor { Generated = generated , Description = doc } ;
@@ -159,21 +166,21 @@ public IEnumerable<Constructor> DescriptorConstructors()
159166 var m = this . DescriptorType ;
160167 foreach ( var url in this . Url . Paths )
161168 {
162- var cp = this . Url . Parts
169+ var requiredUrlRouteParameters = this . Url . Parts
163170 . Where ( p => ! ApiUrl . BlackListRouteValues . Contains ( p . Key ) )
164171 . Where ( p => p . Value . Required )
165172 . Where ( p => url . Contains ( $ "{{{p.Value.Name}}}") )
166173 . OrderBy ( kv => url . IndexOf ( $ "{{{kv.Value.Name}}}", StringComparison . Ordinal ) ) ;
167- var par = string . Join ( ", " , cp . Select ( p => $ "{ ClrParamType ( p . Value . ClrTypeName ) } { p . Key } ") ) ;
174+ var par = string . Join ( ", " , requiredUrlRouteParameters . Select ( p => $ "{ ClrParamType ( p . Value . ClrTypeName ) } { p . Key } ") ) ;
168175 var routing = string . Empty ;
169176 //Routes that take {indices}/{types} and both are optional
170- if ( ! cp . Any ( ) && IndicesAndTypes )
177+ if ( ! requiredUrlRouteParameters . Any ( ) && IndicesAndTypes )
171178 {
172179 AddParameterlessIndicesTypesConstructor ( ctors , m ) ;
173180 continue ;
174181 }
175- if ( cp . Any ( ) )
176- routing = "r=>r." + string . Join ( "." , cp
182+ if ( requiredUrlRouteParameters . Any ( ) )
183+ routing = "r=>r." + string . Join ( "." , requiredUrlRouteParameters
177184 . Select ( p => new
178185 {
179186 route = p . Key ,
@@ -187,20 +194,28 @@ public IEnumerable<Constructor> DescriptorConstructors()
187194 . Select ( p => $ "{ p . call } (\" { p . route } \" , { p . v } )")
188195 ) ;
189196 var doc = $@ "/// <summary>{ url } </summary>";
190- if ( cp . Any ( ) )
197+ if ( requiredUrlRouteParameters . Any ( ) )
191198 {
192- doc += "\r \n " + string . Join ( "\t \t \r \n " , cp . Select ( p => $ "///<param name=\" { p . Key } \" > this parameter is required</param>") ) ;
199+ doc += "\r \n " + string . Join ( "\t \t \r \n " , requiredUrlRouteParameters . Select ( p => $ "///<param name=\" { p . Key } \" > this parameter is required</param>") ) ;
193200 }
194201
195202 var generated = $ "public { m } ({ par } ) : base({ routing } ){{}}";
196203
197204 // Add typeof(T) as the default type when only index specified
198- if ( ( m == "UpdateByQueryDescriptor" ) && cp . Count ( ) == 1 && ! string . IsNullOrEmpty ( this . RequestTypeGeneric ) )
205+ if ( ( m == "UpdateByQueryDescriptor" ) && requiredUrlRouteParameters . Count ( ) == 1 && ! string . IsNullOrEmpty ( this . RequestTypeGeneric ) )
199206 {
200207 var generic = this . RequestTypeGeneric . Replace ( "<" , "" ) . Replace ( ">" , "" ) ;
201208 generated = $ "public { m } ({ par } ) : base({ routing } .Required(\" type\" , (Types)typeof({ generic } ))){{}}";
202209 }
203210
211+ // Add typeof(T) as the default index to use for Suggest
212+ if ( ( m == "SuggestDescriptor" ) && ! string . IsNullOrEmpty ( this . RequestTypeGeneric ) )
213+ {
214+ var generic = this . RequestTypeGeneric . Replace ( "<" , "" ) . Replace ( ">" , "" ) ;
215+ doc = AppendToSummary ( doc , ". Will infer the index from the generic type" ) ;
216+ generated = $ "public { m } ({ par } ) : base(r => r.Required(\" index\" , (Indices)typeof({ generic } ))){{}}";
217+ }
218+
204219 var c = new Constructor { Generated = generated , Description = doc } ;
205220 ctors . Add ( c ) ;
206221 }
@@ -288,6 +303,13 @@ public IEnumerable<FluentRouteSetter> GetFluentRouteSetters()
288303 return setters ;
289304 }
290305
306+ private string AppendToSummary ( string doc , string toAppend )
307+ {
308+ return Regex . Replace (
309+ doc ,
310+ @"^(\/\/\/ <summary>)(.*?)(<\/summary>)(.*)" ,
311+ "$1$2" + toAppend + "$3$4" ) ;
312+ }
291313
292314 private bool IsPartless => this . Url . Parts == null || ! this . Url . Parts . Any ( ) ;
293315 private bool IsScroll => this . Url . Parts . All ( p => p . Key == "scroll_id" ) ;
0 commit comments