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

[Analyzer] : Warn when registering custom IProblemDetailsWriter after calling AddRazorPages, etc #48180

Open
2 of 15 tasks
Rick-Anderson opened this issue May 10, 2023 · 11 comments · May be fixed by #49286
Open
2 of 15 tasks
Labels
analyzer Indicates an issue which is related to analyzer experience api-approved API was approved in API review, it can be implemented area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates bug This issue describes a behavior which is not expected - a bug. help candidate Indicates that the issues may be a good fit for community to help with. Requires work from eng. team

Comments

@Rick-Anderson
Copy link
Contributor

Rick-Anderson commented May 10, 2023

Background and Motivation

Registering a custom IProblemDetailsWriter after calling AddRazorPages, etc results in the DefaultProblemDetailsWriter, not the custom IProblemDetailsWriter being called.

Proposed Analyzer

Analyzer Behavior and Message

Issue a warning when Registering a custom IProblemDetailsWriter after calling AddRazorPages, etc.

ASP0001: Authorization middleware is incorrectly configured is similar.

See dotnet/AspNetCore.Docs#29152

Note: When using a custom IProblemDetailsWriter, the custom IProblemDetailsWriter must be registered before calling AddRazorPages, AddControllers, or AddControllersWithViews.

Category

  • Design
  • Documentation
  • Globalization
  • Interoperability
  • Maintainability
  • Naming
  • Performance
  • Reliability
  • Security
  • Style
  • Usage

Severity Level

  • Error
  • Warning
  • Info
  • Hidden

Usage Scenarios

Issue a warning when Registering a custom IProblemDetailsWriter after calling AddRazorPages, etc.

Risks

None.

@Rick-Anderson Rick-Anderson added api-suggestion Early API idea and discussion, it is NOT ready for implementation analyzer Indicates an issue which is related to analyzer experience labels May 10, 2023
@dotnet-issue-labeler dotnet-issue-labeler bot added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label May 10, 2023
@mkArtakMSFT mkArtakMSFT added this to the .NET 8 Planning milestone May 17, 2023
@mkArtakMSFT mkArtakMSFT added the bug This issue describes a behavior which is not expected - a bug. label May 17, 2023
@ghost
Copy link

ghost commented May 17, 2023

Thanks for contacting us.

We're moving this issue to the .NET 8 Planning milestone for future evaluation / consideration. We would like to keep this around to collect more feedback, which can help us with prioritizing this work. We will re-evaluate this issue, during our next planning meeting(s).
If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues.
To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

@captainsafia captainsafia added the help wanted Up for grabs. We would accept a PR to help resolve this issue label May 18, 2023
@david-acker
Copy link
Member

We could add this new analyzer to the Microsoft.AspNetCore.Analyzers project in src/Analyzers since a similar middleware analyzer (UseAuthorizationAnalyzer) for ASP0001 lives there. Then we can leverage the existing middleware information that's available on the StartupAnalysis context and then register the new analyzer alongside the others StartupAnalyzer.

@david-acker
Copy link
Member

david-acker commented May 25, 2023

Here's a simple suggestion for the title and message of the new diagnostic:

Title: Custom IProblemDetailsWriter is incorrectly configured
Message: The custom IProblemDetailsWriter must be registered before calling AddControllers, AddControllersWithViews, AddMvc, or AddRazorPages.

@david-acker
Copy link
Member

I'd be happy to put up a PR for this once the API for the new diagnostic is approved.

@JamesNK JamesNK added api-suggestion Early API idea and discussion, it is NOT ready for implementation and removed api-suggestion Early API idea and discussion, it is NOT ready for implementation labels May 25, 2023
@david-acker
Copy link
Member

david-acker commented Jul 9, 2023

We'll want to report this diagnostic for AddMvc as well, since it calls AddControllersWithViews and AddRazorPages internally.

@david-acker
Copy link
Member

david-acker commented Jul 21, 2023

API for proposed analyzer

Diagnostic:

Title: Custom IProblemDetailsWriter is incorrectly configured
Message: The custom IProblemDetailsWriter must be registered before calling AddControllers, AddControllersWithViews, AddMvc, or AddRazorPages.
Category: Usage
Severity: Warning

Context:

If a custom IProblemDetailsWriter is registered after a MvcServiceCollectionExtensions method, the DefaultProblemDetailsWriter will be used rather of the custom IProblemDetailsWriter.

This analyzer would report a warning level diagnostic when an IProblemDetailsWriter is registered (using AddTransient, AddScoped, or AddSingleton) and appears after a call to a MvcServiceCollectionExtensions method (AddControllers, AddControllersWithViews, AddMvc, or AddRazorPages).

Example Usage:

Diagnostic Reported:

services.AddControllers();

// Diagnostic reported on method invocation below
services.AddTransient<IProblemDetailsWriter, SampleProblemDetailsWriter>();

No Diagnostic Reported:

services.AddTransient<IProblemDetailsWriter, SampleProblemDetailsWriter>();

services.AddControllers();

@david-acker david-acker added api-ready-for-review API is ready for formal API review - https://github.com/dotnet/apireviews and removed api-suggestion Early API idea and discussion, it is NOT ready for implementation labels Jul 21, 2023
@ghost
Copy link

ghost commented Jul 21, 2023

Thank you for submitting this for API review. This will be reviewed by @dotnet/aspnet-api-review at the next meeting of the ASP.NET Core API Review group. Please ensure you take a look at the API review process documentation and ensure that:

  • The PR contains changes to the reference-assembly that describe the API change. Or, you have included a snippet of reference-assembly-style code that illustrates the API change.
  • The PR describes the impact to users, both positive (useful new APIs) and negative (breaking changes).
  • Someone is assigned to "champion" this change in the meeting, and they understand the impact and design of the change.

@mkArtakMSFT mkArtakMSFT added help candidate Indicates that the issues may be a good fit for community to help with. Requires work from eng. team and removed help wanted Up for grabs. We would accept a PR to help resolve this issue labels Oct 28, 2023
@halter73
Copy link
Member

API Review Notes:

  • We discussed whether there's something clever we can do at runtime to avoid these errors like only adding default writers if there is no IProblemDetailsWriter configured and/or looping over the writers backwards, but we think adding complication is worth it.
  • We also discussed using a different service type to mark the "default" writer so it would never unintendedly be used when a custom writer is provided.
  • A runtime warning if there's a writer registered after the default may be more reliable since it could catch things a static analyzer cannot.
  • Would there be a code fix to move the services.AddTransient<IProblemDetailsWriter,... up?
    • That seems useful.
  • Note: We only want this to warn if someone calls AddControllers, AddControllersWithViews, AddMvc, or AddRazorPages, not if they add multiple IProblemDetailsWriter's manually even if that includes the default.

While all the runtime ideas seem good, it doesn't negate the fact that the analyzer would also be helpful.

API Approved!

Diagnostic:
Title: Custom IProblemDetailsWriter is incorrectly configured
Message: The custom IProblemDetailsWriter must be registered before calling AddControllers, AddControllersWithViews, AddMvc, or AddRazorPages.
Category: Usage
Severity: Warning

@halter73 halter73 added api-approved API was approved in API review, it can be implemented and removed api-ready-for-review API is ready for formal API review - https://github.com/dotnet/apireviews labels Jan 18, 2024
@dotnet-policy-service dotnet-policy-service bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Feb 6, 2024
@wtgodbe wtgodbe removed the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Feb 6, 2024
@dotnet-policy-service dotnet-policy-service bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Feb 6, 2024
@david-acker
Copy link
Member

david-acker commented Feb 9, 2024

@halter73 I can add a code fix for moving the IProblemDetailsWriter registration up. Should we add the code fixes to the existing analyzers project, like we do with Mvc.Api.Analyzers, or create a separate project for them? Microsoft.AspNetCore.Analyzers doesn't currently have any code fixes associated with it.

@david-acker david-acker removed the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Feb 9, 2024
@halter73
Copy link
Member

Thanks for offering to do the code fix @david-acker! New analyzers should go into src/Framework/AspNetCoreAnalyzers which includes both Microsoft.AspNetCore.App.Analyzers and Microsoft.AspNetCore.App.CodeFixes projects.

We prefer that over the legacy Microsoft.AspNetCore.Analyzers project in /src/Analyzers because the "App" analyzers are included in the Microsoft.AspNetCore.App.Ref pack which stays aligned with specific versions of ASP.NET Core while the legacy project is shipped as part of the Microsoft.Net.Sdk.Web SDK and run against arbitrary versions.

@david-acker
Copy link
Member

Sounds good! I'll move the new analyzer over to AspNetCoreAnalyzers in the PR that I have up. Originally, I added it to the legacy analyzers project in order to leverage the StartupAnalyzer that's included there.

Do we want to clone some of the StartupAnalyzer infrastructure that it's using (minus the analyzers themselves) into AspNetCoreAnalyzers while we're at it, or should I just update the new analyzer to use something ad hoc?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analyzer Indicates an issue which is related to analyzer experience api-approved API was approved in API review, it can be implemented area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates bug This issue describes a behavior which is not expected - a bug. help candidate Indicates that the issues may be a good fit for community to help with. Requires work from eng. team
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants