Skip to content

Commit 83e2dba

Browse files
Mpdreamzrusscam
authored andcommitted
fix #2817 allow {dynamic_type} to be set when doing a generic mapping… (#2946)
* fix #2817 allow {dynamic_type} to be set when doing a generic mapping, cleaned up the obsolete constructors for PropertyBase(Descriptor) in the process as well' * Make SingleMappingPropertyTestsBase an integration test
1 parent 8f07d14 commit 83e2dba

27 files changed

+213
-141
lines changed

src/Nest/CommonAbstractions/SerializationBehavior/ElasticContractResolver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ private static IEnumerable<Type> TypeWithInterfaces(Type objectType)
9595
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
9696
{
9797
// Only serialize explicitly implemented IProperty properties on attribute types
98-
if (typeof(ElasticsearchPropertyAttributeBase).IsAssignableFrom(type))
98+
if (typeof(PropertyBase).IsAssignableFrom(type) || typeof(ElasticsearchPropertyAttributeBase).IsAssignableFrom(type))
9999
return PropertiesOfInterface<IProperty>(type, memberSerialization);
100100

101101
// Descriptors implement properties explicitly, these are not picked up by default

src/Nest/Indices/MappingManagement/GetFieldMapping/FieldMappingJsonConverter.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
2222
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
2323
{
2424
var r = new Dictionary<string, IFieldMapping>();
25-
26-
JObject o = JObject.Load(reader);
25+
var o = JObject.Load(reader);
2726

2827
foreach (var p in o.Properties())
2928
{
@@ -46,8 +45,7 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
4645
}
4746
if (mapping == null) continue;
4847

49-
var esType = mapping as IProperty;
50-
if (esType != null)
48+
if (mapping is IProperty esType)
5149
esType.Name = name;
5250

5351
r.Add(name, mapping);

src/Nest/Mapping/AttributeBased/ElasticsearchCorePropertyAttributeBase.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ namespace Nest
1010
public abstract class ElasticsearchCorePropertyAttributeBase : ElasticsearchPropertyAttributeBase, ICoreProperty
1111
{
1212
protected ElasticsearchCorePropertyAttributeBase(FieldType type) : base(type) { }
13-
[Obsolete("Please use overload taking FieldType")]
14-
protected ElasticsearchCorePropertyAttributeBase(string typeName) : base(typeName) { }
15-
[Obsolete("Please use overload taking FieldType")]
16-
protected ElasticsearchCorePropertyAttributeBase(Type type) : base(type) { }
1713

1814
private ICoreProperty Self => this;
1915

src/Nest/Mapping/AttributeBased/ElasticsearchDocValuesPropertyAttributeBase.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,14 @@ public abstract class ElasticsearchDocValuesPropertyAttributeBase : Elasticsearc
1212

1313
bool? IDocValuesProperty.DocValues { get; set; }
1414

15-
public bool DocValues { get { return Self.DocValues.GetValueOrDefault(true); } set { Self.DocValues = value; } }
16-
15+
public bool DocValues
16+
{
17+
get => Self.DocValues.GetValueOrDefault(true);
18+
set => Self.DocValues = value;
19+
}
1720

1821
protected ElasticsearchDocValuesPropertyAttributeBase(FieldType type) : base(type) { }
1922

20-
[Obsolete("Please use overload taking FieldType")]
21-
protected ElasticsearchDocValuesPropertyAttributeBase(string typeName) : base(typeName) { }
22-
23-
[Obsolete("Please use overload taking FieldType")]
24-
protected ElasticsearchDocValuesPropertyAttributeBase(Type type) : base(type) { }
25-
2623
public new static ElasticsearchDocValuesPropertyAttributeBase From(MemberInfo memberInfo)
2724
{
2825
return memberInfo.GetCustomAttribute<ElasticsearchDocValuesPropertyAttributeBase>(true);

src/Nest/Mapping/AttributeBased/ElasticsearchPropertyAttributeBase.cs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public abstract class ElasticsearchPropertyAttributeBase : Attribute, IProperty,
1414
private IProperty Self => this;
1515

1616
PropertyName IProperty.Name { get; set; }
17-
TypeName IProperty.Type { get; set; }
17+
string IProperty.Type { get; set; }
1818
IDictionary<string, object> IProperty.LocalMetadata { get; set; }
1919

2020
public string Name { get; set; }
@@ -25,18 +25,6 @@ protected ElasticsearchPropertyAttributeBase(FieldType type)
2525
Self.Type = type.GetStringValue();
2626
}
2727

28-
[Obsolete("Please use overload taking FieldType")]
29-
protected ElasticsearchPropertyAttributeBase(string typeName)
30-
{
31-
Self.Type = typeName;
32-
}
33-
34-
[Obsolete("Please use overload taking FieldType")]
35-
protected ElasticsearchPropertyAttributeBase(Type type)
36-
{
37-
Self.Type = type;
38-
}
39-
4028
public static ElasticsearchPropertyAttributeBase From(MemberInfo memberInfo)
4129
{
4230
return memberInfo.GetCustomAttribute<ElasticsearchPropertyAttributeBase>(true);

src/Nest/Mapping/DynamicTemplate/SingleMapping.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Nest
66
{
7+
//TODO make this implement SelectorBase when that PR is moved
78
public class SingleMappingDescriptor<T> :
89
DescriptorBase<SingleMappingDescriptor<T>, IPropertiesDescriptor<T, IProperty>>, IPropertiesDescriptor<T, IProperty>
910
where T : class

src/Nest/Mapping/Types/Complex/Nested/NestedAttribute.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
{
33
public class NestedAttribute : ObjectAttribute, INestedProperty
44
{
5-
INestedProperty Self => this;
5+
private INestedProperty Self => this;
66

77
bool? INestedProperty.IncludeInParent { get; set; }
88
bool? INestedProperty.IncludeInRoot { get; set; }
99

10-
public bool IncludeInParent { get { return Self.IncludeInParent.GetValueOrDefault(); } set { Self.IncludeInParent = value; } }
11-
public bool IncludeInRoot { get { return Self.IncludeInRoot.GetValueOrDefault(); } set { Self.IncludeInRoot = value; } }
10+
public bool IncludeInParent { get => Self.IncludeInParent.GetValueOrDefault(); set => Self.IncludeInParent = value; }
11+
public bool IncludeInRoot { get => Self.IncludeInRoot.GetValueOrDefault(); set => Self.IncludeInRoot = value; }
1212

13-
public NestedAttribute() : base("nested") { }
13+
public NestedAttribute() : base(FieldType.Nested) { }
1414
}
1515
}

src/Nest/Mapping/Types/Complex/Object/ObjectAttribute.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,12 @@ public class ObjectAttribute : ElasticsearchCorePropertyAttributeBase, IObjectPr
77
private IObjectProperty Self => this;
88

99
public ObjectAttribute() : base(FieldType.Object) { }
10-
#pragma warning disable 618
11-
protected ObjectAttribute(string typeName) : base(typeName) { }
12-
protected ObjectAttribute(Type type) : base(type) { }
13-
#pragma warning restore 618
10+
protected ObjectAttribute(FieldType type) : base(type) { }
1411

1512
Union<bool, DynamicMapping> IObjectProperty.Dynamic { get; set; }
1613
bool? IObjectProperty.Enabled { get; set; }
1714
IProperties IObjectProperty.Properties { get; set; }
1815

19-
public bool Enabled { get { return Self.Enabled.GetValueOrDefault(); } set { Self.Enabled = value; } }
16+
public bool Enabled { get => Self.Enabled.GetValueOrDefault(); set => Self.Enabled = value; }
2017
}
2118
}

src/Nest/Mapping/Types/Complex/Object/ObjectProperty.cs

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,7 @@ public class ObjectProperty : CorePropertyBase, IObjectProperty
2323
{
2424
public ObjectProperty() : base(FieldType.Object) { }
2525

26-
[Obsolete("Please use overload taking FieldType")]
27-
protected ObjectProperty(string type) : base(type) { }
28-
29-
#pragma warning disable 618
30-
protected ObjectProperty(FieldType type) : this(type.GetStringValue()) { }
31-
#pragma warning restore 618
26+
protected ObjectProperty(FieldType type) : base(type) { }
3227

3328
public Union<bool, DynamicMapping> Dynamic { get; set; }
3429
public bool? Enabled { get; set; }
@@ -59,24 +54,16 @@ public abstract class ObjectPropertyDescriptorBase<TDescriptor, TInterface, TPar
5954

6055
protected ObjectPropertyDescriptorBase() : this(FieldType.Object) { }
6156

62-
[Obsolete("Please use overload taking FieldType")]
63-
protected ObjectPropertyDescriptorBase(string type) : base(type)
57+
protected ObjectPropertyDescriptorBase(FieldType type) : base(type)
6458
{
6559
_TypeName = TypeName.Create<TChild>();
6660
}
6761

68-
#pragma warning disable 618
69-
protected ObjectPropertyDescriptorBase(FieldType type) : this(type.GetStringValue()) { }
70-
#pragma warning restore 618
71-
72-
public TDescriptor Dynamic(Union<bool, DynamicMapping> dynamic) =>
73-
Assign(a => a.Dynamic = dynamic);
62+
public TDescriptor Dynamic(Union<bool, DynamicMapping> dynamic) => Assign(a => a.Dynamic = dynamic);
7463

75-
public TDescriptor Dynamic(bool dynamic = true) =>
76-
Assign(a => a.Dynamic = dynamic);
64+
public TDescriptor Dynamic(bool dynamic = true) => Assign(a => a.Dynamic = dynamic);
7765

78-
public TDescriptor Enabled(bool enabled = true) =>
79-
Assign(a => a.Enabled = enabled);
66+
public TDescriptor Enabled(bool enabled = true) => Assign(a => a.Enabled = enabled);
8067

8168
public TDescriptor Properties(Func<PropertiesDescriptor<TChild>, IPromise<IProperties>> selector) =>
8269
Assign(a => a.Properties = selector?.Invoke(new PropertiesDescriptor<TChild>(a.Properties))?.Value);

src/Nest/Mapping/Types/Core/Number/NumberAttribute.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@ public class NumberAttribute : ElasticsearchDocValuesPropertyAttributeBase, INum
1111
private INumberProperty Self => this;
1212

1313
public NumberAttribute() : base(FieldType.Float) { }
14-
public NumberAttribute(NumberType type) : base(type.ToFieldType()) { }
15-
#pragma warning disable 618
16-
protected NumberAttribute(string type) : base(type) { }
17-
#pragma warning restore 618
14+
protected NumberAttribute(NumberType type) : base(type.ToFieldType()) { }
1815

1916
bool? INumberProperty.Index { get; set; }
2017
double? INumberProperty.Boost { get; set; }
@@ -24,12 +21,12 @@ protected NumberAttribute(string type) : base(type) { }
2421
INumericFielddata INumberProperty.Fielddata { get; set; }
2522
double? INumberProperty.ScalingFactor { get; set; }
2623

27-
public bool Index { get { return Self.Index.GetValueOrDefault(); } set { Self.Index = value; } }
28-
public double Boost { get { return Self.Boost.GetValueOrDefault(); } set { Self.Boost = value; } }
29-
public double NullValue { get { return Self.NullValue.GetValueOrDefault(); } set { Self.NullValue = value; } }
30-
public bool IgnoreMalformed { get { return Self.IgnoreMalformed.GetValueOrDefault(); } set { Self.IgnoreMalformed = value; } }
31-
public bool Coerce { get { return Self.Coerce.GetValueOrDefault(); } set { Self.Coerce = value; } }
32-
public double ScalingFactor { get { return Self.ScalingFactor.GetValueOrDefault(); } set { Self.ScalingFactor = value; } }
24+
public bool Index { get => Self.Index.GetValueOrDefault(); set => Self.Index = value; }
25+
public double Boost { get => Self.Boost.GetValueOrDefault(); set => Self.Boost = value; }
26+
public double NullValue { get => Self.NullValue.GetValueOrDefault(); set => Self.NullValue = value; }
27+
public bool IgnoreMalformed { get => Self.IgnoreMalformed.GetValueOrDefault(); set => Self.IgnoreMalformed = value; }
28+
public bool Coerce { get => Self.Coerce.GetValueOrDefault(); set => Self.Coerce = value; }
29+
public double ScalingFactor { get => Self.ScalingFactor.GetValueOrDefault(); set => Self.ScalingFactor = value; }
3330

3431
}
3532
}

src/Nest/Mapping/Types/Core/Number/NumberProperty.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ public abstract class NumberPropertyDescriptorBase<TDescriptor, TInterface, T>
5454
{
5555
protected NumberPropertyDescriptorBase() : base(FieldType.Float) { }
5656

57-
[Obsolete("Please use overload taking FieldType")]
58-
protected NumberPropertyDescriptorBase(string type) : base(type) { }
57+
protected NumberPropertyDescriptorBase(FieldType type) : base(type) { }
5958

6059
bool? INumberProperty.Index { get; set; }
6160
double? INumberProperty.Boost { get; set; }
@@ -83,8 +82,7 @@ public TDescriptor Fielddata(Func<NumericFielddataDescriptor, INumericFielddata>
8382
public TDescriptor ScalingFactor(double scalingFactor) => Assign(a => a.ScalingFactor = scalingFactor);
8483
}
8584

86-
public class NumberPropertyDescriptor<T>
87-
: NumberPropertyDescriptorBase<NumberPropertyDescriptor<T>, INumberProperty, T>, INumberProperty
85+
public class NumberPropertyDescriptor<T> : NumberPropertyDescriptorBase<NumberPropertyDescriptor<T>, INumberProperty, T>
8886
where T : class
8987
{
9088
}

src/Nest/Mapping/Types/Core/String/StringAttribute.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,18 @@ public class StringAttribute : ElasticsearchDocValuesPropertyAttributeBase, IStr
1919
int? IStringProperty.PositionIncrementGap { get; set; }
2020
IStringFielddata IStringProperty.Fielddata { get; set; }
2121

22-
public string Analyzer { get { return Self.Analyzer; } set { Self.Analyzer = value; } }
23-
public double Boost { get { return Self.Boost.GetValueOrDefault(); } set { Self.Boost = value; } }
24-
public int IgnoreAbove { get { return Self.IgnoreAbove.GetValueOrDefault(); } set { Self.IgnoreAbove = value; } }
25-
public FieldIndexOption Index { get { return Self.Index.GetValueOrDefault(); } set { Self.Index = value; } }
26-
public IndexOptions IndexOptions { get { return Self.IndexOptions.GetValueOrDefault(); } set { Self.IndexOptions = value; } }
27-
public string NullValue { get { return Self.NullValue; } set { Self.NullValue = value; } }
28-
public int PositionIncrementGap { get { return Self.PositionIncrementGap.GetValueOrDefault(); } set { Self.PositionIncrementGap = value; } }
29-
public string SearchAnalyzer { get { return Self.SearchAnalyzer; } set { Self.SearchAnalyzer = value; } }
30-
public TermVectorOption TermVector { get { return Self.TermVector.GetValueOrDefault(); } set { Self.TermVector = value; } }
31-
public bool Norms { get { return Self.Norms.GetValueOrDefault(true); } set { Self.Norms = value; } }
22+
public string Analyzer { get => Self.Analyzer; set => Self.Analyzer = value; }
23+
public double Boost { get => Self.Boost.GetValueOrDefault(); set => Self.Boost = value; }
24+
public int IgnoreAbove { get => Self.IgnoreAbove.GetValueOrDefault(); set => Self.IgnoreAbove = value; }
25+
public FieldIndexOption Index { get => Self.Index.GetValueOrDefault(); set => Self.Index = value; }
26+
public IndexOptions IndexOptions { get => Self.IndexOptions.GetValueOrDefault(); set => Self.IndexOptions = value; }
27+
public string NullValue { get => Self.NullValue; set => Self.NullValue = value; }
28+
public int PositionIncrementGap { get => Self.PositionIncrementGap.GetValueOrDefault(); set => Self.PositionIncrementGap = value; }
29+
public string SearchAnalyzer { get => Self.SearchAnalyzer; set => Self.SearchAnalyzer = value; }
30+
public TermVectorOption TermVector { get => Self.TermVector.GetValueOrDefault(); set => Self.TermVector = value; }
31+
public bool Norms { get => Self.Norms.GetValueOrDefault(true); set => Self.Norms = value; }
3232

3333
[Obsolete("Only valid for indices created before Elasticsearch 5.0 and will be removed in the next major version. For newly created indices, use Text or Keyword attribute instead.")]
34-
public StringAttribute() : base("string") { }
34+
public StringAttribute() : base(FieldType.String) { }
3535
}
3636
}

src/Nest/Mapping/Types/Core/String/StringProperty.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public interface IStringProperty : IDocValuesProperty
4646
[DebuggerDisplay("{DebugDisplay}")]
4747
public class StringProperty : DocValuesPropertyBase, IStringProperty
4848
{
49-
public StringProperty() : base("string") { }
49+
public StringProperty() : base(FieldType.String) { }
5050

5151
public FieldIndexOption? Index { get; set; }
5252
public TermVectorOption? TermVector { get; set; }
@@ -79,7 +79,7 @@ public class StringPropertyDescriptor<T>
7979
int? IStringProperty.PositionIncrementGap { get; set; }
8080
IStringFielddata IStringProperty.Fielddata { get; set; }
8181

82-
public StringPropertyDescriptor() : base("string") { }
82+
public StringPropertyDescriptor() : base(FieldType.String) { }
8383

8484
/// <summary>
8585
/// Shortcut into .Index(FieldIndexOption.NotAnalyzed)

src/Nest/Mapping/Types/CorePropertyBase.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ public interface ICoreProperty : IProperty
2525
[DebuggerDisplay("{DebugDisplay}")]
2626
public abstract class CorePropertyBase : PropertyBase, ICoreProperty
2727
{
28-
[Obsolete("Please use overload taking FieldType")]
29-
protected CorePropertyBase(TypeName typeName) : base(typeName) { }
3028
protected CorePropertyBase(FieldType type) : base(type) { }
3129

3230

src/Nest/Mapping/Types/CorePropertyDescriptorBase.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,7 @@ public abstract class CorePropertyDescriptorBase<TDescriptor, TInterface, T>
1414
Fields ICoreProperty.CopyTo { get; set; }
1515
IProperties ICoreProperty.Fields { get; set; }
1616

17-
[Obsolete("Please use overload taking FieldType")]
18-
protected CorePropertyDescriptorBase(string type) : base(type) {}
19-
20-
#pragma warning disable 618
21-
protected CorePropertyDescriptorBase(FieldType type) : this(type.GetStringValue()) {}
22-
#pragma warning restore 618
17+
protected CorePropertyDescriptorBase(FieldType type) : base(type) {}
2318

2419
public TDescriptor Store(bool store = true) => Assign(a => a.Store = store);
2520

src/Nest/Mapping/Types/DocValuesPropertyBase.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,7 @@ public interface IDocValuesProperty : ICoreProperty
1414

1515
public abstract class DocValuesPropertyBase : CorePropertyBase, IDocValuesProperty
1616
{
17-
[Obsolete("Please use overload taking FieldType")]
18-
protected DocValuesPropertyBase(TypeName typeName) : base(typeName) { }
19-
20-
#pragma warning disable 618
21-
protected DocValuesPropertyBase(FieldType type) : this(type.GetStringValue()) { }
22-
#pragma warning restore 618
17+
protected DocValuesPropertyBase(FieldType type) : base(type) { }
2318

2419
public bool? DocValues { get; set; }
2520
}

src/Nest/Mapping/Types/DocValuesPropertyDescriptorBase.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ public abstract class DocValuesPropertyDescriptorBase<TDescriptor, TInterface, T
1111
{
1212
bool? IDocValuesProperty.DocValues { get; set; }
1313

14-
[Obsolete("Please use overload taking FieldType")]
15-
protected DocValuesPropertyDescriptorBase(string type) : base(type) { }
1614
protected DocValuesPropertyDescriptorBase(FieldType type) : base(type) { }
1715

1816
public TDescriptor DocValues(bool docValues = true) => Assign(a => a.DocValues = docValues);

src/Nest/Mapping/Types/PropertiesJsonConverter.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
4-
using Elasticsearch.Net;
53
using Newtonsoft.Json;
64
using Newtonsoft.Json.Linq;
75

@@ -19,8 +17,7 @@ internal class PropertiesJsonConverter : JsonConverter
1917

2018
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
2119
{
22-
var dict = value as IDictionary<PropertyName, IProperty>;
23-
if (dict == null) return;
20+
if (!(value is IDictionary<PropertyName, IProperty> dict)) return;
2421
var settings = serializer.GetConnectionSettings();
2522
var props = new Properties();
2623
foreach (var kv in dict)
@@ -33,8 +30,7 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
3330
continue;
3431
}
3532
// Check against connection settings mappings
36-
IPropertyMapping propertyMapping;
37-
if (settings.PropertyMappings.TryGetValue(propertyInfo, out propertyMapping))
33+
if (settings.PropertyMappings.TryGetValue(propertyInfo, out var propertyMapping))
3834
{
3935
if (propertyMapping.Ignore) continue;
4036
props.Add(propertyMapping.Name, kv.Value);

0 commit comments

Comments
 (0)