2020-12-20 08:36:11 +01:00
|
|
|
// Copyright (c) Umbraco.
|
|
|
|
|
// See LICENSE for more details.
|
|
|
|
|
|
2020-06-29 12:35:08 +02:00
|
|
|
using System.Runtime.Serialization;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using NUnit.Framework;
|
2021-02-18 11:06:02 +01:00
|
|
|
using Umbraco.Cms.Web.Common.Formatters;
|
2020-06-29 12:35:08 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Web.Common.Formatters;
|
|
|
|
|
|
|
|
|
|
[TestFixture]
|
|
|
|
|
public class IgnoreRequiredAttributesResolverUnitTests
|
2020-06-29 12:35:08 +02:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
[Test]
|
|
|
|
|
public void Test()
|
2020-06-29 12:35:08 +02:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
const string emptyJsonObject = "{}";
|
2020-06-29 12:35:08 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
Assert.Multiple(() =>
|
|
|
|
|
{
|
|
|
|
|
// Ensure the deserialization throws if using default settings
|
|
|
|
|
Assert.Throws<JsonSerializationException>(() =>
|
|
|
|
|
JsonConvert.DeserializeObject<ObjectWithRequiredProperty>(emptyJsonObject));
|
2020-06-29 12:35:08 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var actual = JsonConvert.DeserializeObject<ObjectWithRequiredProperty>(
|
|
|
|
|
emptyJsonObject,
|
|
|
|
|
new JsonSerializerSettings { ContractResolver = new IgnoreRequiredAttributesResolver() });
|
2020-06-29 12:35:08 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
Assert.IsNotNull(actual);
|
|
|
|
|
Assert.IsNull(actual.Property);
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-06-29 12:35:08 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
[DataContract(Name = "objectWithRequiredProperty", Namespace = "")]
|
|
|
|
|
private class ObjectWithRequiredProperty
|
|
|
|
|
{
|
|
|
|
|
[DataMember(Name = "property", IsRequired = true)]
|
|
|
|
|
public string Property { get; set; }
|
2020-06-29 12:35:08 +02:00
|
|
|
}
|
|
|
|
|
}
|