Skip to content

Commit 28ac57d

Browse files
committed
Fix HTTP method choice for index requests (#3221)
This commit fixes the selection of HTTP method for an index request based on the presence of an explicilty set Id, or an Id value that can be inferred from the document. In the previous implementation, the IUrlParameter.GetString() method on the Id type evaluated the Document to infer an Id from it, setting the internal Value of the Id to this inferred value. As part of refactoring, this assignment was removed, which changes the behaviour of the client when determining the HTTP method for an index request; in this scenario, it is not enough to look to see if the Id type has a value, or that the Document is not null, we need to know at this point that an Id can be inferred from the Document, and can thereby send a PUT request. An Id is inferred from the Document before the HTTP method is determined as part of resolving RouteValues, so this commit changes the implementation to check if Id has a value, or RouteValues.Id has a value. (cherry picked from commit 948cbb1)
1 parent 4aff53f commit 28ac57d

File tree

3 files changed

+7
-15
lines changed

3 files changed

+7
-15
lines changed

src/Nest/CommonAbstractions/Infer/Id/Id.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,10 @@ public class Id : IEquatable<Id>, IUrlParameter
3030

3131
string IUrlParameter.GetString(IConnectionConfigurationValues settings)
3232
{
33-
var nestSettings = settings as IConnectionSettingsValues;
34-
return GetString(nestSettings);
33+
var nestSettings = (IConnectionSettingsValues)settings;
34+
return nestSettings.Inferrer.Id(this.Document) ?? this.StringOrLongValue;
3535
}
3636

37-
private string GetString(IConnectionSettingsValues nestSettings) =>
38-
this.Document != null ? nestSettings.Inferrer.Id(this.Document) : this.StringOrLongValue;
39-
4037
public bool Equals(Id other)
4138
{
4239
if (this.Tag + other.Tag == 1)

src/Nest/Document/Single/Index/IndexRequest.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ public partial class IndexRequest<TDocument>
1919

2020
protected override HttpMethod HttpMethod => GetHttpMethod(this);
2121

22-
internal static HttpMethod GetHttpMethod(IIndexRequest<TDocument> request) => request.Id.IsConditionless() ? HttpMethod.POST : HttpMethod.PUT;
22+
internal static HttpMethod GetHttpMethod(IIndexRequest<TDocument> request) =>
23+
request.Id?.StringOrLongValue != null || request.RouteValues.Id != null ? HttpMethod.PUT: HttpMethod.POST;
2324

2425
partial void DocumentFromPath(TDocument document) => this.Document = document;
2526

2627
private TDocument AutoRouteDocument() => Self.Document;
2728

2829
void IProxyRequest.WriteJson(IElasticsearchSerializer sourceSerializer, Stream stream, SerializationFormatting formatting) =>
2930
sourceSerializer.Serialize(this.Document, stream, formatting);
30-
3131
}
3232

3333
public partial class IndexDescriptor<TDocument> where TDocument : class
@@ -41,7 +41,5 @@ public partial class IndexDescriptor<TDocument> where TDocument : class
4141

4242
void IProxyRequest.WriteJson(IElasticsearchSerializer sourceSerializer, Stream stream, SerializationFormatting formatting) =>
4343
sourceSerializer.Serialize(Self.Document, stream, formatting);
44-
4544
}
46-
4745
}

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,11 @@ await POST("/project/doc?routing=NEST")
2323
Document = project
2424
}));
2525

26-
//no explit ID is provided and none can be inferred on the anonymous object so this falls back to a PUT to /index/type
27-
await PUT("/project/doc")
28-
.Fluent(c => c.Index(new { }, i => i.Index(typeof(Project)).Type(typeof(Project))))
29-
.FluentAsync(c => c.IndexAsync(new { }, i => i.Index(typeof(Project)).Type(typeof(Project))));
30-
31-
//no explit ID is provided and document is not fed into DocumentPath using explicit OIS.
26+
//no explicit ID is provided and none can be inferred on the anonymous object so this falls back to a POST to /index/type
3227
await POST("/project/doc")
28+
.Fluent(c => c.Index(new { }, i => i.Index(typeof(Project)).Type(typeof(Project))))
3329
.Request(c => c.Index(new IndexRequest<object>("project", "doc") {Document = new { }}))
30+
.FluentAsync(c => c.IndexAsync(new { }, i => i.Index(typeof(Project)).Type(typeof(Project))))
3431
.RequestAsync(c => c.IndexAsync(new IndexRequest<object>(typeof(Project), TypeName.From<Project>())
3532
{
3633
Document = new { }

0 commit comments

Comments
 (0)