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

Add analyzer for custom IProblemDetailsWriter registration #49286

Open
wants to merge 24 commits into
base: main
Choose a base branch
from

Conversation

david-acker
Copy link
Member

@david-acker david-acker commented Jul 10, 2023

Add analyzer for custom IProblemDetailsWriter registration

Adds an analyzer that checks that custom IProblemDetailsWriters are registered in the correct order, relative to MvcServiceCollectionExtensions method calls.

Description

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

This analyzer reports 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).

Diagnostic

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

Code Examples:

Diagnostic Reported:

services.AddControllers();

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

No Diagnostic Reported:

services.AddTransient<IProblemDetailsWriter, SampleProblemDetailsWriter>();

services.AddControllers();

Analyzer Infrastructure Changes

  • Brings over StartupAnalyzer-related infrastructure from the legacy Microsoft.AspNetCore.Analyzers project

Fixes #48180

@ghost ghost added the community-contribution Indicates that the PR has been added by a community member label Jul 10, 2023
@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically label Jul 10, 2023
@ghost
Copy link

ghost commented Jul 10, 2023

Thanks for your PR, @david-acker. Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@david-acker david-acker added area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates area-web-frameworks *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels and removed needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically labels Jul 10, 2023
@david-acker david-acker added the analyzer Indicates an issue which is related to analyzer experience label Oct 25, 2023
@adityamandaleeka
Copy link
Member

@david-acker, Are you still working on this one? No pressure, just asking because it's been sitting in draft state for a while.

@david-acker
Copy link
Member Author

I'm planning on working on this again fairly soon now that the API has finally approved.

@david-acker david-acker marked this pull request as ready for review February 10, 2024 04:26
@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 17, 2024
@adityamandaleeka
Copy link
Member

@captainsafia PTAL

@captainsafia
Copy link
Member

/azp run

@dotnet-policy-service dotnet-policy-service bot removed the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Mar 4, 2024
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@captainsafia
Copy link
Member

@david-acker I saw you posted a question in the original issue about moving StartupAnalyzer-related infrastructure to the analyzers that get shipped in the refpack. I'm in favor of incorproating a StartupAnalyzer in that project and using source-sharing to bring in helpful dependencies like StartupFacts.

@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 Mar 12, 2024
@david-acker david-acker marked this pull request as draft March 13, 2024 05:34
@captainsafia captainsafia added area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc and removed area-web-frameworks *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels labels Aug 22, 2024
@david-acker david-acker marked this pull request as ready for review September 2, 2024 02:02
@david-acker david-acker removed the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Sep 2, 2024
Copy link
Contributor

Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime.
To make sure no conflicting changes have occurred, please rerun validation before merging. You can do this by leaving an /azp run comment here (requires commit rights), or by simply closing and reopening.

@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 Sep 10, 2024
@mkArtakMSFT mkArtakMSFT removed the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Sep 16, 2024
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 area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc community-contribution Indicates that the PR has been added by a community member pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Analyzer] : Warn when registering custom IProblemDetailsWriter after calling AddRazorPages, etc
4 participants