Skip to content
This repository has been archived by the owner on Dec 13, 2021. It is now read-only.

Commit

Permalink
Merge pull request #109 from leekelleher/hotfix-0.8.1
Browse files Browse the repository at this point in the history
Rejected TypeConverter value returns null
  • Loading branch information
leekelleher committed Sep 17, 2015
2 parents 885df62 + bd5c6ee commit 4749fb1
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 1 deletion.
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# version format
version: 0.8.0.{build}
version: 0.8.1.{build}

# UMBRACO_PACKAGE_PRERELEASE_SUFFIX if a rtm release build this should be blank, otherwise if empty will default to alpha
# example UMBRACO_PACKAGE_PRERELEASE_SUFFIX=beta
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,12 @@ private static object GetConvertedValue(
}
}
}
else if (propertyType.IsInstanceOfType(propertyValue))
{
// If the TypeConverter's `CanConvertFrom` has returned false,
// then we can check if the value is the same type as the target type.
result = propertyValue;
}
}
}
}
Expand Down
99 changes: 99 additions & 0 deletions tests/Our.Umbraco.Ditto.Tests/ClassLevelTypeConverterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
namespace Our.Umbraco.Ditto.Tests
{
using System;
using System.ComponentModel;
using System.Globalization;
using NUnit.Framework;
using Our.Umbraco.Ditto.Tests.Mocks;

[TestFixture]
public class ClassLevelTypeConverterTests
{
[TypeConverter(typeof(MyCustomConverter))]
public class MyCustomModel
{
public MyCustomModel(string name)
{
Name = name;
}

public string Name { get; set; }
}

public class MyModel1
{
[UmbracoProperty("Name")]
public MyCustomModel MyProperty { get; set; }
}

public class MyModel2
{
[DittoValueResolver(typeof(MyCustomValueResolver))]
public MyCustomModel MyProperty { get; set; }
}

public class MyCustomValueResolver : DittoValueResolver
{
public override object ResolveValue()
{
return new MyCustomModel("MyCustomName");
}
}

public class MyCustomConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return sourceType == typeof(string);
}

public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string)
return new MyCustomModel((string)value);

return base.ConvertFrom(context, culture, value);
}
}

[Test]
public void ClassLevel_TypeConverter_UmbracoProperty()
{
// In this test, the `MyProperty` property gets a `string` value
// via the `UmbracoProperty`. The `string` type/value is passed
// to the `MyCustomConverter` so to convert the `string` to a
// `MyCustomModel` type/object.

var content = new PublishedContentMock() { Name = "MyName" };
var model = content.As<MyModel1>();

Assert.IsNotNull(model);
Assert.IsInstanceOf<MyModel1>(model);

Assert.IsNotNull(model.MyProperty);
Assert.IsInstanceOf<MyCustomModel>(model.MyProperty);
Assert.That(model.MyProperty.Name, Is.EqualTo("MyName"));
}

[Test]
public void ClassLevel_TypeConverter_ValueResolver()
{
// In this test, the `MyProperty` property gets its value from
// the `MyCustomValueResolver` (returning a `MyCustomModel`).
// The `MyCustomConverter` is called, but fails the
// `CanConvertFrom` check, so wouldn't try to convert it.
// Since the value type is the same as the target property type,
// the property value can be set.

var content = new PublishedContentMock() { Name = "MyName" };
var model = content.As<MyModel2>();

Assert.IsNotNull(model);
Assert.IsInstanceOf<MyModel2>(model);

Assert.IsNotNull(model.MyProperty);
Assert.IsInstanceOf<MyCustomModel>(model.MyProperty);
Assert.That(model.MyProperty.Name, Is.EqualTo("MyCustomName"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@
<Compile Include="CaseSensitivityTests.cs" />
<Compile Include="BasicMappingTests.cs" />
<Compile Include="CastableInterfaceTests.cs" />
<Compile Include="ClassLevelTypeConverterTests.cs" />
<Compile Include="CustomTypeConverterTests.cs" />
<Compile Include="ConversionHandlerTests.cs" />
<Compile Include="CustomValueResolverTests.cs" />
Expand Down

0 comments on commit 4749fb1

Please sign in to comment.