Skip to content

Commit baf8f99

Browse files
committed
fix #1731 contract resolvers were never called
1 parent 1c5f24d commit baf8f99

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

Diff for: src/Nest/CommonAbstractions/SerializationBehavior/ElasticContractResolver.cs

+8-8
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ public ElasticContractResolver(IConnectionSettingsValues connectionSettings, ILi
3232
this.ConnectionSettings = connectionSettings;
3333
}
3434

35-
3635
protected override JsonContract CreateContract(Type objectType)
3736
{
3837
JsonContract contract = base.CreateContract(objectType);
@@ -44,12 +43,8 @@ protected override JsonContract CreateContract(Type objectType)
4443
else if (objectType == typeof(ServerError))
4544
contract.Converter = new ServerErrorJsonConverter();
4645
else if (objectType == typeof(DateTime) || objectType == typeof(DateTime?))
47-
contract.Converter = new IsoDateTimeConverter();
48-
else if (!objectType.FullName.StartsWith("Nest.", StringComparison.OrdinalIgnoreCase)) return contract;
49-
50-
else if (ApplyExactContractJsonAttribute(objectType, contract)) return contract;
51-
else if (ApplyContractJsonAttribute(objectType, contract)) return contract;
52-
46+
contract.Converter = new IsoDateTimeConverter();
47+
5348
if (this._contractConverters.HasAny())
5449
{
5550
foreach (var c in this._contractConverters)
@@ -60,7 +55,12 @@ protected override JsonContract CreateContract(Type objectType)
6055
contract.Converter = converter;
6156
break;
6257
}
63-
}
58+
}
59+
if (!objectType.FullName.StartsWith("Nest.", StringComparison.OrdinalIgnoreCase)) return contract;
60+
61+
else if (ApplyExactContractJsonAttribute(objectType, contract)) return contract;
62+
else if (ApplyContractJsonAttribute(objectType, contract)) return contract;
63+
6464
return contract;
6565
}
6666

Diff for: src/Tests/ClientConcepts/LowLevel/Connecting.doc.cs

+15-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Nest;
1010
using Newtonsoft.Json;
1111
using Tests.Framework;
12+
using Tests.Framework.MockData;
1213

1314
namespace Tests.ClientConcepts.LowLevel
1415
{
@@ -296,14 +297,21 @@ public MyJsonNetSerializer(IConnectionSettingsValues settings) : base(settings)
296297
/**
297298
* Override ModifyJsonSerializerSettings if you need access to `JsonSerializerSettings`
298299
*/
299-
public int X { get; set; } = 0;
300-
protected override void ModifyJsonSerializerSettings(JsonSerializerSettings settings) => ++X;
300+
public int CallToModify { get; set; } = 0;
301+
protected override void ModifyJsonSerializerSettings(JsonSerializerSettings settings) => ++CallToModify;
301302

302303
/**
303304
* You can inject contract resolved converters by implementing the ContractConverters property
304305
* This can be much faster then registering them on JsonSerializerSettings.Converters
305306
*/
306-
protected override IList<Func<Type, JsonConverter>> ContractConverters => new List<Func<Type, JsonConverter>>();
307+
public int CallToContractConverter { get; set; } = 0;
308+
protected override IList<Func<Type, JsonConverter>> ContractConverters => new List<Func<Type, JsonConverter>>()
309+
{
310+
{ t => {
311+
CallToContractConverter++;
312+
return null;
313+
} }
314+
};
307315

308316
}
309317

@@ -320,7 +328,10 @@ public void ModifyJsonSerializerSettingsIsCalled()
320328
client.RootNodeInfo();
321329
client.RootNodeInfo();
322330
var serializer = ((IConnectionSettingsValues)settings).Serializer as MyJsonNetSerializer;
323-
serializer.X.Should().BeGreaterThan(0);
331+
serializer.CallToModify.Should().BeGreaterThan(0);
332+
333+
serializer.SerializeToString(new Project { });
334+
serializer.CallToContractConverter.Should().BeGreaterThan(0);
324335
}
325336
}
326337
}

0 commit comments

Comments
 (0)