Skip to content

Commit 8ad3032

Browse files
committed
Initialize Fields if null when adding attachment metadata field
See #1826 Fix issue where FileField was mapped as "file" and not "content" Fix issue where Language fluent method accepted a NumberPropertyDescriptor<T> instead of StringPropertyDescriptor<T> Fix issue where ContentLength fluent method accepted a StringPropertyDescriptor<T> instead of NumberPropertyDescriptor<T> Both of these fluent method changes overload the fluent method and mean that the lambda expression must be typed
1 parent 931828f commit 8ad3032

File tree

2 files changed

+140
-6
lines changed

2 files changed

+140
-6
lines changed

Diff for: src/Nest/Mapping/Types/Specialized/Attachment/AttachmentProperty.cs

+14-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using System;
2-
using System.Collections;
2+
using System.Collections.Generic;
33
using Newtonsoft.Json;
44

55
namespace Nest
@@ -22,7 +22,7 @@ public class AttachmentProperty : PropertyBase, IAttachmentProperty
2222
{
2323
public AttachmentProperty() : base("attachment") { }
2424

25-
private IDictionary Dictionary => this.Fields;
25+
private IDictionary<PropertyName, IProperty> Dictionary => this.Fields ?? (this.Fields = new Properties());
2626

2727
public IStringProperty AuthorField
2828
{
@@ -50,8 +50,8 @@ public IDateProperty DateField
5050

5151
public IStringProperty FileField
5252
{
53-
get { return Dictionary["file"] as IStringProperty; }
54-
set { Dictionary["file"] = value; }
53+
get { return Dictionary["content"] as IStringProperty; }
54+
set { Dictionary["content"] = value; }
5555
}
5656

5757
public IStringProperty KeywordsField
@@ -93,7 +93,7 @@ public class AttachmentPropertyDescriptor<T>
9393
INumberProperty IAttachmentProperty.ContentLengthField { get; set; }
9494
IStringProperty IAttachmentProperty.LanguageField { get; set; }
9595

96-
private IDictionary Dictionary => Self.Fields;
96+
private IDictionary<PropertyName, IProperty> Dictionary => Self.Fields ?? (Self.Fields = new Properties());
9797

9898
public AttachmentPropertyDescriptor() : base("attachment") { }
9999

@@ -117,7 +117,7 @@ public AttachmentPropertyDescriptor<T> NameField(Func<StringPropertyDescriptor<T
117117
SetMetadataField(selector, "name");
118118

119119
public AttachmentPropertyDescriptor<T> FileField(Func<StringPropertyDescriptor<T>, IStringProperty> selector) =>
120-
SetMetadataField(selector, "file");
120+
SetMetadataField(selector, "content");
121121

122122
public AttachmentPropertyDescriptor<T> AuthorField(Func<StringPropertyDescriptor<T>, IStringProperty> selector) =>
123123
SetMetadataField(selector, "author");
@@ -128,10 +128,18 @@ public AttachmentPropertyDescriptor<T> KeywordsField(Func<StringPropertyDescript
128128
public AttachmentPropertyDescriptor<T> ContentTypeField(Func<StringPropertyDescriptor<T>, IStringProperty> selector) =>
129129
SetMetadataField(selector, "content_type");
130130

131+
[Obsolete("Use ContentLengthField(Func<NumberPropertyDescriptor<T>, INumberProperty> selector)")]
131132
public AttachmentPropertyDescriptor<T> ContentLengthField(Func<StringPropertyDescriptor<T>, IStringProperty> selector) =>
132133
SetMetadataField(selector, "content_length");
133134

135+
public AttachmentPropertyDescriptor<T> ContentLengthField(Func<NumberPropertyDescriptor<T>, INumberProperty> selector) =>
136+
SetMetadataField(selector, "content_length");
137+
138+
[Obsolete("Use LanguageField(Func<StringPropertyDescriptor<T>, IStringProperty> selector)")]
134139
public AttachmentPropertyDescriptor<T> LanguageField(Func<NumberPropertyDescriptor<T>, INumberProperty> selector) =>
135140
SetMetadataField(selector, "language");
141+
142+
public AttachmentPropertyDescriptor<T> LanguageField(Func<StringPropertyDescriptor<T>, IStringProperty> selector) =>
143+
SetMetadataField(selector, "language");
136144
}
137145
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Nest;
6+
using Xunit;
7+
8+
namespace Tests.Mapping.Types.Specialized.Attachment
9+
{
10+
public class AttachmentTest
11+
{
12+
public string File { get; set; }
13+
14+
public string Author { get; set; }
15+
16+
public long ContentLength { get; set; }
17+
18+
public string ContentType { get; set; }
19+
20+
public DateTime Date { get; set; }
21+
22+
public string Keywords { get; set; }
23+
24+
public string Language { get; set; }
25+
26+
public string Name { get; set; }
27+
28+
public string Title { get; set; }
29+
}
30+
31+
public class AttachmentMappingTests : TypeMappingTestBase<AttachmentTest>
32+
{
33+
protected override object ExpectJson => new
34+
{
35+
properties = new
36+
{
37+
file = new
38+
{
39+
type = "attachment",
40+
fields = new
41+
{
42+
author = new
43+
{
44+
type = "string"
45+
},
46+
content = new
47+
{
48+
type = "string"
49+
},
50+
content_length = new
51+
{
52+
type = "double"
53+
},
54+
content_type = new
55+
{
56+
type = "string"
57+
},
58+
date = new
59+
{
60+
type = "date"
61+
},
62+
keywords = new
63+
{
64+
type = "string"
65+
},
66+
language = new
67+
{
68+
type = "string",
69+
doc_values = true,
70+
index = "not_analyzed"
71+
},
72+
name = new
73+
{
74+
type = "string"
75+
},
76+
title = new
77+
{
78+
type = "string"
79+
}
80+
}
81+
}
82+
}
83+
};
84+
85+
protected override void AttributeBasedSerializes()
86+
{
87+
// TODO: Implement
88+
}
89+
90+
protected override Func<PropertiesDescriptor<AttachmentTest>, IPromise<IProperties>> FluentProperties => p => p
91+
.Attachment(a => a
92+
//.Fields(s => s)
93+
.Name(n => n.File)
94+
.AuthorField(d => d
95+
.Name(n => n.Author)
96+
)
97+
.FileField(d => d
98+
.Name(n => n.File)
99+
)
100+
.ContentLengthField((NumberPropertyDescriptor<AttachmentTest> d) => d
101+
.Name(n => n.ContentLength)
102+
)
103+
.ContentTypeField(d => d
104+
.Name(n => n.ContentType)
105+
)
106+
.DateField(d => d
107+
.Name(n => n.Date)
108+
)
109+
.KeywordsField(d => d
110+
.Name(n => n.Keywords)
111+
)
112+
.LanguageField((StringPropertyDescriptor<AttachmentTest> d) => d
113+
.Name(n => n.Language)
114+
.DocValues()
115+
.NotAnalyzed()
116+
)
117+
.NameField(d => d
118+
.Name(n => n.Name)
119+
)
120+
.TitleField(d => d
121+
.Name(n => n.Title)
122+
)
123+
);
124+
}
125+
}
126+

0 commit comments

Comments
 (0)