Skip to content

Support conditional expressions in API1000/API1001 analyzers#65893

Open
luissena wants to merge 1 commit intodotnet:mainfrom
luissena:fix/api-analyzers-conditional-expression-support
Open

Support conditional expressions in API1000/API1001 analyzers#65893
luissena wants to merge 1 commit intodotnet:mainfrom
luissena:fix/api-analyzers-conditional-expression-support

Conversation

@luissena
Copy link

Summary

Fixes #33105

The API convention analyzers (API1000 and API1001) did not inspect conditional (ternary) expressions, causing undocumented status codes to go undetected in return statements like:

return id == 0 ? NotFound() : Ok(new object());

While if/else and switch expressions were already handled, conditional expressions were silently skipped.

Changes

  • Extended InspectReturnOperation in ActualApiResponseMetadataFactory to recurse into both branches (WhenTrue/WhenFalse) of IConditionalOperation, following the same pattern already used for ISwitchExpressionOperation
  • Generalized the ISwitchExpressionArmOperation? parameter to IOperation? so both switch arm values and conditional branch values flow through the same code path
  • Updated the existing test that documented this gap (NoDiagnosticsAreReturned_ForApiController_IfStatusCodesCannotBeInferred) — now that conditionals are inspected, the analyzer correctly reports undocumented status codes
  • Added unit tests for conditional and nested conditional expressions
  • Added integration tests for conditional returns, nested conditionals, expression-bodied members, and the all-documented-codes no-diagnostic case

Test plan

  • All 215 existing + new analyzer tests pass
  • Conditional expressions (a ? b : c) correctly produce API1000 diagnostics for undocumented status codes
  • Nested conditionals (a ? b : c ? d : e) correctly produce diagnostics for all branches
  • Expression-bodied members with conditionals are handled
  • No diagnostics when all conditional branch status codes are documented
  • No regressions in existing if/else, switch, and switch expression test coverage

The API convention analyzers (API1000 and API1001) did not inspect
conditional (ternary) expressions, so undocumented status codes in
returns like `return id == 0 ? NotFound() : Ok()` went undetected.

This change extends `InspectReturnOperation` in
`ActualApiResponseMetadataFactory` to recurse into both branches of
`IConditionalOperation`, matching the existing pattern used for
`ISwitchExpressionOperation`.

The `ISwitchExpressionArmOperation?` parameter was generalized to
`IOperation?` to support passing branch values from both switch arms
and conditional expressions through the same code path.

Fixes dotnet#33105
@luissena luissena requested a review from a team as a code owner March 20, 2026 22:26
@github-actions github-actions bot added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Mar 20, 2026
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Mar 20, 2026
@dotnet-policy-service
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Analyzers API1000 and API1001 should trigger on conditional returns

1 participant