Skip to content

feat: register JSON contexts and accept JsonTypeInfo parameters - #2333

Merged
ChrisPulman merged 1 commit into
mainfrom
feat/json-context-registration
Sep 21, 2026
Merged

ChrisPulman merged 1 commit into
mainfrom
feat/json-context-registration

Conversation

@glennawatson

Copy link
Copy Markdown
Contributor

Summary

Generated Refit clients take a source-generated JSON context, and methods can take JsonTypeInfo<T> parameters, so a first Native AOT client needs no hand-built JSON options.

  • A context registers on the client entry points. RestService.ForGenerated<T>, AddRefitGeneratedClient<T> and AddKeyedRefitGeneratedClient<T> take a JsonSerializerContext, with settings and allowReflectionFallback variants.
  • Reflection-based JSON is off by default. A type the context does not list throws NotSupportedException, and allowReflectionFallback: true opts in to reflection.
  • Your serializer settings win. Settings you pass keep their naming, converters, resolvers and polymorphism registrations while the context supplies metadata. With no settings, the context's own options apply.
  • The reflection APIs get the same helpers. RefitSettings.ForJsonContext, RefitSettings.UseJsonContext, SystemTextJsonContentSerializer.ForContext and WithContext work with RestService.For<T> and AddRefitClient<T>.
  • A method can take JsonTypeInfo<T> parameters. The generator matches each one by T to the JSON body or the reply, for Task<T>, ApiResponse<T>, IAsyncEnumerable<T>, IObservable<T> and paged replies.
  • RF014 reports a parameter that cannot be used. The reflection request builder throws for such a parameter instead of sending it as a body or query value.
  • IJsonTypeInfoContentSerializer adds metadata overloads. SystemTextJsonContentSerializer implements it for bodies, replies, buffered text and framed streams.
  • Runnable examples cover the workflow. An orders workload shows each way to set up JSON, including building your own options, and the Native AOT smoke test creates its client from a context.

Why

Getting a first Native AOT client right meant building JsonSerializerOptions, a TypeInfoResolver, a serializer and settings by hand. A context declared without the web defaults also reads camelCase replies as empty objects and reports no error. No issue tracks this.

Breaking changes

None. Every addition is source compatible. The two RestService.ForGenerated<T>(..., RefitSettings) overloads carry OverloadResolutionPriority, so ForGenerated<T>(client, new()) binds to the settings overload, and SystemTextJsonContentSerializer implements one more interface.

How this was verified

Tests cover the runtime, the generator diagnostics, emission and snapshots, and generated clients run against a stub handler. The Native AOT smoke test and the documentation client were published with dotnet publish -r linux-x64 and run.

Notes for the reviewer

  • Start at Parser.Request.Parameters.cs and GeneratedRequestRunner.JsonTypeInfo.cs for the parameter support, and SystemTextJsonContentSerializer.JsonContext.cs for how options and contexts compose. That file is the subtle part.
  • The examples under src/examples/Documentation/JsonContexts, the snapshots and the public API baselines are mechanical to skim.
  • The context and metadata APIs exist on .NET 8 and later only. The .NET Framework targets are unchanged.
  • The generator emits no [JsonSerializable] entries, because one JSON source generator cannot read another's output.
  • CLAUDE.md carries the design notes. The website pages are in a companion pull request in reactiveui/website.

Checklist

  • I have read the Contribute guide
  • The PR title follows Conventional Commits
  • Tests cover this change, or the summary says why they do not
  • New or changed public API has XML documentation

- ForGenerated, AddRefitGeneratedClient and the keyed variants take a JsonSerializerContext. Reflection-based JSON is off unless allowReflectionFallback is true.
- Settings you pass keep their naming, converters, resolvers and polymorphism registrations. With none, the context's own options apply.
- RefitSettings.ForJsonContext, RefitSettings.UseJsonContext, SystemTextJsonContentSerializer.ForContext and WithContext serve the reflection APIs too.
- A generated method can take JsonTypeInfo<T> parameters for its JSON body and its reply. RF014 reports a parameter that cannot be used, and the reflection request builder refuses one.
- IJsonTypeInfoContentSerializer adds metadata overloads to SystemTextJsonContentSerializer.
- Runnable order examples show each way to set up JSON, and the Native AOT smoke test creates its client from a context.
@codecov

codecov Bot commented Sep 21, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (aa139a2) to head (a654fb0).

Additional details and impacted files
@@            Coverage Diff             @@
##              main     #2333    +/-   ##
==========================================
  Coverage   100.00%   100.00%            
==========================================
  Files          206       211     +5     
  Lines        10805     11083   +278     
  Branches      2106      2154    +48     
==========================================
+ Hits         10805     11083   +278     

☔ 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.

@sonarqubecloud

Copy link
Copy Markdown

@ChrisPulman
ChrisPulman merged commit bb98726 into main Sep 21, 2026
21 checks passed
@ChrisPulman
ChrisPulman deleted the feat/json-context-registration branch September 21, 2026 03:21
@github-actions

github-actions Bot commented Oct 5, 2026

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 Oct 5, 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.

2 participants

⌂ 换网址