Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed bug in ExtractGenericArgumentDataContextChangeAttribute #1704

Merged
merged 2 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ public ExtractGenericArgumentDataContextChangeAttribute(Type genericType, int ty

public override Type? GetChildDataContextType(Type dataContext, DataContextStack controlContextStack, DotvvmBindableObject control, DotvvmProperty? property = null)
{
var implementations = ReflectionUtils.GetBaseTypesAndInterfaces(dataContext).ToList();
var implementations = ReflectionUtils.GetBaseTypesAndInterfaces(dataContext)
.Where(i => i.IsGenericType && i.GetGenericTypeDefinition() == GenericType)
.ToList();
if (implementations.Count == 0)
{
throw new Exception($"The data context {dataContext} doesn't implement {GenericType}!");
Expand Down
1 change: 1 addition & 0 deletions src/Framework/Framework/Utils/ReflectionUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,7 @@ public static IEnumerable<Type> GetBaseTypesAndInterfaces(Type type)
yield return i;
}

yield return type;
while (type.BaseType is { } baseType)
{
yield return baseType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ public string Text
}
public static readonly DotvvmProperty TextProperty
= DotvvmProperty.Register<string, ControlWithExtractGenericArgument>(c => c.Text, null);

[ExtractGenericArgumentDataContextChange(typeof(List<>), 0)]
public string Text2
{
get { return (string)GetValue(Text2Property); }
set { SetValue(Text2Property, value); }
}
public static readonly DotvvmProperty Text2Property
= DotvvmProperty.Register<string, ControlWithExtractGenericArgument>(c => c.Text2, null);

}

public class ListOfString : List<string>
{
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ public void ResolvedTree_CustomBindingResolverInDataContext()
}

[TestMethod]
public void ResolvedTree_DataContextChange_ExtractGenericArgument()
public void ResolvedTree_DataContextChange_ExtractGenericArgument_Interface()
{
var prop = ControlWithExtractGenericArgument.TextProperty;

Expand All @@ -682,6 +682,36 @@ public void ResolvedTree_DataContextChange_ExtractGenericArgument()
Assert.AreEqual(typeof(List<string>), binding.Binding.DataContextTypeStack.Parent!.DataContextType);
}

[TestMethod]
public void ResolvedTree_DataContextChange_ExtractGenericArgument_Self()
{
var prop = ControlWithExtractGenericArgument.Text2Property;

var root = ParseSource(@"@viewModel System.Collections.Generic.List<System.String>
<cc:ControlWithExtractGenericArgument Text2={value: _this} />");

var binding = root.Content.Single().Properties[prop] as ResolvedPropertyBinding;
Assert.IsNotNull(binding);

Assert.AreEqual(typeof(string), binding.Binding.DataContextTypeStack.DataContextType);
Assert.AreEqual(typeof(List<string>), binding.Binding.DataContextTypeStack.Parent!.DataContextType);
}

[TestMethod]
public void ResolvedTree_DataContextChange_ExtractGenericArgument_BaseType()
{
var prop = ControlWithExtractGenericArgument.Text2Property;

var root = ParseSource(@"@viewModel DotVVM.Framework.Tests.Runtime.ControlTree.DefaultControlTreeResolver.ListOfString
<cc:ControlWithExtractGenericArgument Text2={value: _this} />");

var binding = root.Content.Single().Properties[prop] as ResolvedPropertyBinding;
Assert.IsNotNull(binding);

Assert.AreEqual(typeof(string), binding.Binding.DataContextTypeStack.DataContextType);
Assert.AreEqual(typeof(ListOfString), binding.Binding.DataContextTypeStack.Parent!.DataContextType);
}

[TestMethod]
public void DefaultViewCompiler_ControlUsageValidator()
{
Expand Down
Loading