[rust] fix: map-typed query parameters do not compile (.to_string() on a HashMap)

Merged
WI
2mo ago
+69-0

A map-typed query parameter (type: object with typed additionalProperties, generated as HashMap<String, T>) falls through to the scalar path in several template branches, which call .to_string() on it. HashMap has no Display, so the crate does not compile:

error[E0599]: `HashMap<std::string::String, std::string::String>` doesn't implement `std::fmt::Display`
error[E0599]: the method `to_string` exists for reference `&HashMap<std::string::String, i32>`,
              but its trait bounds were not satisfied
branchreqwestreqwest-trait
required, non-nullable.to_string(): broken.to_string(): broken
required, nullable, non-deepObjectJSON-encoded: ok.to_string() fallthrough: broken
optional, non-deepObjectJSON-encoded: ok.to_string() fallthrough: broken
deepObject + explodehandledhandled

Fix

An {{#isMap}} split at each hole serializes the map with serde_json::to_string, as reqwest's optional non-primitive branch already does. No wire change for anything that compiled before: isMap also covers a bare free-form object (serde_json::Value), which moves from .to_string() to serde_json::to_string(..)?, and those produce the same JSON.

Verification

  • RustClientCodegenTest#testMapQueryParamsSerializeAsJson runs the fixture through both libraries and fails without the templates.
  • cargo build is clean with the fix and fails with E0599 without it.
  • Samples: zero diffs.

Known gaps

A required non-nullable style: deepObject map is sent as one JSON parameter. That branch never had deepObject handling (for maps it did not compile before); deepObject serialization is #24899's scope.

Related

Follow-up to #24866.

PR checklist

  • Read the contribution guidelines.
  • Built the project and updated samples (./bin/generate-samples.sh ./bin/configs/rust-*): zero diffs.
  • Technical committee (Rust): @frol @farcaller @richardwhiuk @paladinzh @jacob-pro @dsteeley

Summary by cubic

Fixes Rust client codegen for map-typed query parameters, which previously emitted .to_string() on a HashMap (no Display/ToString) and failed to compile in reqwest and reqwest-trait. Maps now serialize as a single JSON-encoded query parameter via serde_json::to_string, matching how non-primitive parameters are already handled. No behavior change for parameters that compiled before.

  • Adds a test fixture and regression test covering required and optional map query params in both libraries.
  • A required non-nullable style: deepObject map still sends as one JSON parameter; deepObject serialization remains out of scope.

Generated with Claude Code