Skip to content

Commit 37b9546

Browse files
John Danielsgmarz
John Daniels
authored andcommitted
Fix issues with deserializing mappings when retrieving mappings from the server.
We need to make sure that we can deserialize the interfaces on the RootObjectMapping, which requires specifying ResolveAsTypeConverter on all the appropriate interfaces. I added a test as well, though I'm not sure it's in the right place, and I added a Deserialize function on TestElasticClient to make it easier to roundtrip the serialization.
1 parent 682bc52 commit 37b9546

13 files changed

+136
-6
lines changed

Diff for: src/Nest/Domain/Mapping/SpecialFields/AnalyzerFieldMapping.cs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
namespace Nest
77
{
8+
[JsonConverter(typeof(ReadAsTypeConverter<AnalyzerFieldMapping>))]
89
public interface IAnalyzerFieldMapping : ISpecialField
910
{
1011
[JsonProperty("index"), JsonConverter(typeof(YesNoBoolConverter))]

Diff for: src/Nest/Domain/Mapping/SpecialFields/BoostFieldMapping.cs

+2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using System;
22
using System.Linq.Expressions;
33
using Newtonsoft.Json;
4+
using Nest.Resolvers.Converters;
45

56
namespace Nest
67
{
8+
[JsonConverter(typeof(ReadAsTypeConverter<BoostFieldMapping>))]
79
public interface IBoostFieldMapping : ISpecialField
810
{
911
[JsonProperty("name")]

Diff for: src/Nest/Domain/Mapping/SpecialFields/IdFieldMapping.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
using Newtonsoft.Json;
1+
using Nest.Resolvers.Converters;
2+
using Newtonsoft.Json;
23

34
namespace Nest
45
{
6+
[JsonConverter(typeof(ReadAsTypeConverter<IdFieldMapping>))]
57
public interface IIdFieldMapping : ISpecialField
68
{
79
[JsonProperty("path")]

Diff for: src/Nest/Domain/Mapping/SpecialFields/IndexFieldMapping.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
using Newtonsoft.Json;
1+
using Nest.Resolvers.Converters;
2+
using Newtonsoft.Json;
23

34
namespace Nest
45
{
6+
[JsonConverter(typeof(ReadAsTypeConverter<IndexFieldMapping>))]
57
public interface IIndexFieldMapping : ISpecialField
68
{
79
[JsonProperty("enabled")]

Diff for: src/Nest/Domain/Mapping/SpecialFields/RoutingFieldMapping.cs

+2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using System;
22
using Newtonsoft.Json;
33
using System.Linq.Expressions;
4+
using Nest.Resolvers.Converters;
45

56
namespace Nest
67
{
8+
[JsonConverter(typeof(ReadAsTypeConverter<RoutingFieldMapping>))]
79
public interface IRoutingFieldMapping : ISpecialField
810
{
911
[JsonProperty("required")]

Diff for: src/Nest/Domain/Mapping/SpecialFields/SizeFieldMapping.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
using Newtonsoft.Json;
1+
using Nest.Resolvers.Converters;
2+
using Newtonsoft.Json;
23

34
namespace Nest
45
{
6+
[JsonConverter(typeof(ReadAsTypeConverter<SizeFieldMapping>))]
57
public interface ISizeFieldMapping : ISpecialField
68
{
79
[JsonProperty("enabled")]

Diff for: src/Nest/Domain/Mapping/SpecialFields/SourceFieldMapping.cs

+2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using System.Collections.Generic;
22
using Newtonsoft.Json;
3+
using Nest.Resolvers.Converters;
34

45
namespace Nest
56
{
7+
[JsonConverter(typeof(ReadAsTypeConverter<SourceFieldMapping>))]
68
public interface ISourceFieldMapping : ISpecialField
79
{
810
[JsonProperty("enabled")]

Diff for: src/Nest/Domain/Mapping/SpecialFields/TimestampFieldMapping.cs

+2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using System;
22
using Newtonsoft.Json;
33
using System.Linq.Expressions;
4+
using Nest.Resolvers.Converters;
45

56
namespace Nest
67
{
8+
[JsonConverter(typeof(ReadAsTypeConverter<TimestampFieldMapping>))]
79
public interface ITimestampFieldMapping : ISpecialField
810
{
911
[JsonProperty("enabled")]

Diff for: src/Nest/Domain/Mapping/SpecialFields/TtlFieldMapping.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
using Newtonsoft.Json;
1+
using Nest.Resolvers.Converters;
2+
using Newtonsoft.Json;
23

34
namespace Nest
45
{
6+
[JsonConverter(typeof(ReadAsTypeConverter<TtlFieldMapping>))]
57
public interface ITtlFieldMapping : ISpecialField
68
{
79
[JsonProperty("enabled")]

Diff for: src/Nest/Domain/Mapping/SpecialFields/TypeFieldMapping.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
using Newtonsoft.Json;
1+
using Nest.Resolvers.Converters;
2+
using Newtonsoft.Json;
23
using Newtonsoft.Json.Converters;
34

45
namespace Nest
56
{
7+
[JsonConverter(typeof(ReadAsTypeConverter<TypeFieldMapping>))]
68
public interface ITypeFieldMapping : ISpecialField
79
{
810
[JsonProperty("index"), JsonConverter(typeof(StringEnumConverter))]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
using Newtonsoft.Json;
2+
using NUnit.Framework;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.IO;
6+
using System.Linq;
7+
using System.Reflection;
8+
using System.Text;
9+
using System.Threading.Tasks;
10+
11+
namespace Nest.Tests.Unit.Core.Map
12+
{
13+
class GetMappingSerializationTests : BaseJsonTests
14+
{
15+
/// <summary>
16+
/// Verify that we can serialize/deserialize a populated root mapping.
17+
/// If we can round trip a full root mapping, we're in a pretty good place.
18+
/// </summary>
19+
[Test]
20+
public void CanDeserializeRootMapping()
21+
{
22+
var rootMapping = new RootObjectMapping()
23+
{
24+
AllFieldMapping = new AllFieldMapping()
25+
{
26+
Enabled = true,
27+
IndexAnalyzer = "index_analyzer"
28+
},
29+
SourceFieldMappingDescriptor = new SourceFieldMapping()
30+
{
31+
Compress = false,
32+
Excludes = new[] { "excluded" }
33+
},
34+
RoutingFieldMapping = new RoutingFieldMapping()
35+
{
36+
Path = "routing_path"
37+
},
38+
SizeFieldMapping = new SizeFieldMapping()
39+
{
40+
Enabled = true,
41+
},
42+
TtlFieldMappingDescriptor = new TtlFieldMapping()
43+
{
44+
Enabled = true,
45+
},
46+
IdFieldMappingDescriptor = new IdFieldMapping()
47+
{
48+
Index = "not_analyzed",
49+
Store = false,
50+
Path = "id_field",
51+
},
52+
TimestampFieldMapping = new TimestampFieldMapping()
53+
{
54+
Enabled = true,
55+
Format = "YYY-MM-dd",
56+
Path = "the_timestamp",
57+
},
58+
IndexFieldMapping = new IndexFieldMapping()
59+
{
60+
Enabled = true,
61+
},
62+
AnalyzerFieldMapping = new AnalyzerFieldMapping()
63+
{
64+
Path = "index_path",
65+
},
66+
BoostFieldMapping = new BoostFieldMapping()
67+
{
68+
Name = "boost",
69+
NullValue = 2.0,
70+
},
71+
Parent = new ParentTypeMapping()
72+
{
73+
Type = "type"
74+
},
75+
TypeFieldMappingDescriptor = new TypeFieldMapping()
76+
{
77+
Index = NonStringIndexOption.NotAnalyzed,
78+
Store = false,
79+
}
80+
};
81+
var json = TestElasticClient.Serialize(rootMapping);
82+
83+
var mapping = TestElasticClient.Deserialize<RootObjectMapping>(json);
84+
TestElasticClient.Serialize(mapping).JsonEquals(json);
85+
}
86+
87+
/// <summary>
88+
/// Verify that we can serialize/deserialize an empty root mapping. One of the
89+
/// failure modes of serialization/deserialization is handling data that's not there.
90+
/// </summary>
91+
[Test]
92+
public void CanDeserializeEmptyRootMapping()
93+
{
94+
var rootMapping = new RootObjectMapping()
95+
{
96+
};
97+
var json = TestElasticClient.Serialize(rootMapping);
98+
99+
var mapping = TestElasticClient.Deserialize<RootObjectMapping>(json);
100+
TestElasticClient.Serialize(mapping).JsonEquals(json);
101+
}
102+
103+
}
104+
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
<Compile Include="Core\Indices\Analysis\Analyzers\AnalyzerTests.cs" />
126126
<Compile Include="Core\Indices\Similarity\SimilarityTests.cs" />
127127
<Compile Include="Core\Map\CustomMapping\ImagePluginMappingTests.cs" />
128+
<Compile Include="Core\Map\GetMappingSerializationTests.cs" />
128129
<Compile Include="Core\Map\MappingBehaviourTests.cs" />
129130
<None Include="Cluster\PutSettings\PutSettings.json">
130131
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
@@ -1216,7 +1217,6 @@
12161217
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
12171218
</None>
12181219
</ItemGroup>
1219-
<ItemGroup />
12201220
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
12211221
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
12221222
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

Diff for: src/Tests/Nest.Tests.Unit/TestElasticClient.cs

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.IO;
34
using System.Linq;
45
using System.Text;
56

@@ -15,9 +16,15 @@ static TestElasticClient()
1516

1617
Client = new ElasticClient(Settings);
1718
}
19+
1820
public static string Serialize<T>(T obj) where T : class
1921
{
2022
return Encoding.UTF8.GetString(Client.Serializer.Serialize(obj));
2123
}
24+
25+
public static T Deserialize<T>(string json) where T : class
26+
{
27+
return Client.Serializer.Deserialize<T>(new MemoryStream(Encoding.UTF8.GetBytes(json)));
28+
}
2229
}
2330
}

0 commit comments

Comments
 (0)