Skip to content

Commit

Permalink
Merge pull request #526 from aspnet/ajbaaska/generic-int-fix
Browse files Browse the repository at this point in the history
Fix invalid cast in non-generic parameterized ChildContent
  • Loading branch information
ajaybhargavb authored May 2, 2019
2 parents 1a4b0cb + f78e4c2 commit e622d7c
Show file tree
Hide file tree
Showing 7 changed files with 242 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,7 @@ private void RewriteTypeNames(TypeNameRewriter rewriter, ComponentIntermediateNo
}
else if (childContent.IsParameterized)
{
// This is a weakly typed parameterized child content, treat it as RenderFragment<object>
childContent.TypeName = ComponentsApi.RenderFragment.FullTypeName + "<System.Object>";
// This is a non-generic parameterized child content like RenderFragment<int>, leave it as is.
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2655,6 +2655,39 @@ public class MyComponent<TItem> : ComponentBase
CompileToAssembly(generated);
}

[Fact]
public void ChildComponent_NonGenericParameterizedChildContent_TypeInference()
{
// Arrange
AdditionalSyntaxTrees.Add(Parse(@"
using Microsoft.AspNetCore.Components;
namespace Test
{
public class MyComponent<TItem> : ComponentBase
{
[Parameter] TItem Item { get; set; }
[Parameter] RenderFragment<TItem> GenericFragment { get; set; }
[Parameter] RenderFragment<int> IntFragment { get; set; }
}
}
"));

// Act
var generated = CompileToCSharp(@"
<MyComponent Item=""@(""hi"")"">
<GenericFragment>@context.ToLower()</GenericFragment>
<IntFragment>@context</IntFragment>
</MyComponent>");

// Assert
AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
AssertCSharpDocumentMatchesBaseline(generated.CodeDocument);
CompileToAssembly(generated);
}

[Fact]
public void GenericComponent_WithFullyQualifiedTagName()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// <auto-generated/>
#pragma warning disable 1591
namespace Test
{
#line hidden
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
public class TestComponent : Microsoft.AspNetCore.Components.ComponentBase
{
#pragma warning disable 219
private void __RazorDirectiveTokenHelpers__() {
}
#pragma warning restore 219
#pragma warning disable 0414
private static System.Object __o = null;
#pragma warning restore 0414
#pragma warning disable 1998
protected override void BuildRenderTree(Microsoft.AspNetCore.Components.RenderTree.RenderTreeBuilder builder)
{
__Blazor.Test.TestComponent.TypeInference.CreateMyComponent_0(builder, -1, -1,
#nullable restore
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
"hi"

#line default
#line hidden
#nullable disable
, -1, (context) => (builder2) => {
#nullable restore
#line 2 "x:\dir\subdir\Test\TestComponent.cshtml"
__o = context.ToLower();
#line default
#line hidden
#nullable disable
}
, -1, (context) => (builder2) => {
#nullable restore
#line 3 "x:\dir\subdir\Test\TestComponent.cshtml"
__o = context;
#line default
#line hidden
#nullable disable
}
);
#nullable restore
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
__o = typeof(MyComponent<>);

#line default
#line hidden
#nullable disable
}
#pragma warning restore 1998
}
}
namespace __Blazor.Test.TestComponent
{
#line hidden
internal static class TypeInference
{
public static void CreateMyComponent_0<TItem>(global::Microsoft.AspNetCore.Components.RenderTree.RenderTreeBuilder builder, int seq, int __seq0, TItem __arg0, int __seq1, global::Microsoft.AspNetCore.Components.RenderFragment<TItem> __arg1, int __seq2, global::Microsoft.AspNetCore.Components.RenderFragment<System.Int32> __arg2)
{
builder.OpenComponent<global::Test.MyComponent<TItem>>(seq);
builder.AddAttribute(__seq0, "Item", __arg0);
builder.AddAttribute(__seq1, "GenericFragment", __arg1);
builder.AddAttribute(__seq2, "IntFragment", __arg2);
builder.CloseComponent();
}
}
}
#pragma warning restore 1591
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Document -
NamespaceDeclaration - - Test
UsingDirective - (3:1,1 [12] ) - System
UsingDirective - (18:2,1 [32] ) - System.Collections.Generic
UsingDirective - (53:3,1 [17] ) - System.Linq
UsingDirective - (73:4,1 [28] ) - System.Threading.Tasks
UsingDirective - (104:5,1 [37] ) - Microsoft.AspNetCore.Components
ClassDeclaration - - public - TestComponent - Microsoft.AspNetCore.Components.ComponentBase -
DesignTimeDirective -
CSharpCode -
IntermediateToken - - CSharp - #pragma warning disable 0414
CSharpCode -
IntermediateToken - - CSharp - private static System.Object __o = null;
CSharpCode -
IntermediateToken - - CSharp - #pragma warning restore 0414
MethodDeclaration - - protected override - void - BuildRenderTree
Component - (0:0,0 [140] x:\dir\subdir\Test\TestComponent.cshtml) - MyComponent
ComponentChildContent - (32:1,2 [53] x:\dir\subdir\Test\TestComponent.cshtml) - GenericFragment - context
CSharpExpression - (50:1,20 [17] x:\dir\subdir\Test\TestComponent.cshtml)
IntermediateToken - (50:1,20 [17] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - context.ToLower()
ComponentChildContent - (89:2,2 [35] x:\dir\subdir\Test\TestComponent.cshtml) - IntFragment - context
CSharpExpression - (103:2,16 [7] x:\dir\subdir\Test\TestComponent.cshtml)
IntermediateToken - (103:2,16 [7] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - context
ComponentAttribute - (19:0,19 [7] x:\dir\subdir\Test\TestComponent.cshtml) - Item - AttributeStructure.DoubleQuotes
CSharpExpression - (20:0,20 [6] x:\dir\subdir\Test\TestComponent.cshtml)
IntermediateToken - (21:0,21 [4] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - "hi"
NamespaceDeclaration - - __Blazor.Test.TestComponent
ClassDeclaration - - internal static - TypeInference - -
ComponentTypeInferenceMethod - - __Blazor.Test.TestComponent.TypeInference - CreateMyComponent_0
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Source Location: (21:0,21 [4] x:\dir\subdir\Test\TestComponent.cshtml)
|"hi"|
Generated Location: (957:25,21 [4] )
|"hi"|

Source Location: (50:1,20 [17] x:\dir\subdir\Test\TestComponent.cshtml)
|context.ToLower()|
Generated Location: (1151:33,20 [17] )
|context.ToLower()|

Source Location: (103:2,16 [7] x:\dir\subdir\Test\TestComponent.cshtml)
|context|
Generated Location: (1370:42,16 [7] )
|context|

Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// <auto-generated/>
#pragma warning disable 1591
namespace Test
{
#line hidden
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
public class TestComponent : Microsoft.AspNetCore.Components.ComponentBase
{
#pragma warning disable 1998
protected override void BuildRenderTree(Microsoft.AspNetCore.Components.RenderTree.RenderTreeBuilder builder)
{
__Blazor.Test.TestComponent.TypeInference.CreateMyComponent_0(builder, 0, 1,
#nullable restore
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
"hi"

#line default
#line hidden
#nullable disable
, 2, (context) => (builder2) => {
builder2.AddContent(3,
#nullable restore
#line 2 "x:\dir\subdir\Test\TestComponent.cshtml"
context.ToLower()
#line default
#line hidden
#nullable disable
);
}
, 4, (context) => (builder2) => {
builder2.AddContent(5,
#nullable restore
#line 3 "x:\dir\subdir\Test\TestComponent.cshtml"
context
#line default
#line hidden
#nullable disable
);
}
);
}
#pragma warning restore 1998
}
}
namespace __Blazor.Test.TestComponent
{
#line hidden
internal static class TypeInference
{
public static void CreateMyComponent_0<TItem>(global::Microsoft.AspNetCore.Components.RenderTree.RenderTreeBuilder builder, int seq, int __seq0, TItem __arg0, int __seq1, global::Microsoft.AspNetCore.Components.RenderFragment<TItem> __arg1, int __seq2, global::Microsoft.AspNetCore.Components.RenderFragment<System.Int32> __arg2)
{
builder.OpenComponent<global::Test.MyComponent<TItem>>(seq);
builder.AddAttribute(__seq0, "Item", __arg0);
builder.AddAttribute(__seq1, "GenericFragment", __arg1);
builder.AddAttribute(__seq2, "IntFragment", __arg2);
builder.CloseComponent();
}
}
}
#pragma warning restore 1591
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Document -
NamespaceDeclaration - - Test
UsingDirective - (3:1,1 [14] ) - System
UsingDirective - (18:2,1 [34] ) - System.Collections.Generic
UsingDirective - (53:3,1 [19] ) - System.Linq
UsingDirective - (73:4,1 [30] ) - System.Threading.Tasks
UsingDirective - (104:5,1 [39] ) - Microsoft.AspNetCore.Components
ClassDeclaration - - public - TestComponent - Microsoft.AspNetCore.Components.ComponentBase -
MethodDeclaration - - protected override - void - BuildRenderTree
Component - (0:0,0 [140] x:\dir\subdir\Test\TestComponent.cshtml) - MyComponent
ComponentChildContent - (32:1,2 [53] x:\dir\subdir\Test\TestComponent.cshtml) - GenericFragment - context
CSharpExpression - (50:1,20 [17] x:\dir\subdir\Test\TestComponent.cshtml)
IntermediateToken - (50:1,20 [17] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - context.ToLower()
ComponentChildContent - (89:2,2 [35] x:\dir\subdir\Test\TestComponent.cshtml) - IntFragment - context
CSharpExpression - (103:2,16 [7] x:\dir\subdir\Test\TestComponent.cshtml)
IntermediateToken - (103:2,16 [7] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - context
ComponentAttribute - (19:0,19 [7] x:\dir\subdir\Test\TestComponent.cshtml) - Item - AttributeStructure.DoubleQuotes
CSharpExpression - (20:0,20 [6] x:\dir\subdir\Test\TestComponent.cshtml)
IntermediateToken - (21:0,21 [4] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - "hi"
NamespaceDeclaration - - __Blazor.Test.TestComponent
ClassDeclaration - - internal static - TypeInference - -
ComponentTypeInferenceMethod - - __Blazor.Test.TestComponent.TypeInference - CreateMyComponent_0

0 comments on commit e622d7c

Please sign in to comment.