Added unittest of IgnoreRequiredAttributesResolver
Signed-off-by: Bjarke Berg <mail@bergmania.dk>
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Runtime.Serialization;
|
||||
using Newtonsoft.Json;
|
||||
using NUnit.Framework;
|
||||
using Umbraco.Web.Common.Formatters;
|
||||
|
||||
namespace Umbraco.Tests.UnitTests.Umbraco.Web.Common.Formatters
|
||||
{
|
||||
[TestFixture]
|
||||
public class IgnoreRequiredAttributesResolverUnitTests
|
||||
{
|
||||
|
||||
[Test]
|
||||
public void Test()
|
||||
{
|
||||
const string emptyJsonObject = "{}";
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
//Ensure the deserialization throws if using default settings
|
||||
Assert.Throws<JsonSerializationException>(() =>
|
||||
JsonConvert.DeserializeObject<ObjectWithRequiredProperty>(emptyJsonObject));
|
||||
|
||||
var actual = JsonConvert.DeserializeObject<ObjectWithRequiredProperty>(emptyJsonObject,new JsonSerializerSettings
|
||||
{
|
||||
ContractResolver = new IgnoreRequiredAttributesResolver()
|
||||
});
|
||||
|
||||
Assert.IsNotNull(actual);
|
||||
Assert.IsNull(actual.Property);
|
||||
});
|
||||
}
|
||||
|
||||
[DataContract(Name = "objectWithRequiredProperty", Namespace = "")]
|
||||
private class ObjectWithRequiredProperty
|
||||
{
|
||||
[DataMember(Name = "property", IsRequired = true)]
|
||||
public string Property{ get; set; }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,6 @@ namespace Umbraco.Web.Common.Formatters
|
||||
{
|
||||
var property = base.CreateProperty(member, memberSerialization);
|
||||
|
||||
|
||||
property.Required = Required.Default;
|
||||
|
||||
return property;
|
||||
|
||||
Reference in New Issue
Block a user