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

Commit

Permalink
Workaround Mono bugs to fix travis
Browse files Browse the repository at this point in the history
This change works around two mono issues that are blocking travis. I'd
like to have tests skip instead, but unfortunately that would mean not
running any validation tests on mono so this seems better.

- RuntimeHelpers.GetHashCode will sometimes crash the runtime
- PropertyHelper sometimes returns throws null-ref
  • Loading branch information
rynowak committed Sep 28, 2015
1 parent 18c80d1 commit 67b43b4
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ IEnumerator IEnumerable.GetEnumerator()

private class ReferenceEqualityComparer : IEqualityComparer<object>
{
private static readonly bool IsMono = Type.GetType("Mono.Runtime") != null;

public static readonly ReferenceEqualityComparer Instance = new ReferenceEqualityComparer();

public new bool Equals(object x, object y)
Expand All @@ -172,6 +174,14 @@ private class ReferenceEqualityComparer : IEqualityComparer<object>

public int GetHashCode(object obj)
{
// RuntimeHelpers.GetHashCode sometimes crashes the runtime on Mono 4.0.4
// See: https://github.com/aspnet/External/issues/45
// The workaround here is to just not hash anything, and fall back to an equality check.
if (IsMono)
{
return 0;
}

return RuntimeHelpers.GetHashCode(obj);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

namespace Microsoft.AspNet.Mvc.ModelBinding.Validation
{
Expand All @@ -12,6 +13,8 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Validation
/// </summary>
public class DefaultComplexObjectValidationStrategy : IValidationStrategy
{
private static readonly bool IsMono = Type.GetType("Mono.Runtime") != null;

/// <summary>
/// Gets an instance of <see cref="DefaultComplexObjectValidationStrategy"/>.
/// </summary>
Expand Down Expand Up @@ -78,7 +81,34 @@ public bool MoveNext()
var property = _properties[_index];
var propertyName = property.BinderModelName ?? property.PropertyName;
var key = ModelNames.CreatePropertyModelName(_key, propertyName);
var model = property.PropertyGetter(_model);

object model;

// Our property accessors don't work on Mono 4.0.4 - see https://github.com/aspnet/External/issues/44
// This is a workaround for what the PropertyGetter does in the background.
if (IsMono)
{
if (_model == null)
{
model = null;
}
else
{
var propertyInfo = _model.GetType().GetRuntimeProperty(property.PropertyName);
try
{
model = propertyInfo.GetValue(_model);
}
catch (TargetInvocationException ex)
{
throw ex.InnerException;
}
}
}
else
{
model = property.PropertyGetter(_model);
}

_entry = new ValidationEntry(property, key, model);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;

Expand All @@ -10,6 +11,8 @@ internal class ReferenceEqualityComparer : IEqualityComparer<object>
{
private static readonly ReferenceEqualityComparer _instance = new ReferenceEqualityComparer();

private static readonly bool IsMono = Type.GetType("Mono.Runtime") != null;

public static ReferenceEqualityComparer Instance
{
get
Expand All @@ -25,6 +28,14 @@ public static ReferenceEqualityComparer Instance

public int GetHashCode(object obj)
{
// RuntimeHelpers.GetHashCode sometimes crashes the runtime on Mono 4.0.4
// See: https://github.com/aspnet/External/issues/45
// The workaround here is to just not hash anything, and fall back to an equality check.
if (IsMono)
{
return 0;
}

return RuntimeHelpers.GetHashCode(obj);
}
}
Expand Down

0 comments on commit 67b43b4

Please sign in to comment.