Skip to content

Commit 0ab5078

Browse files
committed
Cat endpoints are now generated with a text/plain request settings and different default return type, fixes failing _cat yaml tests
1 parent 47898e0 commit 0ab5078

27 files changed

+1267
-268
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ public IEnumerable<CsharpMethod> CsharpMethods
156156
QueryStringParamName = queryStringParamName,
157157
ReturnType = "ElasticsearchResponse<T>",
158158
ReturnTypeGeneric = "<T>",
159+
CallTypeGeneric = "T",
159160
ReturnDescription =
160161
"ElasticsearchResponse&lt;T&gt; holding the reponse body deserialized as T."
161162
+ explanationOfT,
@@ -180,6 +181,7 @@ public IEnumerable<CsharpMethod> CsharpMethods
180181
QueryStringParamName = queryStringParamName,
181182
ReturnType = "Task<ElasticsearchResponse<T>>",
182183
ReturnTypeGeneric = "<T>",
184+
CallTypeGeneric = "T",
183185
ReturnDescription =
184186
"A task that'll return an ElasticsearchResponse&lt;T&gt; holding the reponse body deserialized as T."
185187
+ explanationOfT,
@@ -204,11 +206,15 @@ public IEnumerable<CsharpMethod> CsharpMethods
204206
+ paraIndent +
205207
"<para> - can be safely dispatched to a nullable type even if intermediate properties do not exist</para>";
206208

209+
var defaultBoundGeneric = Url.Path.Contains("_cat") ? "string" : "DynamicDictionary";
210+
207211
apiMethod = new CsharpMethod
208212
{
209213
QueryStringParamName = queryStringParamName,
210-
ReturnType = "ElasticsearchResponse<DynamicDictionary>",
214+
ReturnType = string.Format("ElasticsearchResponse<{0}>", defaultBoundGeneric),
211215
ReturnTypeGeneric = null,
216+
CallTypeGeneric = defaultBoundGeneric == "DynamicDictionary"
217+
? "Dictionary<string, object>" : defaultBoundGeneric,
212218
ReturnDescription =
213219
"ElasticsearchResponse&lt;T&gt; holding the response body deserialized as DynamicDictionary"
214220
+ explanationOfDynamic,
@@ -226,8 +232,10 @@ public IEnumerable<CsharpMethod> CsharpMethods
226232
apiMethod = new CsharpMethod
227233
{
228234
QueryStringParamName = queryStringParamName,
229-
ReturnType = "Task<ElasticsearchResponse<DynamicDictionary>>",
235+
ReturnType = string.Format("Task<ElasticsearchResponse<{0}>>", defaultBoundGeneric),
230236
ReturnTypeGeneric = null,
237+
CallTypeGeneric = defaultBoundGeneric == "DynamicDictionary"
238+
? "Dictionary<string, object>" : defaultBoundGeneric,
231239
ReturnDescription =
232240
"Task that'll return an ElasticsearchResponse&lt;T$gt; holding the response body deserialized as DynamicDictionary"
233241
+ explanationOfDynamic,

src/CodeGeneration/CodeGeneration.LowLevelClient/Domain/CsharpMethod.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ public class CsharpMethod
66
{
77
public string ReturnType { get; set; }
88
public string ReturnTypeGeneric { get; set; }
9+
public string CallTypeGeneric { get; set; }
910
public string ReturnDescription { get; set; }
1011
public string FullName { get; set; }
1112
public string QueryStringParamName { get; set; }

src/CodeGeneration/CodeGeneration.LowLevelClient/Views/ElasticsearchClient.Generated.cshtml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,20 @@ namespace Elasticsearch.Net
6565
BaseRequestParameters requestParams = null;
6666
if (requestParameters != null)
6767
{
68-
requestParams = requestParameters(new @(method.QueryStringParamName)());
68+
requestParams = requestParameters(new @(method.QueryStringParamName)()@(url.StartsWith("_cat") ? Raw(".RequestConfiguration(r=>r.ContentType(\"text/plain\"))") : null));
6969
ToNameValueCollection(requestParams);
7070
}
71+
@if (url.StartsWith("_cat"))
72+
{
73+
<text> else requestParams = new @(method.QueryStringParamName)().RequestConfiguration(r=>r.ContentType("text/plain")); </text>
74+
}
75+
7176
@{
7277
bool isAsync = method.ReturnType.StartsWith("Task<");
7378
string requestMethod = isAsync ? "DoRequestAsync" : "DoRequest";
74-
bool wrap = method.ReturnTypeGeneric == null;
79+
bool wrap = method.CallTypeGeneric == "Dictionary<string, object>";
7580
}
76-
return @(wrap ? "ElasticsearchResponse.Wrap"+ (isAsync ? "Async" : "") +"(" : "")this.@(requestMethod)@(Raw(method.ReturnTypeGeneric ?? "<Dictionary<string, object>>"))(
81+
return @(wrap ? "ElasticsearchResponse.Wrap"+ (isAsync ? "Async" : "") +"(" : "")this.@(requestMethod)@(Raw("<" + method.CallTypeGeneric + ">"))(
7782
"@method.HttpMethod", url@(method.Parts.Any(pp=>pp.Name == "body") ? ", body" : ", data: null"),
7883
requestParameters: requestParams
7984
)@(wrap ? ")" : "");

src/Elasticsearch.Net/Connection/HttpConnection.cs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,46 +71,46 @@ public virtual ElasticsearchResponse<Stream> DeleteSync(Uri uri, byte[] data, IR
7171

7272
private ElasticsearchResponse<Stream> HeaderOnlyRequest(Uri uri, string method, IRequestConnectionConfiguration requestSpecificConfig)
7373
{
74-
var r = this.CreateHttpWebRequest(uri, method);
74+
var r = this.CreateHttpWebRequest(uri, method, requestSpecificConfig);
7575
return this.DoSynchronousRequest(r, requestSpecificConfig: requestSpecificConfig);
7676
}
7777

7878
private ElasticsearchResponse<Stream> BodyRequest(Uri uri, byte[] data, string method, IRequestConnectionConfiguration requestSpecificConfig)
7979
{
80-
var r = this.CreateHttpWebRequest(uri, method);
80+
var r = this.CreateHttpWebRequest(uri, method, requestSpecificConfig);
8181
return this.DoSynchronousRequest(r, data, requestSpecificConfig);
8282
}
8383

8484
public virtual Task<ElasticsearchResponse<Stream>> Get(Uri uri, IRequestConnectionConfiguration requestSpecificConfig = null)
8585
{
86-
var r = this.CreateHttpWebRequest(uri, "GET");
86+
var r = this.CreateHttpWebRequest(uri, "GET", requestSpecificConfig);
8787
return this.DoAsyncRequest(r, requestSpecificConfig: requestSpecificConfig);
8888
}
8989
public virtual Task<ElasticsearchResponse<Stream>> Head(Uri uri, IRequestConnectionConfiguration requestSpecificConfig = null)
9090
{
91-
var r = this.CreateHttpWebRequest(uri, "HEAD");
91+
var r = this.CreateHttpWebRequest(uri, "HEAD", requestSpecificConfig);
9292
return this.DoAsyncRequest(r, requestSpecificConfig: requestSpecificConfig);
9393
}
9494
public virtual Task<ElasticsearchResponse<Stream>> Post(Uri uri, byte[] data, IRequestConnectionConfiguration requestSpecificConfig = null)
9595
{
96-
var r = this.CreateHttpWebRequest(uri, "POST");
96+
var r = this.CreateHttpWebRequest(uri, "POST", requestSpecificConfig);
9797
return this.DoAsyncRequest(r, data, requestSpecificConfig: requestSpecificConfig);
9898
}
9999

100100
public virtual Task<ElasticsearchResponse<Stream>> Put(Uri uri, byte[] data, IRequestConnectionConfiguration requestSpecificConfig = null)
101101
{
102-
var r = this.CreateHttpWebRequest(uri, "PUT");
102+
var r = this.CreateHttpWebRequest(uri, "PUT", requestSpecificConfig);
103103
return this.DoAsyncRequest(r, data, requestSpecificConfig: requestSpecificConfig);
104104
}
105105

106106
public virtual Task<ElasticsearchResponse<Stream>> Delete(Uri uri, byte[] data, IRequestConnectionConfiguration requestSpecificConfig = null)
107107
{
108-
var r = this.CreateHttpWebRequest(uri, "DELETE");
108+
var r = this.CreateHttpWebRequest(uri, "DELETE", requestSpecificConfig);
109109
return this.DoAsyncRequest(r, data, requestSpecificConfig: requestSpecificConfig);
110110
}
111111
public virtual Task<ElasticsearchResponse<Stream>> Delete(Uri uri, IRequestConnectionConfiguration requestSpecificConfig = null)
112112
{
113-
var r = this.CreateHttpWebRequest(uri, "DELETE");
113+
var r = this.CreateHttpWebRequest(uri, "DELETE", requestSpecificConfig);
114114
return this.DoAsyncRequest(r, requestSpecificConfig: requestSpecificConfig);
115115
}
116116

@@ -127,9 +127,9 @@ private static void ThreadTimeoutCallback(object state, bool timedOut)
127127
}
128128

129129

130-
protected virtual HttpWebRequest CreateHttpWebRequest(Uri uri, string method)
130+
protected virtual HttpWebRequest CreateHttpWebRequest(Uri uri, string method, IRequestConnectionConfiguration requestSpecificConfig)
131131
{
132-
var myReq = this.CreateWebRequest(uri, method);
132+
var myReq = this.CreateWebRequest(uri, method, requestSpecificConfig);
133133
this.SetBasicAuthorizationIfNeeded(myReq);
134134
this.SetProxyIfNeeded(myReq);
135135
return myReq;
@@ -160,17 +160,19 @@ private void SetBasicAuthorizationIfNeeded(HttpWebRequest myReq)
160160
//}
161161
}
162162

163-
protected virtual HttpWebRequest CreateWebRequest(Uri uri, string method)
163+
protected virtual HttpWebRequest CreateWebRequest(Uri uri, string method, IRequestConnectionConfiguration requestSpecificConfig)
164164
{
165165
//TODO append global querystring
166166
//var url = this._CreateUriString(path);
167167

168168
var myReq = (HttpWebRequest)WebRequest.Create(uri);
169-
//TODO move this to transport
170-
if (!uri.AbsolutePath.StartsWith("_cat"))
169+
170+
myReq.Accept = "application/json";
171+
myReq.ContentType = "application/json";
172+
if (requestSpecificConfig != null && !string.IsNullOrWhiteSpace(requestSpecificConfig.AcceptsContentType))
171173
{
172-
myReq.Accept = "application/json";
173-
myReq.ContentType = "application/json";
174+
myReq.Accept = requestSpecificConfig.AcceptsContentType;
175+
myReq.ContentType = requestSpecificConfig.AcceptsContentType;
174176
}
175177
var timeout = this._ConnectionSettings.Timeout;
176178
myReq.Timeout = timeout; // 1 minute timeout.

src/Elasticsearch.Net/Connection/IRequestConfiguration.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,6 @@ public bool? PingDisabled
6161
get { return _pingDisabled; }
6262
}
6363

64-
public string AcceptsContentType
65-
{
66-
get { return _acceptsContentType; }
67-
}
6864

6965
public T DisableSniffing(bool? disable = true)
7066
{

src/Elasticsearch.Net/Connection/Transport.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public IList<Uri> Sniff()
9090
.RequestTimeout(pingTimeout)
9191
.DisableSniffing();
9292
var requestParameters = new FluentRequestParameters()
93-
.RequestConfiguration(requestOverrides);
93+
.RequestConfiguration(r => requestOverrides);
9494

9595
var path = "_nodes/_all/clear?timeout=" + pingTimeout;
9696

src/Elasticsearch.Net/Domain/BaseRequestParameters.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public abstract class BaseRequestParameters : IRequestParameters
1010

1111
internal object _DeserializationState = null;
1212

13-
internal IRequestConfiguration _RequestConfiguration = null;
13+
internal RequestConfiguration _RequestConfiguration = null;
1414
internal NameValueCollection _queryString;
1515

1616
NameValueCollection IRequestParameters.QueryString { get { return _queryString;} }

src/Elasticsearch.Net/Domain/FluentRequestParameters.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,13 @@ public T Add(string name, object value)
2121
return (T)this;
2222
}
2323

24-
public T RequestConfiguration(Func<RequestConfiguration, IRequestConfiguration> selector)
24+
public T RequestConfiguration(Func<RequestConfiguration, RequestConfiguration> selector)
2525
{
2626
selector.ThrowIfNull("selector");
27-
this._RequestConfiguration = selector(new RequestConfiguration());
28-
return (T)this;
29-
}public T RequestConfiguration(IRequestConfiguration requestConfiguration)
30-
{
31-
this._RequestConfiguration = requestConfiguration;
27+
this._RequestConfiguration = selector(this._RequestConfiguration ?? new RequestConfiguration());
3228
return (T)this;
3329
}
30+
3431
public T DeserializationState(object deserializationState)
3532
{
3633
_DeserializationState = deserializationState;

0 commit comments

Comments
 (0)