Skip to content
This repository has been archived by the owner on Mar 20, 2019. It is now read-only.

Commit

Permalink
Fix for UIRequest.Icon deserialization exception due to nullable stru…
Browse files Browse the repository at this point in the history
…ct types.

Fixes Trac #195
  • Loading branch information
AArnott committed May 8, 2010
1 parent 998cfe3 commit 1be3ed3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ public void RequiredNonNullableStruct() {

[TestCase]
public void OptionalNullableStruct() {
this.ParameterizedMessageTypeTest(typeof(MessageWithNullableOptionalStruct));
var message = new MessageWithNullableOptionalStruct();
var part = this.ParameterizedMessageTypeTest(message.GetType());

Assert.IsNull(part.GetValue(message));
part.SetValue(message, "3");
Assert.AreEqual("3", part.GetValue(message));
}

[TestCase]
Expand Down
19 changes: 18 additions & 1 deletion src/DotNetOpenAuth.Test/OpenId/Extensions/UI/UIRequestTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,27 @@ namespace DotNetOpenAuth.Test.OpenId.Extensions.UI {
public class UIRequestTests : OpenIdTestBase {
[TestCase]
public void Defaults() {
UIRequest request = new UIRequest();
var request = new UIRequest();
Assert.AreEqual("popup", request.Mode);
Assert.AreEqual(1, request.LanguagePreference.Length);
Assert.AreEqual(CultureInfo.CurrentUICulture, request.LanguagePreference[0]);
Assert.IsFalse(request.Icon.HasValue);
}

[TestCase]
public void IconEncodingDecoding()
{
var request = new UIRequest();
MessageDictionary dictionary = this.MessageDescriptions.GetAccessor(request);
Assert.IsFalse(dictionary.ContainsKey("icon"));

Assert.IsFalse(request.Icon.HasValue);
dictionary["icon"] = "true";
Assert.IsTrue(request.Icon.Value);

dictionary.ClearValues();
request.Icon = true;
Assert.AreEqual("true", dictionary["icon"]);
}

[TestCase]
Expand Down
24 changes: 20 additions & 4 deletions src/DotNetOpenAuth/Messaging/Reflection/MessagePart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,25 @@ internal MessagePart(MemberInfo member, MessagePartAttribute attribute) {
Contract.Assume(this.memberDeclaredType != null); // CC missing PropertyInfo.PropertyType ensures result != null
if (attribute.Encoder == null) {
if (!converters.TryGetValue(this.memberDeclaredType, out this.converter)) {
this.converter = new ValueMapping(
obj => obj != null ? obj.ToString() : null,
str => str != null ? Convert.ChangeType(str, this.memberDeclaredType, CultureInfo.InvariantCulture) : null);
if (this.memberDeclaredType.IsGenericType &&
this.memberDeclaredType.GetGenericTypeDefinition() == typeof(Nullable<>)) {
// It's a nullable type. Try again to look up an appropriate converter for the underlying type.
Type underlyingType = Nullable.GetUnderlyingType(this.memberDeclaredType);
ValueMapping underlyingMapping;
if (converters.TryGetValue(underlyingType, out underlyingMapping)) {
this.converter = new ValueMapping(
underlyingMapping.ValueToString,
str => str != null ? underlyingMapping.StringToValue(str) : null);
} else {
this.converter = new ValueMapping(
obj => obj != null ? obj.ToString() : null,
str => str != null ? Convert.ChangeType(str, underlyingType, CultureInfo.InvariantCulture) : null);
}
} else {
this.converter = new ValueMapping(
obj => obj != null ? obj.ToString() : null,
str => str != null ? Convert.ChangeType(str, this.memberDeclaredType, CultureInfo.InvariantCulture) : null);
}
}
} else {
this.converter = new ValueMapping(GetEncoder(attribute.Encoder));
Expand Down Expand Up @@ -252,7 +268,7 @@ private static object DeriveDefaultValue(Type type) {
}

/// <summary>
/// Adds a pair of type conversion functions to the static converstion map.
/// Adds a pair of type conversion functions to the static conversion map.
/// </summary>
/// <typeparam name="T">The custom type to convert to and from strings.</typeparam>
/// <param name="toString">The function to convert the custom type to a string.</param>
Expand Down

0 comments on commit 1be3ed3

Please sign in to comment.