Skip to content

Commit 5989d19

Browse files
committed
Re-fixes the issue with using IDataValueSetter.SetValue on the DefaultData implementation and ensures that when there is a null value that it reverts to an empty string since this was what the default value was in the Value getter of DefaultData when there was no value. Have added a couple unit tests to support.
1 parent da1c33a commit 5989d19

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

src/Umbraco.Core/Models/PropertyExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ internal static XElement ToXml(this Property property, IDataTypeService dataType
4343
//This seems to fail during testing
4444
//SD: With the new null checks below, this shouldn't fail anymore.
4545
var dt = property.PropertyType.DataType(property.Id, dataTypeService);
46-
if (dt != null && dt.Data != null && dt.Data.Value != null)
46+
if (dt != null && dt.Data != null)
4747
{
4848
//We've already got the value for the property so we're going to give it to the
4949
// data type's data property so it doesn't go re-look up the value from the db again.

src/Umbraco.Tests/Models/DataValueSetterTests.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ public void SetValue_Is_Called_When_Executing_ToXml_On_A_Property_With_DataType_
6666
var dataTypeId = Guid.NewGuid();
6767

6868
var dataTypeData = MockRepository.GenerateMock<IData, IDataValueSetter>();
69-
//needs to have a value for SetValue to be called
70-
dataTypeData.Stub(data => data.Value).Return(string.Empty);
69+
7170
dataTypeData
7271
.Stub(data => data.ToXMl(Arg<XmlDocument>.Is.Anything))
7372
.Return(null) // you have to call Return() even though we're about to override it
@@ -100,5 +99,18 @@ public void SetValue_Is_Called_When_Executing_ToXml_On_A_Property_With_DataType_
10099
((IDataValueSetter)dataTypeData).AssertWasCalled(setter => setter.SetValue("Hello world", DataTypeDatabaseType.Nvarchar.ToString()));
101100
}
102101

102+
[TestCase(DataTypeDatabaseType.Nvarchar)]
103+
[TestCase(DataTypeDatabaseType.Date)]
104+
[TestCase(DataTypeDatabaseType.Integer)]
105+
[TestCase(DataTypeDatabaseType.Ntext)]
106+
public void DefaultData_SetValue_Ensures_Empty_String_When_Null_Value_Any_Data_Type(DataTypeDatabaseType type)
107+
{
108+
var defaultData = new DefaultData(MockRepository.GenerateMock<BaseDataType>());
109+
110+
((IDataValueSetter)defaultData).SetValue(null, type.ToString());
111+
112+
Assert.AreEqual(string.Empty, defaultData.Value);
113+
}
114+
103115
}
104116
}

src/umbraco.cms/businesslogic/datatype/DefaultData.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,17 @@ public virtual void Initialize(object InitValue, int InitPropertyId)
6565
/// <param name="strDbType"></param>
6666
void IDataValueSetter.SetValue(object val, string strDbType)
6767
{
68+
//We need to ensure that val is not a null value, if it is then we'll convert this to an empty string.
69+
//The reason for this is because by default the DefaultData.Value property returns an empty string when
70+
// there is no value, this is based on the PropertyDataDto.GetValue return value which defaults to an
71+
// empty string (which is called from this class's method LoadValueFromDatabase).
72+
//Some legacy implementations of DefaultData are expecting an empty string when there is
73+
// no value so we need to keep this consistent.
74+
if (val == null)
75+
{
76+
val = string.Empty;
77+
}
78+
6879
_value = val;
6980
//now that we've set our value, we can update our BaseDataType object with the correct values from the db
7081
//instead of making it query for itself. This is a peformance optimization enhancement.

0 commit comments

Comments
 (0)