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

Ignore [FromServices] parameters when generating resolved types #289

Open
colinblaise opened this issue Jan 9, 2025 · 2 comments
Open

Comments

@colinblaise
Copy link

First, much love for this project!

I have hundreds of warnings when generating my typescript for the "Could not find suitable TypeScript type..." that are all coming from parameters using the [FromServices] attribute. Is there a built-in way for the MethodCodeGenerator.GenerateNode to ignore parameters with that attribute?

@pavel-b-novikov
Copy link
Member

pavel-b-novikov commented Jan 9, 2025

Hello and thank you for your words :)

You can use <RtSuppress> parameter in Reinforced.Typings.settings.xml to suppress unwanted warnings

<RtSuppress>RTW0013;RTW0014</RtSuppress>

@colinblaise
Copy link
Author

The warning is actually very useful and I don't want to suppress that type of warning outright. I just want to suppress the warnings for parameters that use the [FromServices] attribute since those parameters are completely excluded from our generated typescript.

I implemented a very hacky but working solution in the meantime.

    // Use reflection to modify the type resolver's private _resolveCache dictionary to
    // include the [FromService] parameters so that reinforced types doesn't complain
    // about not being able to find a suitable type.
    var fromServicesParams = element.GetParameters()
      .Where(p => p.GetCustomAttribute(typeof(FromServicesAttribute)) != null)
      .ToList();
    var resolveCache = typeof(TypeResolver).GetField("_resolveCache", BindingFlags.NonPublic | BindingFlags.Instance);
    var dict = (Dictionary<Type, RtTypeName>?)resolveCache?.GetValue(resolver);
    if (dict != null)
    {
      foreach (var p in fromServicesParams)
      {
        dict[p.ParameterType] = new RtSimpleTypeName("any");
      }
    }

    result = base.GenerateNode(element, result, resolver);

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

No branches or pull requests

2 participants