Skip to content

Commit 147d5b4

Browse files
committed
Fix update request not properly inferring doc id
Update was only inferring the doc id from Upsert and not Doc as well. Additionally, a NullArgumentException was being thrown when specifying the id property via the ElasticType attribute due to the order in which route parameters were being validated (checking id before UpdatePathInfo() was being called). Closes #806
1 parent 37b9546 commit 147d5b4

File tree

5 files changed

+69
-18
lines changed

5 files changed

+69
-18
lines changed

Diff for: src/Nest/DSL/Common/BaseRequest.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ TParameters queryString
4242
}
4343

4444
SetRouteParameters(settings, pathInfo);
45-
4645
UpdatePathInfo(settings, pathInfo);
46+
ValidatePathInfo(pathInfo);
47+
4748
return pathInfo;
4849
}
4950

@@ -54,6 +55,10 @@ protected virtual void SetRouteParameters(
5455

5556
}
5657

58+
protected virtual void ValidatePathInfo(ElasticsearchPathInfo<TParameters> pathInfo)
59+
{
60+
}
61+
5762
protected abstract void UpdatePathInfo(IConnectionSettingsValues settings, ElasticsearchPathInfo<TParameters> pathInfo);
5863

5964
ElasticsearchPathInfo<TParameters> IPathInfo<TParameters>.ToPathInfo(IConnectionSettingsValues settings)

Diff for: src/Nest/DSL/Paths/DocumentPathDescriptor.cs

+13-9
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ protected DocumentPathBase(IndexNameMarker indexName, TypeNameMarker typeName, s
1313
}
1414
}
1515

16-
1716
public abstract class DocumentPathBase<TParameters, T> : DocumentOptionalPathBase<TParameters, T>
1817
where TParameters : FluentRequestParameters<TParameters>, new()
1918
where T : class
@@ -24,12 +23,9 @@ protected DocumentPathBase(long id) : base(id) { }
2423

2524
protected DocumentPathBase(T document) : base(document) { }
2625

27-
protected override void SetRouteParameters(IConnectionSettingsValues settings, ElasticsearchPathInfo<TParameters> pathInfo)
26+
protected override void ValidatePathInfo(ElasticsearchPathInfo<TParameters> pathInfo)
2827
{
29-
base.SetRouteParameters(settings, pathInfo);
30-
pathInfo.Index.ThrowIfNullOrEmpty("index");
31-
pathInfo.Type.ThrowIfNullOrEmpty("type");
32-
pathInfo.Id.ThrowIfNullOrEmpty("id");
28+
DocumentPathValidation.Validate(pathInfo);
3329
}
3430
}
3531

@@ -45,13 +41,21 @@ public abstract class DocumentPathDescriptor<TDescriptor, TParameters, T> : Docu
4541
where TParameters : FluentRequestParameters<TParameters>, new()
4642
where T : class
4743
{
48-
protected override void SetRouteParameters(IConnectionSettingsValues settings, ElasticsearchPathInfo<TParameters> pathInfo)
44+
protected override void ValidatePathInfo(ElasticsearchPathInfo<TParameters> pathInfo)
45+
{
46+
DocumentPathValidation.Validate(pathInfo);
47+
}
48+
}
49+
50+
internal static class DocumentPathValidation
51+
{
52+
public static void Validate<TParameters>(ElasticsearchPathInfo<TParameters> pathInfo)
53+
where TParameters : FluentRequestParameters<TParameters>, new()
4954
{
50-
base.SetRouteParameters(settings, pathInfo);
5155
pathInfo.Index.ThrowIfNullOrEmpty("index");
5256
pathInfo.Type.ThrowIfNullOrEmpty("type");
5357
pathInfo.Id.ThrowIfNullOrEmpty("id");
5458
}
55-
5659
}
60+
5761
}

Diff for: src/Nest/DSL/UpdateDescriptor.cs

+23-8
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,27 @@ public interface IUpdateRequest<TDocument, TPartialDocument> : IDocumentOptional
3232
TPartialDocument Doc { get; set; }
3333
}
3434

35+
internal static class UpdateRequestPathInfo
36+
{
37+
public static void Update<TDocument, TPartialDocument>(
38+
IConnectionSettingsValues settings,
39+
ElasticsearchPathInfo<UpdateRequestParameters> pathInfo,
40+
IUpdateRequest<TDocument, TPartialDocument> self)
41+
where TDocument : class
42+
where TPartialDocument : class
43+
{
44+
if (pathInfo.Id.IsNullOrEmpty())
45+
{
46+
if (self.Doc != null)
47+
pathInfo.Id = settings.Inferrer.Id(self.Doc);
48+
else if (self.Upsert != null)
49+
pathInfo.Id = settings.Inferrer.Id(self.Upsert);
50+
}
51+
52+
pathInfo.HttpMethod = PathInfoHttpMethod.POST;
53+
}
54+
}
55+
3556
public class UpdateRequest<TDocument> : UpdateRequest<TDocument, TDocument>
3657
where TDocument : class
3758
{
@@ -61,7 +82,7 @@ public UpdateRequest(TDocument idFrom, bool useAsUpsert = false) : base(idFrom)
6182

6283
protected override void UpdatePathInfo(IConnectionSettingsValues settings, ElasticsearchPathInfo<UpdateRequestParameters> pathInfo)
6384
{
64-
pathInfo.HttpMethod = PathInfoHttpMethod.POST;
85+
UpdateRequestPathInfo.Update(settings, pathInfo, this);
6586
}
6687

6788

@@ -73,7 +94,6 @@ protected override void UpdatePathInfo(IConnectionSettingsValues settings, Elast
7394
public TPartialDocument Doc { get; set; }
7495
}
7596

76-
7797
public partial class UpdateDescriptor<TDocument,TPartialDocument>
7898
: DocumentPathDescriptor<UpdateDescriptor<TDocument, TPartialDocument>, UpdateRequestParameters, TDocument>
7999
, IUpdateRequest<TDocument, TPartialDocument>
@@ -164,12 +184,7 @@ public UpdateDescriptor<TDocument,TPartialDocument> Fields(params Expression<Fun
164184

165185
protected override void UpdatePathInfo(IConnectionSettingsValues settings, ElasticsearchPathInfo<UpdateRequestParameters> pathInfo)
166186
{
167-
if (pathInfo.Id.IsNullOrEmpty())
168-
{
169-
pathInfo.Id = settings.Inferrer.Id(Self.Upsert);
170-
}
171-
172-
pathInfo.HttpMethod = PathInfoHttpMethod.POST;
187+
UpdateRequestPathInfo.Update(settings, pathInfo, this);
173188
}
174189
}
175190
}

Diff for: src/Tests/Nest.Tests.Unit/Nest.Tests.Unit.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@
363363
<Compile Include="QueryParsers\Visitor\VisitorTests.cs" />
364364
<Compile Include="Reproduce\Reproduce646Tests.cs" />
365365
<Compile Include="Reproduce\Reproduce579Tests.cs" />
366+
<Compile Include="Reproduce\Reproduce806Tests.cs" />
366367
<Compile Include="Search\Fields\FieldsTests.cs" />
367368
<Compile Include="Search\Filter\Modes\ConditionlessFilterJson.cs" />
368369
<Compile Include="Search\Filter\Modes\FilterModesTests.cs" />
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using NUnit.Framework;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace Nest.Tests.Unit.Reproduce
9+
{
10+
[ElasticType(Name="mypoco", IdProperty="Code")]
11+
public class MyPoco
12+
{
13+
[ElasticProperty(Name="code")]
14+
public string Code { get; set; }
15+
}
16+
17+
[TestFixture]
18+
public class Reproduce806Tests : BaseJsonTests
19+
{
20+
[Test]
21+
public void IdPropertyAttributeCausesArgumentNullException()
22+
{
23+
var r = _client.Update<MyPoco>(d => d.DocAsUpsert().Doc(new MyPoco { Code = "1" }));
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)