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

Commit

Permalink
Fix #2278 - Only activate public properties
Browse files Browse the repository at this point in the history
  • Loading branch information
rynowak committed Jun 4, 2015
1 parent 30d11a8 commit 4ac7b68
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Microsoft.AspNet.Mvc.Razor/RazorPageActivator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ private PageActivationInfo CreateViewActivationInfo(Type type)
PropertyActivators = PropertyActivator<ViewContext>.GetPropertiesToActivate(
type,
typeof(RazorInjectAttribute),
CreateActivateInfo)
CreateActivateInfo,
includeNonPublic: true)
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,31 @@ public void CreateController_IgnoresPropertiesThatAreNotDecoratedWithAttribute()
Assert.Null(controller.ActionContext);
}

[Fact]
public void CreateController_IgnoresNonPublicProperties()
{
// Arrange
var actionDescriptor = new ControllerActionDescriptor
{
ControllerTypeInfo = typeof(ControllerWithNonVisibleProperties).GetTypeInfo()
};
var services = GetServices();
var httpContext = new DefaultHttpContext
{
RequestServices = services
};
var context = new ActionContext(httpContext, new RouteData(), actionDescriptor);
var factory = new DefaultControllerFactory(new DefaultControllerActivator(new DefaultTypeActivatorCache()));

// Act
var result = factory.CreateController(context);

// Assert
var controller = Assert.IsType<ControllerWithNonVisibleProperties>(result);
Assert.Null(controller.ActionContext);
Assert.Null(controller.BindingContext);
}

[Fact]
public void CreateController_ThrowsIfPropertyCannotBeActivated()
{
Expand Down Expand Up @@ -271,6 +296,13 @@ private class ControllerWithoutAttributes
public ViewDataDictionary ViewData { get; set; }
}

public class ControllerWithNonVisibleProperties
{
internal ActionContext ActionContext { get; set; }

public ActionBindingContext BindingContext { get; private set; }
}

private class ControllerWithAttributes
{
[ActionContext]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,36 @@ public void DefaultViewComponentActivator_ActivatesViewComponentContext()
Assert.Same(context, instance.ViewComponentContext);
}

[Fact]
public void DefaultViewComponentActivator_ActivatesViewComponentContext_IgnoresNonPublic()
{
// Arrange
var activator = new DefaultViewComponentActivator();

var context = new ViewComponentContext();
var instance = new VisibilityViewComponent();

// Act
activator.Activate(instance, context);

// Assert
Assert.Same(context, instance.ViewComponentContext);
Assert.Null(instance.C);
}

private class TestViewComponent : ViewComponent
{
public Task ExecuteAsync()
{
throw new NotImplementedException();
}
}

private class VisibilityViewComponent : ViewComponent
{
[ViewComponentContext]
protected internal ViewComponentContext C { get; set; }
}
}
}
#endif

0 comments on commit 4ac7b68

Please sign in to comment.