Skip to content

Commit 7cb539a

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
1 parent 53faea4 commit 7cb539a

File tree

15 files changed

+291
-53
lines changed

15 files changed

+291
-53
lines changed

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

+2-2
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

+12-2
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

+1-1
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

+1-1
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace Elasticsearch.Net@(ns)
3535
public override HttpMethod DefaultHttpMethod => HttpMethod.@r.HttpMethod;
3636
@foreach (var param in r.Params)
3737
{
38-
<text> @Raw(param.InitializerGenerator(param.TypeLowLevel, param.ClsName, param.QueryStringKey, param.SetterLowLevel, param.Description))
38+
<text> @Raw(param.InitializerGenerator(r.CsharpNames.Namespace, param.TypeLowLevel, param.ClsName, param.QueryStringKey, param.SetterLowLevel, param.Description))
3939
</text>
4040
}
4141
}</text>

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

+6
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,11 @@ public interface IRequestParameters
3838
/// Gets the stringified representation of a query string value as it would be sent to Elasticsearch.
3939
/// </summary>
4040
string GetResolvedQueryStringValue(string n, IConnectionConfigurationValues s);
41+
42+
/// <summary>
43+
/// Gets the HTTP Accept Header value from the shortened name. If the shortened name is not recognized,
44+
/// <c>null</c> is returned.
45+
/// </summary>
46+
string AcceptHeaderFromFormat(string format);
4147
}
4248
}

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

+95-19
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ public class CatAliasesRequestParameters : RequestParameters<CatAliasesRequestPa
3232
public string Format
3333
{
3434
get => Q<string>("format");
35-
set => Q("format", value);
35+
set
36+
{
37+
Q("format", value);
38+
SetAcceptHeader(value);
39+
}
3640
}
3741

3842
///<summary>Comma-separated list of column names to display</summary>
@@ -86,7 +90,11 @@ public class CatAllocationRequestParameters : RequestParameters<CatAllocationReq
8690
public string Format
8791
{
8892
get => Q<string>("format");
89-
set => Q("format", value);
93+
set
94+
{
95+
Q("format", value);
96+
SetAcceptHeader(value);
97+
}
9098
}
9199

92100
///<summary>Comma-separated list of column names to display</summary>
@@ -140,7 +148,11 @@ public class CatCountRequestParameters : RequestParameters<CatCountRequestParame
140148
public string Format
141149
{
142150
get => Q<string>("format");
143-
set => Q("format", value);
151+
set
152+
{
153+
Q("format", value);
154+
SetAcceptHeader(value);
155+
}
144156
}
145157

146158
///<summary>Comma-separated list of column names to display</summary>
@@ -201,7 +213,11 @@ public string[] Fields
201213
public string Format
202214
{
203215
get => Q<string>("format");
204-
set => Q("format", value);
216+
set
217+
{
218+
Q("format", value);
219+
SetAcceptHeader(value);
220+
}
205221
}
206222

207223
///<summary>Comma-separated list of column names to display</summary>
@@ -255,7 +271,11 @@ public class CatHealthRequestParameters : RequestParameters<CatHealthRequestPara
255271
public string Format
256272
{
257273
get => Q<string>("format");
258-
set => Q("format", value);
274+
set
275+
{
276+
Q("format", value);
277+
SetAcceptHeader(value);
278+
}
259279
}
260280

261281
///<summary>Comma-separated list of column names to display</summary>
@@ -335,7 +355,11 @@ public class CatIndicesRequestParameters : RequestParameters<CatIndicesRequestPa
335355
public string Format
336356
{
337357
get => Q<string>("format");
338-
set => Q("format", value);
358+
set
359+
{
360+
Q("format", value);
361+
SetAcceptHeader(value);
362+
}
339363
}
340364

341365
///<summary>Comma-separated list of column names to display</summary>
@@ -410,7 +434,11 @@ public class CatMasterRequestParameters : RequestParameters<CatMasterRequestPara
410434
public string Format
411435
{
412436
get => Q<string>("format");
413-
set => Q("format", value);
437+
set
438+
{
439+
Q("format", value);
440+
SetAcceptHeader(value);
441+
}
414442
}
415443

416444
///<summary>Comma-separated list of column names to display</summary>
@@ -464,7 +492,11 @@ public class CatNodeAttributesRequestParameters : RequestParameters<CatNodeAttri
464492
public string Format
465493
{
466494
get => Q<string>("format");
467-
set => Q("format", value);
495+
set
496+
{
497+
Q("format", value);
498+
SetAcceptHeader(value);
499+
}
468500
}
469501

470502
///<summary>Comma-separated list of column names to display</summary>
@@ -518,7 +550,11 @@ public class CatNodesRequestParameters : RequestParameters<CatNodesRequestParame
518550
public string Format
519551
{
520552
get => Q<string>("format");
521-
set => Q("format", value);
553+
set
554+
{
555+
Q("format", value);
556+
SetAcceptHeader(value);
557+
}
522558
}
523559

524560
///<summary>Return the full node ID instead of the shortened version (default: false)</summary>
@@ -579,7 +615,11 @@ public class CatPendingTasksRequestParameters : RequestParameters<CatPendingTask
579615
public string Format
580616
{
581617
get => Q<string>("format");
582-
set => Q("format", value);
618+
set
619+
{
620+
Q("format", value);
621+
SetAcceptHeader(value);
622+
}
583623
}
584624

585625
///<summary>Comma-separated list of column names to display</summary>
@@ -633,7 +673,11 @@ public class CatPluginsRequestParameters : RequestParameters<CatPluginsRequestPa
633673
public string Format
634674
{
635675
get => Q<string>("format");
636-
set => Q("format", value);
676+
set
677+
{
678+
Q("format", value);
679+
SetAcceptHeader(value);
680+
}
637681
}
638682

639683
///<summary>Comma-separated list of column names to display</summary>
@@ -701,7 +745,11 @@ public bool? Detailed
701745
public string Format
702746
{
703747
get => Q<string>("format");
704-
set => Q("format", value);
748+
set
749+
{
750+
Q("format", value);
751+
SetAcceptHeader(value);
752+
}
705753
}
706754

707755
///<summary>Comma-separated list of column names to display</summary>
@@ -755,7 +803,11 @@ public class CatRepositoriesRequestParameters : RequestParameters<CatRepositorie
755803
public string Format
756804
{
757805
get => Q<string>("format");
758-
set => Q("format", value);
806+
set
807+
{
808+
Q("format", value);
809+
SetAcceptHeader(value);
810+
}
759811
}
760812

761813
///<summary>Comma-separated list of column names to display</summary>
@@ -809,7 +861,11 @@ public class CatSegmentsRequestParameters : RequestParameters<CatSegmentsRequest
809861
public string Format
810862
{
811863
get => Q<string>("format");
812-
set => Q("format", value);
864+
set
865+
{
866+
Q("format", value);
867+
SetAcceptHeader(value);
868+
}
813869
}
814870

815871
///<summary>Comma-separated list of column names to display</summary>
@@ -849,7 +905,11 @@ public class CatShardsRequestParameters : RequestParameters<CatShardsRequestPara
849905
public string Format
850906
{
851907
get => Q<string>("format");
852-
set => Q("format", value);
908+
set
909+
{
910+
Q("format", value);
911+
SetAcceptHeader(value);
912+
}
853913
}
854914

855915
///<summary>Comma-separated list of column names to display</summary>
@@ -903,7 +963,11 @@ public class CatSnapshotsRequestParameters : RequestParameters<CatSnapshotsReque
903963
public string Format
904964
{
905965
get => Q<string>("format");
906-
set => Q("format", value);
966+
set
967+
{
968+
Q("format", value);
969+
SetAcceptHeader(value);
970+
}
907971
}
908972

909973
///<summary>Comma-separated list of column names to display</summary>
@@ -971,7 +1035,11 @@ public bool? Detailed
9711035
public string Format
9721036
{
9731037
get => Q<string>("format");
974-
set => Q("format", value);
1038+
set
1039+
{
1040+
Q("format", value);
1041+
SetAcceptHeader(value);
1042+
}
9751043
}
9761044

9771045
///<summary>Comma-separated list of column names to display</summary>
@@ -1028,7 +1096,11 @@ public class CatTemplatesRequestParameters : RequestParameters<CatTemplatesReque
10281096
public string Format
10291097
{
10301098
get => Q<string>("format");
1031-
set => Q("format", value);
1099+
set
1100+
{
1101+
Q("format", value);
1102+
SetAcceptHeader(value);
1103+
}
10321104
}
10331105

10341106
///<summary>Comma-separated list of column names to display</summary>
@@ -1082,7 +1154,11 @@ public class CatThreadPoolRequestParameters : RequestParameters<CatThreadPoolReq
10821154
public string Format
10831155
{
10841156
get => Q<string>("format");
1085-
set => Q("format", value);
1157+
set
1158+
{
1159+
Q("format", value);
1160+
SetAcceptHeader(value);
1161+
}
10861162
}
10871163

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

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

+30-2
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ public abstract class RequestParameters<T> : IRequestParameters where T : Reques
2727
public TOut GetQueryStringValue<TOut>(string name)
2828
{
2929
if (!ContainsQueryString(name))
30-
return default(TOut);
30+
return default;
3131

3232
var value = Self.QueryString[name];
3333
if (value == null)
34-
return default(TOut);
34+
return default;
3535

3636
return (TOut)value;
3737
}
@@ -58,5 +58,33 @@ private void RemoveQueryString(string name)
5858

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

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

+1
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)