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

View component tag helpers don't allow generic parameters #5216

Closed
cjqian opened this issue Sep 1, 2016 · 12 comments
Closed

View component tag helpers don't allow generic parameters #5216

cjqian opened this issue Sep 1, 2016 · 12 comments
Assignees
Milestone

Comments

@cjqian
Copy link
Contributor

cjqian commented Sep 1, 2016

View components that are invoked with generic parameters will not be translated into view component tag helpers correctly.
#1051

@NTaylorMullen
Copy link
Contributor

This should be done before #5207

@danroth27 danroth27 added this to the 1.1.0 milestone Sep 7, 2016
@Eilon
Copy link
Member

Eilon commented Oct 6, 2016

@NTaylorMullen @ajaybhargavb can you describe here what the scenario is? What is a generic parameter in this context?

@cjqian
Copy link
Contributor Author

cjqian commented Oct 6, 2016

Say you have a view component Foo with an invoke method that takes in something like a Dictionary<int, string> bar. In the current implementation, if you try to use a view component in your .cshtml file like <vc:foo bar=..., I'm not confident that it will get processed correctly.

@NTaylorMullen
Copy link
Contributor

Scenario:

Having a ViewComponent with a generic parameter in its Invoke/InvokeAsync results in broken C# being generated by Razor; resulting in a compilation error when trying to serve a view.

Reason

We generate a TagHelperDescriptors based on a ViewComponents Invoke/InvokeAsync method. Razor then consumes these TagHelperDescriptors to render appropriate code for rendering a TagHelper. In order for that piece to work we must generate an actual TagHelper class based off of the TagHelperDescriptor to work at runtime. This means generating public __TypeName__ __PropertyName { get; set;} within an actual TagHelper where the type name and property name are represented as strings in a TagHelperAttributeDescriptor. Today we construct a TagHelperAttributeDescriptor by saying: TagHelperAttributeDescriptor.TypeName = ViewComponent.Parameter[x].GetType().FullName;. The code gen then directly consumes the attribute descriptors type name to generate the get; set; property. Therefore, for generic types the .FullName would result in a back tick being generated.

@Eilon
Copy link
Member

Eilon commented Oct 6, 2016

Got it, thanks!

@dougbu
Copy link
Member

dougbu commented Oct 7, 2016

@NTaylorMullen does DefaultViewComponentHelper have any issues w/ generic view component types? I ask because no tests cover the scenario. That lack makes it less clear whether generic view component support was intended (anywhere).

@Eilon
Copy link
Member

Eilon commented Oct 7, 2016

What exactly is a generic view component? I thought view components by definition need to be concrete, no? How else could you invoke one?

@NTaylorMullen
Copy link
Contributor

@dougbu I gave it a shot and it doesn't look like it can support generic ViewComponents, I've filed an issue. That aside however, this issue is more geared towards non generic ViewComponents where a generic parameter, lets say List<string> causes ViewComponentTagHelpers to fail. Ex:

public class XViewComponent
{
    public string Invoke(List<string> values)
    {
        return string.Join(", ", values);
    }
}

@dougbu
Copy link
Member

dougbu commented Oct 7, 2016

@Eilon I should have said "ViewComponent with a generic Invoke[Async]() argument". But @NTaylorMullen thanks for checking on a "ViewComponent that is an open generic type".

@dougbu
Copy link
Member

dougbu commented Oct 7, 2016

Back to this issue, @NTaylorMullen are you looking to support Invoke<T>(List<T> value) or other open generic methods as well? Or is this issue about only parameters of closed generic types in regular 'ole methods?

@NTaylorMullen
Copy link
Contributor

@dougbu Oh I wouldn't expect that to work because that'd expect the ViewComponent invocation system to map parameters provided and then back-map said parameter to the InvokeAsync call. When you try it in practice you get the following:

image

@rynowak
Copy link
Member

rynowak commented Oct 19, 2016

Suggestion: When we port TagHelper discovery to use metadata/ISymbol instead of reflection, we just fix this there. ISymbol (Roslyn) already does the correct formatting of type names for code generation

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants