Skip to content

Commit 0783dbb

Browse files
russcamMpdreamz
authored andcommitted
Set Accept header when format set on Cat APIs (#4269)
* Set Accept header when format set on Cat APIs This commit updates the ApiGenerator to set the Accept HTTP header when the format header is specified. To determine whether an API call is successful, the Content-Type header of the response is checked against the Accept header of the request. The format parameter only really makes sense for the low level client and associated ElasticsearchResponse<T> types, as the high level client always expects to deserialize from JSON; any other format will result in an exception at deserialization. Fixes #4243 * Addressed PR comments (cherry picked from commit 7cb539a)
1 parent 29b641d commit 0783dbb

File tree

15 files changed

+291
-53
lines changed

15 files changed

+291
-53
lines changed

src/CodeGeneration/ApiGenerator/Domain/Specification/QueryParameters.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public string TypeLowLevel
128128
}
129129

130130

131-
public string InitializerGenerator(string type, string name, string key, string setter, params string[] doc) =>
132-
CodeGenerator.Property(type, name, key, setter, Obsolete, doc);
131+
public string InitializerGenerator(string @namespace, string type, string name, string key, string setter, params string[] doc) =>
132+
CodeGenerator.Property(@namespace, type, name, key, setter, Obsolete, doc);
133133
}
134134
}

src/CodeGeneration/ApiGenerator/Generator/CodeGenerator.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,26 @@ namespace ApiGenerator.Generator
99
//TODO this should be in views and models
1010
public static class CodeGenerator
1111
{
12+
public static string CatFormatPropertyGenerator(string type, string name, string key, string setter) =>
13+
$"public {type} {name} {{ "
14+
+ $" get => Q<{type}>(\"{key}\");"
15+
+ $" set {{ Q(\"{key}\", {setter}); SetAcceptHeader({setter}); }}"
16+
+ $"}}";
17+
1218
public static string PropertyGenerator(string type, string name, string key, string setter) =>
1319
$"public {type} {name} {{ get => Q<{type}>(\"{key}\"); set => Q(\"{key}\", {setter}); }}";
1420

15-
public static string Property(string type, string name, string key, string setter, string obsolete, params string[] doc)
21+
public static string Property(string @namespace, string type, string name, string key, string setter, string obsolete, params string[] doc)
1622
{
1723
var components = new List<string>();
1824
foreach (var d in RenderDocumentation(doc)) A(d);
1925
if (!string.IsNullOrWhiteSpace(obsolete)) A($"[Obsolete(\"Scheduled to be removed in 7.0, {obsolete}\")]");
2026

21-
A(PropertyGenerator(type, name, key, setter));
27+
var generated = @namespace != null && @namespace == "Cat" && name == "Format"
28+
? CatFormatPropertyGenerator(type, name, key, setter)
29+
: PropertyGenerator(type, name, key, setter);
30+
31+
A(generated);
2232
return string.Join($"{Environment.NewLine}\t\t", components);
2333

2434
void A(string s)

src/CodeGeneration/ApiGenerator/Views/HighLevel/Requests/PlainRequestBase.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace Nest
2626
{
2727
@foreach (var common in RestApiSpec.CommonApiQueryParameters.Values)
2828
{
29-
<text> @Raw(common.InitializerGenerator(common.TypeHighLevel, common.ClsName, common.QueryStringKey, "value", common.DescriptionHighLevel.ToArray()))
29+
<text> @Raw(common.InitializerGenerator(null, common.TypeHighLevel, common.ClsName, common.QueryStringKey, "value", common.DescriptionHighLevel.ToArray()))
3030
</text>
3131
}
3232
}

src/CodeGeneration/ApiGenerator/Views/HighLevel/Requests/RequestImplementations.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
continue;
6565
}
6666
var doc = param.DescriptionHighLevel.ToArray();
67-
<text> @Raw(param.InitializerGenerator(param.TypeHighLevel, param.ClsName, original, param.SetterHighLevel, doc))
67+
<text> @Raw(param.InitializerGenerator(r.CsharpNames.Namespace, param.TypeHighLevel, param.ClsName, original, param.SetterHighLevel, doc))
6868
</text>
6969
}
7070
@if (names.DescriptorNotFoundInCodebase)

src/CodeGeneration/ApiGenerator/Views/LowLevel/RequestParameters/RequestParameters.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace Elasticsearch.Net@(ns)
3737
public override bool SupportsBody => @(supportsBody ? "true" : "false");
3838
@foreach (var param in r.Params)
3939
{
40-
<text> @Raw(param.InitializerGenerator(param.TypeLowLevel, param.ClsName, param.QueryStringKey, param.SetterLowLevel, param.Description))
40+
<text> @Raw(param.InitializerGenerator(r.CsharpNames.Namespace, param.TypeLowLevel, param.ClsName, param.QueryStringKey, param.SetterLowLevel, param.Description))
4141
</text>
4242
}
4343
}</text>

src/Elasticsearch.Net/Api/RequestParameters/IRequestParameters.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,11 @@ public interface IRequestParameters
4040
/// Gets the stringified representation of a query string value as it would be sent to Elasticsearch.
4141
/// </summary>
4242
string GetResolvedQueryStringValue(string n, IConnectionConfigurationValues s);
43+
44+
/// <summary>
45+
/// Gets the HTTP Accept Header value from the shortened name. If the shortened name is not recognized,
46+
/// <c>null</c> is returned.
47+
/// </summary>
48+
string AcceptHeaderFromFormat(string format);
4349
}
4450
}

src/Elasticsearch.Net/Api/RequestParameters/RequestParameters.Cat.cs

Lines changed: 95 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ public class CatAliasesRequestParameters : RequestParameters<CatAliasesRequestPa
3333
public string Format
3434
{
3535
get => Q<string>("format");
36-
set => Q("format", value);
36+
set
37+
{
38+
Q("format", value);
39+
SetAcceptHeader(value);
40+
}
3741
}
3842

3943
///<summary>Comma-separated list of column names to display</summary>
@@ -88,7 +92,11 @@ public Bytes? Bytes
8892
public string Format
8993
{
9094
get => Q<string>("format");
91-
set => Q("format", value);
95+
set
96+
{
97+
Q("format", value);
98+
SetAcceptHeader(value);
99+
}
92100
}
93101

94102
///<summary>Comma-separated list of column names to display</summary>
@@ -143,7 +151,11 @@ public class CatCountRequestParameters : RequestParameters<CatCountRequestParame
143151
public string Format
144152
{
145153
get => Q<string>("format");
146-
set => Q("format", value);
154+
set
155+
{
156+
Q("format", value);
157+
SetAcceptHeader(value);
158+
}
147159
}
148160

149161
///<summary>Comma-separated list of column names to display</summary>
@@ -198,7 +210,11 @@ public string[] Fields
198210
public string Format
199211
{
200212
get => Q<string>("format");
201-
set => Q("format", value);
213+
set
214+
{
215+
Q("format", value);
216+
SetAcceptHeader(value);
217+
}
202218
}
203219

204220
///<summary>Comma-separated list of column names to display</summary>
@@ -239,7 +255,11 @@ public class CatHealthRequestParameters : RequestParameters<CatHealthRequestPara
239255
public string Format
240256
{
241257
get => Q<string>("format");
242-
set => Q("format", value);
258+
set
259+
{
260+
Q("format", value);
261+
SetAcceptHeader(value);
262+
}
243263
}
244264

245265
///<summary>Comma-separated list of column names to display</summary>
@@ -314,7 +334,11 @@ public Bytes? Bytes
314334
public string Format
315335
{
316336
get => Q<string>("format");
317-
set => Q("format", value);
337+
set
338+
{
339+
Q("format", value);
340+
SetAcceptHeader(value);
341+
}
318342
}
319343

320344
///<summary>Comma-separated list of column names to display</summary>
@@ -390,7 +414,11 @@ public class CatMasterRequestParameters : RequestParameters<CatMasterRequestPara
390414
public string Format
391415
{
392416
get => Q<string>("format");
393-
set => Q("format", value);
417+
set
418+
{
419+
Q("format", value);
420+
SetAcceptHeader(value);
421+
}
394422
}
395423

396424
///<summary>Comma-separated list of column names to display</summary>
@@ -445,7 +473,11 @@ public class CatNodeAttributesRequestParameters : RequestParameters<CatNodeAttri
445473
public string Format
446474
{
447475
get => Q<string>("format");
448-
set => Q("format", value);
476+
set
477+
{
478+
Q("format", value);
479+
SetAcceptHeader(value);
480+
}
449481
}
450482

451483
///<summary>Comma-separated list of column names to display</summary>
@@ -507,7 +539,11 @@ public Bytes? Bytes
507539
public string Format
508540
{
509541
get => Q<string>("format");
510-
set => Q("format", value);
542+
set
543+
{
544+
Q("format", value);
545+
SetAcceptHeader(value);
546+
}
511547
}
512548

513549
///<summary>Return the full node ID instead of the shortened version (default: false)</summary>
@@ -569,7 +605,11 @@ public class CatPendingTasksRequestParameters : RequestParameters<CatPendingTask
569605
public string Format
570606
{
571607
get => Q<string>("format");
572-
set => Q("format", value);
608+
set
609+
{
610+
Q("format", value);
611+
SetAcceptHeader(value);
612+
}
573613
}
574614

575615
///<summary>Comma-separated list of column names to display</summary>
@@ -624,7 +664,11 @@ public class CatPluginsRequestParameters : RequestParameters<CatPluginsRequestPa
624664
public string Format
625665
{
626666
get => Q<string>("format");
627-
set => Q("format", value);
667+
set
668+
{
669+
Q("format", value);
670+
SetAcceptHeader(value);
671+
}
628672
}
629673

630674
///<summary>Comma-separated list of column names to display</summary>
@@ -700,7 +744,11 @@ public bool? Detailed
700744
public string Format
701745
{
702746
get => Q<string>("format");
703-
set => Q("format", value);
747+
set
748+
{
749+
Q("format", value);
750+
SetAcceptHeader(value);
751+
}
704752
}
705753

706754
///<summary>Comma-separated list of column names to display</summary>
@@ -748,7 +796,11 @@ public class CatRepositoriesRequestParameters : RequestParameters<CatRepositorie
748796
public string Format
749797
{
750798
get => Q<string>("format");
751-
set => Q("format", value);
799+
set
800+
{
801+
Q("format", value);
802+
SetAcceptHeader(value);
803+
}
752804
}
753805

754806
///<summary>Comma-separated list of column names to display</summary>
@@ -810,7 +862,11 @@ public Bytes? Bytes
810862
public string Format
811863
{
812864
get => Q<string>("format");
813-
set => Q("format", value);
865+
set
866+
{
867+
Q("format", value);
868+
SetAcceptHeader(value);
869+
}
814870
}
815871

816872
///<summary>Comma-separated list of column names to display</summary>
@@ -858,7 +914,11 @@ public Bytes? Bytes
858914
public string Format
859915
{
860916
get => Q<string>("format");
861-
set => Q("format", value);
917+
set
918+
{
919+
Q("format", value);
920+
SetAcceptHeader(value);
921+
}
862922
}
863923

864924
///<summary>Comma-separated list of column names to display</summary>
@@ -913,7 +973,11 @@ public class CatSnapshotsRequestParameters : RequestParameters<CatSnapshotsReque
913973
public string Format
914974
{
915975
get => Q<string>("format");
916-
set => Q("format", value);
976+
set
977+
{
978+
Q("format", value);
979+
SetAcceptHeader(value);
980+
}
917981
}
918982

919983
///<summary>Comma-separated list of column names to display</summary>
@@ -982,7 +1046,11 @@ public bool? Detailed
9821046
public string Format
9831047
{
9841048
get => Q<string>("format");
985-
set => Q("format", value);
1049+
set
1050+
{
1051+
Q("format", value);
1052+
SetAcceptHeader(value);
1053+
}
9861054
}
9871055

9881056
///<summary>Comma-separated list of column names to display</summary>
@@ -1040,7 +1108,11 @@ public class CatTemplatesRequestParameters : RequestParameters<CatTemplatesReque
10401108
public string Format
10411109
{
10421110
get => Q<string>("format");
1043-
set => Q("format", value);
1111+
set
1112+
{
1113+
Q("format", value);
1114+
SetAcceptHeader(value);
1115+
}
10441116
}
10451117

10461118
///<summary>Comma-separated list of column names to display</summary>
@@ -1095,7 +1167,11 @@ public class CatThreadPoolRequestParameters : RequestParameters<CatThreadPoolReq
10951167
public string Format
10961168
{
10971169
get => Q<string>("format");
1098-
set => Q("format", value);
1170+
set
1171+
{
1172+
Q("format", value);
1173+
SetAcceptHeader(value);
1174+
}
10991175
}
11001176

11011177
///<summary>Comma-separated list of column names to display</summary>

src/Elasticsearch.Net/Api/RequestParameters/RequestParameters.cs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ public abstract class RequestParameters<T> : IRequestParameters where T : Reques
2828
public TOut GetQueryStringValue<TOut>(string name)
2929
{
3030
if (!ContainsQueryString(name))
31-
return default(TOut);
31+
return default;
3232

3333
var value = Self.QueryString[name];
3434
if (value == null)
35-
return default(TOut);
35+
return default;
3636

3737
return (TOut)value;
3838
}
@@ -59,5 +59,33 @@ private void RemoveQueryString(string name)
5959

6060
Self.QueryString.Remove(name);
6161
}
62+
63+
protected void SetAcceptHeader(string format)
64+
{
65+
if (RequestConfiguration == null)
66+
RequestConfiguration = new RequestConfiguration();
67+
68+
RequestConfiguration.Accept = AcceptHeaderFromFormat(format);
69+
}
70+
71+
/// <inheritdoc />
72+
public string AcceptHeaderFromFormat(string format)
73+
{
74+
if (format == null)
75+
return null;
76+
77+
var lowerFormat = format.ToLowerInvariant();
78+
79+
switch(lowerFormat)
80+
{
81+
case "smile":
82+
case "yaml":
83+
case "cbor":
84+
case "json":
85+
return $"application/{lowerFormat}";
86+
default:
87+
return null;
88+
}
89+
}
6290
}
6391
}

src/Elasticsearch.Net/Transport/Pipeline/RequestData.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ IMemoryStreamFactory memoryStreamFactory
115115
public string ProxyAddress { get; }
116116
public SecureString ProxyPassword { get; }
117117
public string ProxyUsername { get; }
118+
// TODO: rename to ContentType in 8.0.0
118119
public string RequestMimeType { get; }
119120
public TimeSpan RequestTimeout { get; }
120121
public string RunAs { get; }

0 commit comments

Comments
 (0)