Skip to content

Commit 243e623

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 c15779f commit 243e623

File tree

5 files changed

+36
-12
lines changed

5 files changed

+36
-12
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/ElasticLowLevelClient.NoNamespace.cs

+8-8
Original file line numberDiff line numberDiff line change
@@ -622,36 +622,36 @@ public TResponse Index<TResponse>(string index, string id, PostData body, IndexR
622622
[MapsApi("index", "index, id, body")]
623623
public Task<TResponse> IndexAsync<TResponse>(string index, string id, PostData body, IndexRequestParameters requestParameters = null, CancellationToken ctx = default)
624624
where TResponse : class, IElasticsearchResponse, new() => DoRequestAsync<TResponse>(PUT, Url($"{index:index}/_doc/{id:id}"), ctx, body, RequestParams(requestParameters));
625-
///<summary>PUT on /{index}/_doc <para>https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-index_.html</para></summary>
625+
///<summary>POST on /{index}/_doc <para>https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-index_.html</para></summary>
626626
///<param name = "index">The name of the index</param>
627627
///<param name = "body">The document</param>
628628
///<param name = "requestParameters">Request specific configuration such as querystring parameters &amp; request specific connection settings.</param>
629629
public TResponse Index<TResponse>(string index, PostData body, IndexRequestParameters requestParameters = null)
630-
where TResponse : class, IElasticsearchResponse, new() => DoRequest<TResponse>(PUT, Url($"{index:index}/_doc"), body, RequestParams(requestParameters));
631-
///<summary>PUT on /{index}/_doc <para>https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-index_.html</para></summary>
630+
where TResponse : class, IElasticsearchResponse, new() => DoRequest<TResponse>(POST, Url($"{index:index}/_doc"), body, RequestParams(requestParameters));
631+
///<summary>POST on /{index}/_doc <para>https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-index_.html</para></summary>
632632
///<param name = "index">The name of the index</param>
633633
///<param name = "body">The document</param>
634634
///<param name = "requestParameters">Request specific configuration such as querystring parameters &amp; request specific connection settings.</param>
635635
[MapsApi("index", "index, body")]
636636
public Task<TResponse> IndexAsync<TResponse>(string index, PostData body, IndexRequestParameters requestParameters = null, CancellationToken ctx = default)
637-
where TResponse : class, IElasticsearchResponse, new() => DoRequestAsync<TResponse>(PUT, Url($"{index:index}/_doc"), ctx, body, RequestParams(requestParameters));
638-
///<summary>PUT on /{index}/{type} <para>https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-index_.html</para></summary>
637+
where TResponse : class, IElasticsearchResponse, new() => DoRequestAsync<TResponse>(POST, Url($"{index:index}/_doc"), ctx, body, RequestParams(requestParameters));
638+
///<summary>POST on /{index}/{type} <para>https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-index_.html</para></summary>
639639
///<param name = "index">The name of the index</param>
640640
///<param name = "type">The type of the document</param>
641641
///<param name = "body">The document</param>
642642
///<param name = "requestParameters">Request specific configuration such as querystring parameters &amp; request specific connection settings.</param>
643643
[Obsolete("Deprecated in version 7.0.0: Specifying types in urls has been deprecated")]
644644
public TResponse IndexUsingType<TResponse>(string index, string type, PostData body, IndexRequestParameters requestParameters = null)
645-
where TResponse : class, IElasticsearchResponse, new() => DoRequest<TResponse>(PUT, Url($"{index:index}/{type:type}"), body, RequestParams(requestParameters));
646-
///<summary>PUT on /{index}/{type} <para>https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-index_.html</para></summary>
645+
where TResponse : class, IElasticsearchResponse, new() => DoRequest<TResponse>(POST, Url($"{index:index}/{type:type}"), body, RequestParams(requestParameters));
646+
///<summary>POST on /{index}/{type} <para>https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-index_.html</para></summary>
647647
///<param name = "index">The name of the index</param>
648648
///<param name = "type">The type of the document</param>
649649
///<param name = "body">The document</param>
650650
///<param name = "requestParameters">Request specific configuration such as querystring parameters &amp; request specific connection settings.</param>
651651
[Obsolete("Deprecated in version 7.0.0: Specifying types in urls has been deprecated")]
652652
[MapsApi("index", "index, type, body")]
653653
public Task<TResponse> IndexUsingTypeAsync<TResponse>(string index, string type, PostData body, IndexRequestParameters requestParameters = null, CancellationToken ctx = default)
654-
where TResponse : class, IElasticsearchResponse, new() => DoRequestAsync<TResponse>(PUT, Url($"{index:index}/{type:type}"), ctx, body, RequestParams(requestParameters));
654+
where TResponse : class, IElasticsearchResponse, new() => DoRequestAsync<TResponse>(POST, Url($"{index:index}/{type:type}"), ctx, body, RequestParams(requestParameters));
655655
///<summary>PUT on /{index}/{type}/{id} <para>https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-index_.html</para></summary>
656656
///<param name = "index">The name of the index</param>
657657
///<param name = "type">The type of the document</param>

src/Elasticsearch.Net/IElasticLowLevelClient.Generated.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -517,27 +517,27 @@ TResponse Index<TResponse>(string index, string id, PostData body, IndexRequestP
517517
///<param name = "requestParameters">Request specific configuration such as querystring parameters &amp; request specific connection settings.</param>
518518
Task<TResponse> IndexAsync<TResponse>(string index, string id, PostData body, IndexRequestParameters requestParameters = null, CancellationToken ctx = default)
519519
where TResponse : class, IElasticsearchResponse, new();
520-
///<summary>PUT on /{index}/_doc <para>https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-index_.html</para></summary>
520+
///<summary>POST on /{index}/_doc <para>https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-index_.html</para></summary>
521521
///<param name = "index">The name of the index</param>
522522
///<param name = "body">The document</param>
523523
///<param name = "requestParameters">Request specific configuration such as querystring parameters &amp; request specific connection settings.</param>
524524
TResponse Index<TResponse>(string index, PostData body, IndexRequestParameters requestParameters = null)
525525
where TResponse : class, IElasticsearchResponse, new();
526-
///<summary>PUT on /{index}/_doc <para>https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-index_.html</para></summary>
526+
///<summary>POST on /{index}/_doc <para>https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-index_.html</para></summary>
527527
///<param name = "index">The name of the index</param>
528528
///<param name = "body">The document</param>
529529
///<param name = "requestParameters">Request specific configuration such as querystring parameters &amp; request specific connection settings.</param>
530530
Task<TResponse> IndexAsync<TResponse>(string index, PostData body, IndexRequestParameters requestParameters = null, CancellationToken ctx = default)
531531
where TResponse : class, IElasticsearchResponse, new();
532-
///<summary>PUT on /{index}/{type} <para>https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-index_.html</para></summary>
532+
///<summary>POST on /{index}/{type} <para>https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-index_.html</para></summary>
533533
///<param name = "index">The name of the index</param>
534534
///<param name = "type">The type of the document</param>
535535
///<param name = "body">The document</param>
536536
///<param name = "requestParameters">Request specific configuration such as querystring parameters &amp; request specific connection settings.</param>
537537
[Obsolete("Deprecated in version 7.0.0: Specifying types in urls has been deprecated")]
538538
TResponse IndexUsingType<TResponse>(string index, string type, PostData body, IndexRequestParameters requestParameters = null)
539539
where TResponse : class, IElasticsearchResponse, new();
540-
///<summary>PUT on /{index}/{type} <para>https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-index_.html</para></summary>
540+
///<summary>POST on /{index}/{type} <para>https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-index_.html</para></summary>
541541
///<param name = "index">The name of the index</param>
542542
///<param name = "type">The type of the document</param>
543543
///<param name = "body">The document</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)