Skip to content

feat: improve generated clients for AOT#2151

Merged
glennawatson merged 18 commits into
mainfrom
source-gen-remaining-issues
Jun 21, 2026
Merged

feat: improve generated clients for AOT#2151
glennawatson merged 18 commits into
mainfrom
source-gen-remaining-issues

Conversation

@glennawatson

@glennawatson glennawatson commented Jun 20, 2026

Copy link
Copy Markdown
Contributor

What changed

  • Added RestService.ForGenerated(...) for applications that want to require a source-generated Refit client.
  • Added settings-based generated factories so fully generated clients can use HttpClient and RefitSettings without creating the reflection request builder.
  • Added generated request construction for URL-encoded bodies, including declared-type form property discovery for trimming and Native AOT.
  • Kept the generated request-builder path for interfaces with request shapes that still need runtime request-builder support.
  • Moved reusable Refit interface validation into analyzer and code-fix assemblies.
  • Added analyzer diagnostics for:
    • non-Refit members on Refit interfaces
    • route templates that use backslashes
    • multiple CancellationToken parameters
    • invalid [HeaderCollection] parameter types
  • Added code fixes for:
    • replacing route backslashes with forward slashes
    • changing invalid [HeaderCollection] parameter types to IDictionary<string, string>
  • Updated the source generator to use Roslyn incremental generator patterns for built-in Refit HTTP attributes.
  • Improved generated source so it follows the repository analyzer rules for documentation, visibility, nullable context, field ordering, and field naming.
  • Added generated-code compliance tests that compile real generated output with generated-code markers disabled.
  • Added C# 7.3 generated-source coverage, while enabling nullable annotations for generated code on C# 8 and newer.
  • Tightened trimming and Native AOT annotations so remaining warnings describe concrete runtime metadata or dynamic-code requirements.
  • Shared enum, reflected property, and string helpers across runtime request paths.
  • Updated the README with generated-only client creation, generated request building, Native AOT guidance, analyzer diagnostics, code fixes, and generated-code language support.

Compatibility

  • This targets the next major version.
  • RestService.For<T>(...) and AddRefitClient<T>() remain the normal entry points.
  • RestService.ForGenerated<T>(...) is the stricter entry point for applications that require generated clients.
  • Interfaces that cannot use settings-only generated factories still use the existing generated request-builder path.
  • Generated source is kept compatible with C# 7.3 syntax, with nullable annotations emitted when the consuming project supports them.

Issues

Closes #1345.
Closes #1142.
Closes #1475.

Validation

  • dotnet build "Refit/Refit.csproj" -f net8.0 -v:minimal --no-restore /p:NuGetAudit=false /m:1
  • dotnet build "Refit/Refit.csproj" -f net462 -v:minimal --no-restore /p:NuGetAudit=false /m:1
  • dotnet build "Refit/Refit.csproj" -f net10.0 -v:minimal --no-restore /p:NuGetAudit=false /m:1
  • dotnet build "Refit/Refit.csproj" -f net11.0 -v:minimal --no-restore /p:NuGetAudit=false /m:1
  • dotnet test "tests/Refit.Tests/Refit.Tests.csproj" -f net8.0 -v:minimal --no-restore /p:NuGetAudit=false
  • dotnet build "tests/Refit.GeneratorTests/Refit.GeneratorTests.csproj" -f net8.0 -v:minimal --no-restore /p:NuGetAudit=false /m:1
  • dotnet run --project "tests/Refit.GeneratorTests/Refit.GeneratorTests.csproj" -f net8.0 --no-restore --no-build
  • dotnet publish "Refit.NativeAotSmoke/Refit.NativeAotSmoke.csproj" -c Release -r linux-x64 --self-contained true /p:NuGetAudit=false /p:ContinuousIntegrationBuild=true
  • ./Refit.NativeAotSmoke/bin/Release/net8.0/linux-x64/publish/Refit.NativeAotSmoke
  • git diff --check

Checklist

@codecov

codecov Bot commented Jun 20, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.27728% with 21 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.56%. Comparing base (e28a384) to head (c98c2ea).

Files with missing lines Patch % Lines
...c/Refit.Analyzers.Shared/RefitInterfaceAnalyzer.cs 95.62% 0 Missing and 6 partials ⚠️
src/InterfaceStubGenerator.Shared/Emitter.cs 99.62% 0 Missing and 2 partials ⚠️
...ceStubGenerator.Shared/InterfaceStubGeneratorV2.cs 97.80% 0 Missing and 2 partials ⚠️
src/Refit/EnumHelpers.cs 96.61% 0 Missing and 2 partials ⚠️
src/Refit/StringHelpers.cs 87.50% 2 Missing ⚠️
src/Refit/ValidationApiException.cs 96.72% 0 Missing and 2 partials ⚠️
src/InterfaceStubGenerator.Shared/Parser.cs 97.43% 0 Missing and 1 partial ⚠️
....CodeFixes.Shared/RefitInterfaceCodeFixProvider.cs 98.21% 0 Missing and 1 partial ⚠️
src/Refit/ReflectionPropertyHelpers.cs 92.85% 0 Missing and 1 partial ⚠️
...it/RequestBuilderImplementation.QueryAndHeaders.cs 75.00% 0 Missing and 1 partial ⚠️
... and 1 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2151      +/-   ##
==========================================
+ Coverage   97.49%   97.56%   +0.06%     
==========================================
  Files         105      112       +7     
  Lines        4711     5334     +623     
  Branches      845      961     +116     
==========================================
+ Hits         4593     5204     +611     
  Misses         31       31              
- Partials       87       99      +12     

☔ 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 changed the title feat: add AOT-aware generated API paths feat: add AOT-aware generated client paths Jun 20, 2026
@glennawatson glennawatson changed the title feat: add AOT-aware generated client paths feat: improve AOT source-generated clients Jun 20, 2026
@glennawatson glennawatson changed the title feat: improve AOT source-generated clients feat: add AOT generated clients Jun 20, 2026
- Add generated URL-encoded body handling that preserves declared form types for AOT.

- Compile generated source against repository analyzer rules and older C# syntax expectations.

- Share enum, property, and string helpers across runtime request paths.

- Extend native AOT and runtime tests for generated request construction.
@glennawatson glennawatson changed the title feat: add AOT generated clients feat: improve generated clients for AOT Jun 20, 2026
@glennawatson glennawatson marked this pull request as ready for review June 20, 2026 23:13
- Keep the substring fallback for targets without the span URI escaping overload.

- Limit the suppression to the helper method that selects the target-specific path.
- Make generated infrastructure field names unique against interface members.

- Cover collisions with generated request-builder, settings, and type-parameter fields.
- Add problem-details coverage for validation errors and extension values.

- Cover enum backing-type helpers and generated-only RestService overloads.
- Add focused coverage for generator helpers, analyzer exits, and code-fix paths.

- Cover the new generic enum and request-builder fallbacks without reflection-only tests.
- Use JsonElement parsing APIs where modern targets support them.

- Reject duplicate JSON properties in default System.Text.Json options on .NET 10+.

- Include the generator test accessors required by the coverage tests.
- Track the attribute state directly when parsing authorization and body parameters.

- This keeps the same validation behavior while satisfying constant-condition analysis.
@sonarqubecloud

Copy link
Copy Markdown

@glennawatson glennawatson merged commit bf488d6 into main Jun 21, 2026
13 checks passed
@glennawatson glennawatson deleted the source-gen-remaining-issues branch June 21, 2026 02:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants