Skip to content

feat: add analyzer diagnostics for invalid Refit interfaces - #2220

Merged
ChrisPulman merged 1 commit into
mainfrom
feat/analyzer-invalid-interface-diagnostics
Jul 14, 2026
Merged

ChrisPulman merged 1 commit into
mainfrom
feat/analyzer-invalid-interface-diagnostics

Conversation

@glennawatson

@glennawatson glennawatson commented Jul 14, 2026 •

Copy link
Copy Markdown
Contributor

What kind of change does this PR introduce?

Feature - two new Refit interface analyzer diagnostics (no runtime behavior change).

What is the new behavior?

The Refit analyzer now flags two more invalid interface shapes at compile time:

  • RF011 - a Refit method that declares more than one [Body] parameter. Message: Method {0}.{1} has more than one Body parameter.
  • RF012 - a [Multipart] method that also declares a [Body] parameter. Message: Method {0}.{1} is multipart and cannot declare a Body parameter.

Both are Warning severity, so they surface the mistake in the editor and build output without failing the build unless the consumer opts into treat-warnings-as-errors. Both are documented in the README analyzer list.

What is the current behavior?

These interface mistakes were not diagnosed. A method with two [Body] parameters, or a multipart method that also carries a [Body] parameter, was only caught (if at all) at request-build time, and produced no analyzer signal.

Closes #1885

What might this PR break?

Nothing by default. The new diagnostics are Warning severity and add no public API (the descriptors and IDs are internal). Consumers who run treat-warnings-as-errors and already have one of these invalid shapes will see a new build warning surface as an error - which is the intended signal.

Checklist

  • I have read the Contribute guide
  • Tests have been added or updated (for bug fixes / features)
  • Docs have been added or updated (for bug fixes / features)
  • Changes target the main branch
  • PR title follows Conventional Commits

Additional information

  • Both diagnostics live in the analyzer project src/Refit.Analyzers.Shared, mirroring the existing per-method / per-parameter diagnostics (RF004, RF008, RF009). RF011 slots into the parameter-shape counter loop next to the existing CancellationToken / HeaderCollection / Authorize counters; RF012 is a pure attribute check on the method.
  • All builds pass (analyzer projects + code-fix projects, Roslyn 4.8 and 5.0); all tests pass (RefitInterfaceAnalyzerTests, net8.0). No public API changes (internal descriptors/IDs).
  • A third candidate, RF010 (unsupported return type), was evaluated and descoped. Refit supports runtime-registered IReturnTypeAdapter implementations (via RefitSettings.ReturnTypeAdapters) that can make an otherwise-unrecognized return type - even a closed adapter over string/int - valid at runtime. The analyzer cannot see runtime-registered adapters, and the source generator itself never rejects an unknown return type (it falls back to the reflection request builder, already surfaced by RF006). A return-type diagnostic would therefore risk false positives on valid adapter-backed methods, so it was left out.

- RF011: flag Refit methods that declare more than one [Body] parameter
- RF012: flag [Multipart] methods that also declare a [Body] parameter

Both are Warning severity, so existing builds are unaffected unless
warnings are treated as errors.

Closes #1885
@sonarqubecloud

Copy link
Copy Markdown

@codecov

codecov Bot commented Jul 14, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.70%. Comparing base (841ff03) to head (3d5d221).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2220   +/-   ##
=======================================
  Coverage   99.70%   99.70%           
=======================================
  Files         176      176           
  Lines        9136     9180   +44     
  Branches     1701     1710    +9     
=======================================
+ Hits         9109     9153   +44     
  Misses          8        8           
  Partials       19       19           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@glennawatson
glennawatson marked this pull request as ready for review July 14, 2026 14:43
@ChrisPulman
ChrisPulman merged commit a2b1581 into main Jul 14, 2026
14 checks passed
@ChrisPulman
ChrisPulman deleted the feat/analyzer-invalid-interface-diagnostics branch July 14, 2026 16:02
@github-actions

Copy link
Copy Markdown

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Jul 30, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refit should emit diagnostics for invalid interfaces

2 participants