From 1fb218fc5fed94c0a18eea2b94d597a03455e42a Mon Sep 17 00:00:00 2001 From: Martijn Laarman Date: Wed, 18 Dec 2019 18:15:33 +0100 Subject: [PATCH] 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 02d40766cf4fcdd660ca1af0ae818bb8efb57bc3) --- .../Domain/Specification/ApiEndpoint.cs | 4 ++++ .../RequestParameters.CrossClusterReplication.cs | 2 ++ .../ElasticLowLevelClient.NoNamespace.cs | 8 ++++---- .../IElasticLowLevelClient.Generated.cs | 4 ++-- .../Tests/Document/Single/Index/IndexUrlTests.cs | 15 +++++++++++++++ .../Tests/Framework/EndpointTests/UrlTests.cs | 5 +++++ 6 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/CodeGeneration/ApiGenerator/Domain/Specification/ApiEndpoint.cs b/src/CodeGeneration/ApiGenerator/Domain/Specification/ApiEndpoint.cs index 6c2d761230f..d12ab9feba5 100644 --- a/src/CodeGeneration/ApiGenerator/Domain/Specification/ApiEndpoint.cs +++ b/src/CodeGeneration/ApiGenerator/Domain/Specification/ApiEndpoint.cs @@ -133,6 +133,10 @@ public IReadOnlyCollection LowLevelClientMethods var methodName = CsharpNames.PerPathMethodName(path.Path); var parts = new List(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) { diff --git a/src/Elasticsearch.Net/Api/RequestParameters/RequestParameters.CrossClusterReplication.cs b/src/Elasticsearch.Net/Api/RequestParameters/RequestParameters.CrossClusterReplication.cs index 23f2a90e61b..03731ff82fc 100644 --- a/src/Elasticsearch.Net/Api/RequestParameters/RequestParameters.CrossClusterReplication.cs +++ b/src/Elasticsearch.Net/Api/RequestParameters/RequestParameters.CrossClusterReplication.cs @@ -79,6 +79,7 @@ public class GetAutoFollowPatternRequestParameters : RequestParameters { public override HttpMethod DefaultHttpMethod => HttpMethod.POST; + public override bool SupportsBody => false; } ///Request options for PauseFollowIndex https://www.elastic.co/guide/en/elasticsearch/reference/current/ccr-post-pause-follow.html @@ -99,6 +100,7 @@ public class CreateAutoFollowPatternRequestParameters : RequestParameters { public override HttpMethod DefaultHttpMethod => HttpMethod.POST; + public override bool SupportsBody => false; } ///Request options for ResumeFollowIndex https://www.elastic.co/guide/en/elasticsearch/reference/current/ccr-post-resume-follow.html diff --git a/src/Elasticsearch.Net/ElasticLowLevelClient.NoNamespace.cs b/src/Elasticsearch.Net/ElasticLowLevelClient.NoNamespace.cs index 679872c09fe..a734e0a6fe2 100644 --- a/src/Elasticsearch.Net/ElasticLowLevelClient.NoNamespace.cs +++ b/src/Elasticsearch.Net/ElasticLowLevelClient.NoNamespace.cs @@ -516,19 +516,19 @@ public TResponse Index(string index, string id, PostData body, IndexR [MapsApi("index", "index, id, body")] public Task IndexAsync(string index, string id, PostData body, IndexRequestParameters requestParameters = null, CancellationToken ctx = default) where TResponse : class, IElasticsearchResponse, new() => DoRequestAsync(PUT, Url($"{index:index}/_doc/{id:id}"), ctx, body, RequestParams(requestParameters)); - ///PUT on /{index}/_doc https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-index_.html + ///POST on /{index}/_doc https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-index_.html ///The name of the index ///The document ///Request specific configuration such as querystring parameters & request specific connection settings. public TResponse Index(string index, PostData body, IndexRequestParameters requestParameters = null) - where TResponse : class, IElasticsearchResponse, new() => DoRequest(PUT, Url($"{index:index}/_doc"), body, RequestParams(requestParameters)); - ///PUT on /{index}/_doc https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-index_.html + where TResponse : class, IElasticsearchResponse, new() => DoRequest(POST, Url($"{index:index}/_doc"), body, RequestParams(requestParameters)); + ///POST on /{index}/_doc https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-index_.html ///The name of the index ///The document ///Request specific configuration such as querystring parameters & request specific connection settings. [MapsApi("index", "index, body")] public Task IndexAsync(string index, PostData body, IndexRequestParameters requestParameters = null, CancellationToken ctx = default) - where TResponse : class, IElasticsearchResponse, new() => DoRequestAsync(PUT, Url($"{index:index}/_doc"), ctx, body, RequestParams(requestParameters)); + where TResponse : class, IElasticsearchResponse, new() => DoRequestAsync(POST, Url($"{index:index}/_doc"), ctx, body, RequestParams(requestParameters)); ///GET on / https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html ///Request specific configuration such as querystring parameters & request specific connection settings. public TResponse RootNodeInfo(RootNodeInfoRequestParameters requestParameters = null) diff --git a/src/Elasticsearch.Net/IElasticLowLevelClient.Generated.cs b/src/Elasticsearch.Net/IElasticLowLevelClient.Generated.cs index 9c04ae6cf6a..c37c566daf3 100644 --- a/src/Elasticsearch.Net/IElasticLowLevelClient.Generated.cs +++ b/src/Elasticsearch.Net/IElasticLowLevelClient.Generated.cs @@ -417,13 +417,13 @@ TResponse Index(string index, string id, PostData body, IndexRequestP ///Request specific configuration such as querystring parameters & request specific connection settings. Task IndexAsync(string index, string id, PostData body, IndexRequestParameters requestParameters = null, CancellationToken ctx = default) where TResponse : class, IElasticsearchResponse, new(); - ///PUT on /{index}/_doc https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-index_.html + ///POST on /{index}/_doc https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-index_.html ///The name of the index ///The document ///Request specific configuration such as querystring parameters & request specific connection settings. TResponse Index(string index, PostData body, IndexRequestParameters requestParameters = null) where TResponse : class, IElasticsearchResponse, new(); - ///PUT on /{index}/_doc https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-index_.html + ///POST on /{index}/_doc https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-index_.html ///The name of the index ///The document ///Request specific configuration such as querystring parameters & request specific connection settings. diff --git a/src/Tests/Tests/Document/Single/Index/IndexUrlTests.cs b/src/Tests/Tests/Document/Single/Index/IndexUrlTests.cs index 4b4e67e9899..5447a59e3d1 100644 --- a/src/Tests/Tests/Document/Single/Index/IndexUrlTests.cs +++ b/src/Tests/Tests/Document/Single/Index/IndexUrlTests.cs @@ -1,6 +1,7 @@ using System; using System.Threading.Tasks; using Elastic.Xunit.XunitPlumbing; +using Elasticsearch.Net; using FluentAssertions; using Nest; using Tests.Domain; @@ -42,6 +43,20 @@ await PUT("/project/_doc/NEST") .RequestAsync(c => c.IndexAsync(new IndexRequest(project))); } + [U] public async Task LowLevelUrls() + { + var project = new Project { Name = "NEST" }; + + await POST("/index/_doc") + .LowLevel(c => c.Index("index", PostData.Empty)) + .LowLevelAsync(c => c.IndexAsync("index", PostData.Empty)); + + await PUT("/index/_doc/id") + .LowLevel(c => c.Index("index", "id", PostData.Empty)) + .LowLevelAsync(c => c.IndexAsync("index", "id", PostData.Empty)); + + } + [U] public async Task CanIndexUrlIds() { var id = "http://my.local/id?qwe=2"; diff --git a/src/Tests/Tests/Framework/EndpointTests/UrlTests.cs b/src/Tests/Tests/Framework/EndpointTests/UrlTests.cs index d604309cdbe..6bf18ed192b 100644 --- a/src/Tests/Tests/Framework/EndpointTests/UrlTests.cs +++ b/src/Tests/Tests/Framework/EndpointTests/UrlTests.cs @@ -71,6 +71,11 @@ public UrlTester LowLevel(Func call) var callDetails = call(Client.LowLevel); return Assert("lowlevel", callDetails); } + public async Task LowLevelAsync(Func> call) + { + var callDetails = await call(Client.LowLevel); + return Assert("lowlevel async", callDetails); + } private UrlTester WhenCalling(Func call, string typeOfCall) where TResponse : IResponse