Skip to content

Commit 1fb218f

Browse files
committed
Fix #4289 low level client index http method generation wrong (#4290)
* Fix #4289 low level client index http method generation wrong We used to always send POST which is not right either but the server accepted it. Now that we have a new format and transform to the old format in the interim we only picked up PUT for all which is not correct for index operations without an id. * add unit tests (cherry picked from commit 02d4076)
1 parent c37e034 commit 1fb218f

File tree

6 files changed

+32
-6
lines changed

6 files changed

+32
-6
lines changed

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

+4
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ public IReadOnlyCollection<LowLevelClientMethod> LowLevelClientMethods
133133
var methodName = CsharpNames.PerPathMethodName(path.Path);
134134
var parts = new List<UrlPart>(path.Parts);
135135
var mapsApiArgumentHints = parts.Select(p => p.Name).ToList();
136+
// TODO This is hack until we stop transforming the new spec format into the old
137+
if (Name == "index" && !mapsApiArgumentHints.Contains("id"))
138+
httpMethod = "POST";
139+
else if (Name == "index") httpMethod = PreferredHttpMethod;
136140

137141
if (Body != null)
138142
{

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

+2
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public class GetAutoFollowPatternRequestParameters : RequestParameters<GetAutoFo
7979
public class PauseAutoFollowPatternRequestParameters : RequestParameters<PauseAutoFollowPatternRequestParameters>
8080
{
8181
public override HttpMethod DefaultHttpMethod => HttpMethod.POST;
82+
public override bool SupportsBody => false;
8283
}
8384

8485
///<summary>Request options for PauseFollowIndex <para>https://www.elastic.co/guide/en/elasticsearch/reference/current/ccr-post-pause-follow.html</para></summary>
@@ -99,6 +100,7 @@ public class CreateAutoFollowPatternRequestParameters : RequestParameters<Create
99100
public class ResumeAutoFollowPatternRequestParameters : RequestParameters<ResumeAutoFollowPatternRequestParameters>
100101
{
101102
public override HttpMethod DefaultHttpMethod => HttpMethod.POST;
103+
public override bool SupportsBody => false;
102104
}
103105

104106
///<summary>Request options for ResumeFollowIndex <para>https://www.elastic.co/guide/en/elasticsearch/reference/current/ccr-post-resume-follow.html</para></summary>

src/Elasticsearch.Net/ElasticLowLevelClient.NoNamespace.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -516,19 +516,19 @@ public TResponse Index<TResponse>(string index, string id, PostData body, IndexR
516516
[MapsApi("index", "index, id, body")]
517517
public Task<TResponse> IndexAsync<TResponse>(string index, string id, PostData body, IndexRequestParameters requestParameters = null, CancellationToken ctx = default)
518518
where TResponse : class, IElasticsearchResponse, new() => DoRequestAsync<TResponse>(PUT, Url($"{index:index}/_doc/{id:id}"), ctx, body, RequestParams(requestParameters));
519-
///<summary>PUT on /{index}/_doc <para>https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-index_.html</para></summary>
519+
///<summary>POST on /{index}/_doc <para>https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-index_.html</para></summary>
520520
///<param name = "index">The name of the index</param>
521521
///<param name = "body">The document</param>
522522
///<param name = "requestParameters">Request specific configuration such as querystring parameters &amp; request specific connection settings.</param>
523523
public TResponse Index<TResponse>(string index, PostData body, IndexRequestParameters requestParameters = null)
524-
where TResponse : class, IElasticsearchResponse, new() => DoRequest<TResponse>(PUT, Url($"{index:index}/_doc"), body, RequestParams(requestParameters));
525-
///<summary>PUT on /{index}/_doc <para>https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-index_.html</para></summary>
524+
where TResponse : class, IElasticsearchResponse, new() => DoRequest<TResponse>(POST, Url($"{index:index}/_doc"), body, RequestParams(requestParameters));
525+
///<summary>POST on /{index}/_doc <para>https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-index_.html</para></summary>
526526
///<param name = "index">The name of the index</param>
527527
///<param name = "body">The document</param>
528528
///<param name = "requestParameters">Request specific configuration such as querystring parameters &amp; request specific connection settings.</param>
529529
[MapsApi("index", "index, body")]
530530
public Task<TResponse> IndexAsync<TResponse>(string index, PostData body, IndexRequestParameters requestParameters = null, CancellationToken ctx = default)
531-
where TResponse : class, IElasticsearchResponse, new() => DoRequestAsync<TResponse>(PUT, Url($"{index:index}/_doc"), ctx, body, RequestParams(requestParameters));
531+
where TResponse : class, IElasticsearchResponse, new() => DoRequestAsync<TResponse>(POST, Url($"{index:index}/_doc"), ctx, body, RequestParams(requestParameters));
532532
///<summary>GET on / <para>https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html</para></summary>
533533
///<param name = "requestParameters">Request specific configuration such as querystring parameters &amp; request specific connection settings.</param>
534534
public TResponse RootNodeInfo<TResponse>(RootNodeInfoRequestParameters requestParameters = null)

src/Elasticsearch.Net/IElasticLowLevelClient.Generated.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -417,13 +417,13 @@ TResponse Index<TResponse>(string index, string id, PostData body, IndexRequestP
417417
///<param name = "requestParameters">Request specific configuration such as querystring parameters &amp; request specific connection settings.</param>
418418
Task<TResponse> IndexAsync<TResponse>(string index, string id, PostData body, IndexRequestParameters requestParameters = null, CancellationToken ctx = default)
419419
where TResponse : class, IElasticsearchResponse, new();
420-
///<summary>PUT on /{index}/_doc <para>https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-index_.html</para></summary>
420+
///<summary>POST on /{index}/_doc <para>https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-index_.html</para></summary>
421421
///<param name = "index">The name of the index</param>
422422
///<param name = "body">The document</param>
423423
///<param name = "requestParameters">Request specific configuration such as querystring parameters &amp; request specific connection settings.</param>
424424
TResponse Index<TResponse>(string index, PostData body, IndexRequestParameters requestParameters = null)
425425
where TResponse : class, IElasticsearchResponse, new();
426-
///<summary>PUT on /{index}/_doc <para>https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-index_.html</para></summary>
426+
///<summary>POST on /{index}/_doc <para>https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-index_.html</para></summary>
427427
///<param name = "index">The name of the index</param>
428428
///<param name = "body">The document</param>
429429
///<param name = "requestParameters">Request specific configuration such as querystring parameters &amp; request specific connection settings.</param>

src/Tests/Tests/Document/Single/Index/IndexUrlTests.cs

+15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Threading.Tasks;
33
using Elastic.Xunit.XunitPlumbing;
4+
using Elasticsearch.Net;
45
using FluentAssertions;
56
using Nest;
67
using Tests.Domain;
@@ -42,6 +43,20 @@ await PUT("/project/_doc/NEST")
4243
.RequestAsync(c => c.IndexAsync(new IndexRequest<Project>(project)));
4344
}
4445

46+
[U] public async Task LowLevelUrls()
47+
{
48+
var project = new Project { Name = "NEST" };
49+
50+
await POST("/index/_doc")
51+
.LowLevel(c => c.Index<VoidResponse>("index", PostData.Empty))
52+
.LowLevelAsync(c => c.IndexAsync<VoidResponse>("index", PostData.Empty));
53+
54+
await PUT("/index/_doc/id")
55+
.LowLevel(c => c.Index<VoidResponse>("index", "id", PostData.Empty))
56+
.LowLevelAsync(c => c.IndexAsync<VoidResponse>("index", "id", PostData.Empty));
57+
58+
}
59+
4560
[U] public async Task CanIndexUrlIds()
4661
{
4762
var id = "http://my.local/id?qwe=2";

src/Tests/Tests/Framework/EndpointTests/UrlTests.cs

+5
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ public UrlTester LowLevel(Func<IElasticLowLevelClient, IApiCallDetails> call)
7171
var callDetails = call(Client.LowLevel);
7272
return Assert("lowlevel", callDetails);
7373
}
74+
public async Task<UrlTester> LowLevelAsync(Func<IElasticLowLevelClient, Task<VoidResponse>> call)
75+
{
76+
var callDetails = await call(Client.LowLevel);
77+
return Assert("lowlevel async", callDetails);
78+
}
7479

7580
private UrlTester WhenCalling<TResponse>(Func<IElasticClient, TResponse> call, string typeOfCall)
7681
where TResponse : IResponse

0 commit comments

Comments
 (0)