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

Handle common web types #245

Open
JohanLarsson opened this issue Nov 1, 2020 · 1 comment
Open

Handle common web types #245

JohanLarsson opened this issue Nov 1, 2020 · 1 comment

Comments

@JohanLarsson
Copy link
Collaborator

I don't write much web so need help here. Stumbled on this https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpresponsemessage.ensuresuccessstatuscode?view=netcore-3.1#remarks

The EnsureSuccessStatusCode method throws an exception if the HTTP response was unsuccessful. In .NET Framework and .NET Core 2.2 and earlier versions, if the Content is not null, this method will also call Dispose to free managed and unmanaged resources. Starting with .NET Core 3.0, the content will not be disposed.

Analyzer have potential to be very useful for handling things like this.

@JohanLarsson
Copy link
Collaborator Author

JohanLarsson commented Nov 1, 2020

I'm adding support for framework types in this format:

                    IMethodSymbol { ContainingType: { MetadataName: "File" }, MetadataName: "OpenText" } => Result.Yes,
                    IMethodSymbol { ContainingType: { MetadataName: "File" }, MetadataName: "CreateText" } => Result.Yes,
                    IMethodSymbol { ContainingType: { MetadataName: "File" }, MetadataName: "AppendText" } => Result.Yes,
                    IMethodSymbol { ContainingType: { MetadataName: "File" }, MetadataName: "Copy" } => Result.No,
[Test]
public static void Dump()
{
    var set = new HashSet<string>();
    foreach (var method in typeof(System.Net.HttpWebResponse).GetMethods(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.DeclaredOnly).OrderBy(x => x.Name))
    {
        if (!method.IsSpecialName &&
            set.Add(method.Name))
        {
            Console.WriteLine($"                    IMethodSymbol {{ ContainingType: {{ MetadataName: \"{method.DeclaringType.Name}\" }}, MetadataName: \"{method.Name}\" }} => Result.{(typeof(IDisposable).IsAssignableFrom(method.ReturnType) ? "Yes" : "No")},");
        }
    }
}

Of course EnsureSuccessStatusCode above would need more handling and tests but it is pretty straightforward. The problem for me is what types to add support for and how.
My thinking is it is good if we support common types in the framework and open source nugets.

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

1 participant