Skip to content

Commit

Permalink
Fix #4289 low level client index http method generation wrong (#4290)
Browse files Browse the repository at this point in the history
* 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)
  • Loading branch information
Mpdreamz committed Dec 18, 2019
1 parent c37e034 commit 1fb218f
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ public IReadOnlyCollection<LowLevelClientMethod> LowLevelClientMethods
var methodName = CsharpNames.PerPathMethodName(path.Path);
var parts = new List<UrlPart>(path.Parts);
var mapsApiArgumentHints = parts.Select(p => p.Name).ToList();
// TODO This is hack until we stop transforming the new spec format into the old
if (Name == "index" && !mapsApiArgumentHints.Contains("id"))
httpMethod = "POST";
else if (Name == "index") httpMethod = PreferredHttpMethod;

if (Body != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public class GetAutoFollowPatternRequestParameters : RequestParameters<GetAutoFo
public class PauseAutoFollowPatternRequestParameters : RequestParameters<PauseAutoFollowPatternRequestParameters>
{
public override HttpMethod DefaultHttpMethod => HttpMethod.POST;
public override bool SupportsBody => false;
}

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

///<summary>Request options for ResumeFollowIndex <para>https://www.elastic.co/guide/en/elasticsearch/reference/current/ccr-post-resume-follow.html</para></summary>
Expand Down
8 changes: 4 additions & 4 deletions src/Elasticsearch.Net/ElasticLowLevelClient.NoNamespace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -516,19 +516,19 @@ public TResponse Index<TResponse>(string index, string id, PostData body, IndexR
[MapsApi("index", "index, id, body")]
public Task<TResponse> IndexAsync<TResponse>(string index, string id, PostData body, IndexRequestParameters requestParameters = null, CancellationToken ctx = default)
where TResponse : class, IElasticsearchResponse, new() => DoRequestAsync<TResponse>(PUT, Url($"{index:index}/_doc/{id:id}"), ctx, body, RequestParams(requestParameters));
///<summary>PUT on /{index}/_doc <para>https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-index_.html</para></summary>
///<summary>POST on /{index}/_doc <para>https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-index_.html</para></summary>
///<param name = "index">The name of the index</param>
///<param name = "body">The document</param>
///<param name = "requestParameters">Request specific configuration such as querystring parameters &amp; request specific connection settings.</param>
public TResponse Index<TResponse>(string index, PostData body, IndexRequestParameters requestParameters = null)
where TResponse : class, IElasticsearchResponse, new() => DoRequest<TResponse>(PUT, Url($"{index:index}/_doc"), body, RequestParams(requestParameters));
///<summary>PUT on /{index}/_doc <para>https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-index_.html</para></summary>
where TResponse : class, IElasticsearchResponse, new() => DoRequest<TResponse>(POST, Url($"{index:index}/_doc"), body, RequestParams(requestParameters));
///<summary>POST on /{index}/_doc <para>https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-index_.html</para></summary>
///<param name = "index">The name of the index</param>
///<param name = "body">The document</param>
///<param name = "requestParameters">Request specific configuration such as querystring parameters &amp; request specific connection settings.</param>
[MapsApi("index", "index, body")]
public Task<TResponse> IndexAsync<TResponse>(string index, PostData body, IndexRequestParameters requestParameters = null, CancellationToken ctx = default)
where TResponse : class, IElasticsearchResponse, new() => DoRequestAsync<TResponse>(PUT, Url($"{index:index}/_doc"), ctx, body, RequestParams(requestParameters));
where TResponse : class, IElasticsearchResponse, new() => DoRequestAsync<TResponse>(POST, Url($"{index:index}/_doc"), ctx, body, RequestParams(requestParameters));
///<summary>GET on / <para>https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html</para></summary>
///<param name = "requestParameters">Request specific configuration such as querystring parameters &amp; request specific connection settings.</param>
public TResponse RootNodeInfo<TResponse>(RootNodeInfoRequestParameters requestParameters = null)
Expand Down
4 changes: 2 additions & 2 deletions src/Elasticsearch.Net/IElasticLowLevelClient.Generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -417,13 +417,13 @@ TResponse Index<TResponse>(string index, string id, PostData body, IndexRequestP
///<param name = "requestParameters">Request specific configuration such as querystring parameters &amp; request specific connection settings.</param>
Task<TResponse> IndexAsync<TResponse>(string index, string id, PostData body, IndexRequestParameters requestParameters = null, CancellationToken ctx = default)
where TResponse : class, IElasticsearchResponse, new();
///<summary>PUT on /{index}/_doc <para>https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-index_.html</para></summary>
///<summary>POST on /{index}/_doc <para>https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-index_.html</para></summary>
///<param name = "index">The name of the index</param>
///<param name = "body">The document</param>
///<param name = "requestParameters">Request specific configuration such as querystring parameters &amp; request specific connection settings.</param>
TResponse Index<TResponse>(string index, PostData body, IndexRequestParameters requestParameters = null)
where TResponse : class, IElasticsearchResponse, new();
///<summary>PUT on /{index}/_doc <para>https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-index_.html</para></summary>
///<summary>POST on /{index}/_doc <para>https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-index_.html</para></summary>
///<param name = "index">The name of the index</param>
///<param name = "body">The document</param>
///<param name = "requestParameters">Request specific configuration such as querystring parameters &amp; request specific connection settings.</param>
Expand Down
15 changes: 15 additions & 0 deletions src/Tests/Tests/Document/Single/Index/IndexUrlTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Threading.Tasks;
using Elastic.Xunit.XunitPlumbing;
using Elasticsearch.Net;
using FluentAssertions;
using Nest;
using Tests.Domain;
Expand Down Expand Up @@ -42,6 +43,20 @@ await PUT("/project/_doc/NEST")
.RequestAsync(c => c.IndexAsync(new IndexRequest<Project>(project)));
}

[U] public async Task LowLevelUrls()
{
var project = new Project { Name = "NEST" };

await POST("/index/_doc")
.LowLevel(c => c.Index<VoidResponse>("index", PostData.Empty))
.LowLevelAsync(c => c.IndexAsync<VoidResponse>("index", PostData.Empty));

await PUT("/index/_doc/id")
.LowLevel(c => c.Index<VoidResponse>("index", "id", PostData.Empty))
.LowLevelAsync(c => c.IndexAsync<VoidResponse>("index", "id", PostData.Empty));

}

[U] public async Task CanIndexUrlIds()
{
var id = "http://my.local/id?qwe=2";
Expand Down
5 changes: 5 additions & 0 deletions src/Tests/Tests/Framework/EndpointTests/UrlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ public UrlTester LowLevel(Func<IElasticLowLevelClient, IApiCallDetails> call)
var callDetails = call(Client.LowLevel);
return Assert("lowlevel", callDetails);
}
public async Task<UrlTester> LowLevelAsync(Func<IElasticLowLevelClient, Task<VoidResponse>> call)
{
var callDetails = await call(Client.LowLevel);
return Assert("lowlevel async", callDetails);
}

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

0 comments on commit 1fb218f

Please sign in to comment.