Skip to content

feat: add per-type URL parameter formatter registry - #2226

Merged
ChrisPulman merged 4 commits into
mainfrom
feat/per-type-url-parameter-formatter
Jul 15, 2026
Merged

ChrisPulman merged 4 commits into
mainfrom
feat/per-type-url-parameter-formatter

Conversation

@glennawatson

@glennawatson glennawatson commented Jul 14, 2026 •

Copy link
Copy Markdown
Contributor

What kind of change does this PR introduce?

Feature (additive, opt-in) - a per-type URL parameter formatter registry on RefitSettings.

What is the new behavior?

  • New RefitSettings.UrlParameterFormatterMap - an IDictionary<Type, IUrlParameterFormatter> (empty by default) keyed by the CLR type it formats.
  • When a value is rendered into a URL (path parameter, round-trip {**param} segment, or query value), its runtime type (value.GetType()) is looked up in the map first. A registered formatter wins; every other type falls back to the existing UrlParameterFormatter.
  • Matching is by exact runtime type only - no base-class or interface walking - so you register the concrete type the value will have at runtime.
  • Resolution is centralized in one place (GeneratedRequestRunner.FormatUrlParameter) that BOTH request paths call, so the reflection request builder and the source-generated request builder produce identical URLs. AOT/trim-safe: value.GetType() only, no reflection over members.
  • Registering any entry opts a request out of the generator's inline formatting fast path (the value goes through the formatter, which consults the registry).
var settings = new RefitSettings();
settings.UrlParameterFormatterMap[typeof(Temperature)] = new TemperatureUrlParameterFormatter();

What is the current behavior?

Every path/query value is formatted through the single RefitSettings.UrlParameterFormatter (falling back to ToString()). To customize one specific type you must write a custom IUrlParameterFormatter that switches on value.GetType() for every call.

Closes #2219

What might this PR break?

Nothing for existing code. The default map is empty, which changes no behavior - FormatUrlParameter resolves to the configured UrlParameterFormatter exactly as before. The only signature change is the internal generator-support helper GeneratedRequestRunner.RoundTripEscapePath (marked [EditorBrowsable(Never)], only called by generated code), which now takes RefitSettings instead of IUrlParameterFormatter; generated output is regenerated to match.

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

  • Reflection and source-generated paths kept in parity via a single shared resolution helper; applies to path parameters, round-trip path segments, and query values; does not affect header/body serialization.
  • Tests added: registered type used for path and query values, unregistered types fall back to the default, and a generated-vs-reflection parity test.
  • Public API recorded (RefitSettings.UrlParameterFormatterMap, GeneratedRequestRunner.FormatUrlParameter); generator snapshots reviewed and updated (custom formatting branch now routes through FormatUrlParameter).
  • Documented in the README and docs/breaking-changes.md.
  • Full solution builds clean (0 warnings, analyzers satisfied); Refit.Tests (1018) and Refit.GeneratorTests (408) pass on net8.0.
  • Central resolver lives on GeneratedRequestRunner (the existing hidden, [EditorBrowsable(Never)] shared runtime-helper class) so the visible public surface added to RefitSettings is just the map. Registering a per-type formatter for the collection/round-trip cases applies per element/section by the element's runtime type, matching the reflection builder.

- Add `RefitSettings.UrlParameterFormatterMap`, a `Type`-keyed
  `IUrlParameterFormatter` registry consulted before `UrlParameterFormatter`
  when rendering path and query values (exact runtime type, default fallback).
- Centralize resolution in `GeneratedRequestRunner.FormatUrlParameter`, called
  by both the reflection request builder and the generated runtime helpers, so
  the two request paths stay in parity automatically.
- Route path, round-trip segment, query, dictionary-key and collection
  formatting through the registry; a populated map opts a request out of the
  generator's inline formatting fast path.
- Cover reflection, generated, parity and fallback behavior with tests; record
  the new public API and document the setting in the README and breaking-changes
  notes.

Closes #2219
@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 (14895a4) to head (ea3209d).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2226   +/-   ##
=======================================
  Coverage   99.70%   99.70%           
=======================================
  Files         177      177           
  Lines        9259     9275   +16     
  Branches     1728     1729    +1     
=======================================
+ Hits         9232     9248   +16     
  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
…ameter-formatter

# Conflicts:
#	docs/breaking-changes.md
…ameter-formatter

# Conflicts:
#	docs/breaking-changes.md
@sonarqubecloud

Copy link
Copy Markdown

@ChrisPulman
ChrisPulman merged commit 37efb4e into main Jul 15, 2026
14 checks passed
@ChrisPulman
ChrisPulman deleted the feat/per-type-url-parameter-formatter branch July 15, 2026 06:33
@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.

feature: per-type IUrlParameterFormatter registry on RefitSettings

2 participants