Skip to content

Commit 81986a4

Browse files
committed
fix #1940 dispatch special handling of _all cascades falls flat when routes have no fall over url e.g DELETE /{index} where index is required
1 parent bf24902 commit 81986a4

File tree

5 files changed

+172
-130
lines changed

5 files changed

+172
-130
lines changed

src/CodeGeneration/CodeGeneration.LowLevelClient/Domain/ApiEndpoint.cs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,15 @@ public string IfCheck
2222
{
2323
get
2424
{
25-
var parts = this.CsharpMethod.Parts.Where(p => p.Name != "body")
26-
.Select(p => $"p.RouteValues.{p.Name.ToPascalCase()}").ToList();
25+
var parts = this.CsharpMethod.Parts.Where(p => p.Name != "body").ToList();
2726
if (!parts.Any()) return string.Empty;
28-
return $"AllSet({string.Join(", ", parts)})";
27+
28+
var allPartsAreRequired = parts.Any() && parts.All(p => p.Required);
29+
var call = allPartsAreRequired ? "AllSetNoFallback" : "AllSet";
30+
var assignments = parts
31+
.Select(p => $"p.RouteValues.{p.Name.ToPascalCase()}").ToList();
32+
33+
return $"{call}({string.Join(", ", assignments)})";
2934
}
3035
}
3136
}
@@ -81,7 +86,7 @@ public string OptionallyAppendHttpMethod(IEnumerable<string> availableMethods, s
8186
//if on operation has two endpoints and one of them is GET always favor the other as default
8287
return currentHttpMethod == "GET" ? "Get" : string.Empty;
8388
}
84-
89+
8590
return availableMethods.First() == currentHttpMethod ? string.Empty : this.PascalCase(currentHttpMethod);
8691
}
8792

@@ -154,7 +159,7 @@ public IEnumerable<CsharpMethod> CsharpMethods
154159
ReturnType = "ElasticsearchResponse<T>",
155160
ReturnTypeGeneric = "<T>",
156161
CallTypeGeneric = "T",
157-
ReturnDescription =
162+
ReturnDescription =
158163
"ElasticsearchResponse&lt;T&gt; where the behavior depends on the type of T:"
159164
+ explanationOfT,
160165
FullName = methodName,
@@ -166,8 +171,8 @@ public IEnumerable<CsharpMethod> CsharpMethods
166171
};
167172
Generator.PatchMethod(apiMethod);
168173

169-
args = args.Concat(new[]
170-
{
174+
args = args.Concat(new[]
175+
{
171176
"Func<"+apiMethod.QueryStringParamName+", " + apiMethod.QueryStringParamName + "> requestParameters = null"
172177
}).ToList();
173178
apiMethod.Arguments = string.Join(", ", args);
@@ -179,7 +184,7 @@ public IEnumerable<CsharpMethod> CsharpMethods
179184
ReturnType = "Task<ElasticsearchResponse<T>>",
180185
ReturnTypeGeneric = "<T>",
181186
CallTypeGeneric = "T",
182-
ReturnDescription =
187+
ReturnDescription =
183188
"A task of ElasticsearchResponse&lt;T&gt; where the behaviour depends on the type of T:"
184189
+ explanationOfT,
185190
FullName = methodName + "Async",
@@ -192,15 +197,15 @@ public IEnumerable<CsharpMethod> CsharpMethods
192197
};
193198
Generator.PatchMethod(apiMethod);
194199
yield return apiMethod;
195-
200+
196201
//No need for deserialization state when returning dynamicdictionary
197202

198203
var explanationOfDynamic =
199-
paraIndent +
204+
paraIndent +
200205
"<para> - Dynamic dictionary is a special dynamic type that allows json to be traversed safely </para>"
201-
+ paraIndent +
206+
+ paraIndent +
202207
"<para> - i.e result.Response.hits.hits[0].property.nested[\"nested_deeper\"] </para>"
203-
+ paraIndent +
208+
+ paraIndent +
204209
"<para> - can be safely dispatched to a nullable type even if intermediate properties do not exist </para>";
205210

206211
var defaultBoundGeneric = Url.Path.Contains("_cat") ? "string" : "DynamicDictionary";
@@ -212,7 +217,7 @@ public IEnumerable<CsharpMethod> CsharpMethods
212217
ReturnTypeGeneric = null,
213218
//CallTypeGeneric = defaultBoundGeneric == "DynamicDictionary" ? "Dictionary<string, object>" : defaultBoundGeneric,
214219
CallTypeGeneric = defaultBoundGeneric,
215-
ReturnDescription =
220+
ReturnDescription =
216221
"ElasticsearchResponse&lt;DynamicDictionary&gt;"
217222
+ explanationOfDynamic,
218223
FullName = methodName,
@@ -225,15 +230,15 @@ public IEnumerable<CsharpMethod> CsharpMethods
225230
};
226231
Generator.PatchMethod(apiMethod);
227232
yield return apiMethod;
228-
233+
229234
apiMethod = new CsharpMethod
230235
{
231236
QueryStringParamName = queryStringParamName,
232237
ReturnType = $"Task<ElasticsearchResponse<{defaultBoundGeneric}>>",
233238
ReturnTypeGeneric = null,
234239
//CallTypeGeneric = defaultBoundGeneric == "DynamicDictionary" ? "Dictionary<string, object>" : defaultBoundGeneric,
235240
CallTypeGeneric = defaultBoundGeneric,
236-
ReturnDescription =
241+
ReturnDescription =
237242
"A task of ElasticsearchResponse&lt;DynamicDictionary$gt;"
238243
+ explanationOfDynamic,
239244
FullName = methodName + "Async",
@@ -251,4 +256,4 @@ public IEnumerable<CsharpMethod> CsharpMethods
251256
}
252257
}
253258
}
254-
}
259+
}

src/Nest/CommonAbstractions/LowLevelDispatch/LowLevelDispatch.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ public LowLevelDispatch(IElasticLowLevelClient rawElasticClient)
1515
this._lowLevel = rawElasticClient;
1616
}
1717

18-
internal bool AllSet(params string[] pathVariables) => pathVariables.All(p => !p.IsNullOrEmpty()) && !pathVariables.All(p => p == "_all");
18+
internal bool AllSetNoFallback(params string[] pathVariables) => pathVariables.All(p => !p.IsNullOrEmpty());
1919

20-
internal bool AllSet(params IUrlParameter[] pathVariables) => pathVariables.All(p => p != null);
20+
internal bool AllSet(params string[] pathVariables) => pathVariables.All(p => !p.IsNullOrEmpty()) && !pathVariables.All(p => p == "_all");
2121

2222
internal static Exception InvalidDispatch(string apiCall, IRequest provided, HttpMethod[] methods, params string[] endpoints)
2323
{
@@ -38,7 +38,7 @@ internal static string PrettyPrintEndpoint(IRequest request, string endpoint)
3838
{
3939
var pretty = ReplaceParams.Replace(endpoint, (m) =>
4040
{
41-
var key = m.Groups[1].Value.ToLowerInvariant();
41+
var key = m.Groups[1].Value.ToLowerInvariant();
4242
switch(key)
4343
{
4444
case "index" : return PrettyParam(key, request.RouteValues.Index);

0 commit comments

Comments
 (0)