[rust] fix: select response entity variant by status code
TE
3d ago+1278+1278+0123456789012345678901234567890123456789-245-245-012345678901234567890123456789
Fixes #25285
Related: #10977, #22080.
The reqwest and reqwest-trait clients fill ResponseContent::entity by deserializing the body into the #[serde(untagged)] {Op}Error (and, with supportMultipleResponses, {Op}Success) enum. The status code is ignored. When several responses share a schema, the first variant that fits wins: a 409 returning Problem is reported as Status400, and a 201 returning Pet as Status200.
Changes
rust/reqwest/api.mustacheandrust/reqwest-trait/api.mustache: when the enum hasStatusXXXvariants, the entity is picked bymatch status.as_u16():- an exact code deserializes into its variant;
- a range code (
4XX) matches400..=499; defaultmatches_;- a response declared without a body becomes
Some(StatusXXX()).
If the body doesn't deserialize into the selected variant, or the status isn't declared, it falls back toUnknownValue(serde_json::Value), orNoneif the body isn't JSON. Responses are already sorted as exact codes, then ranges, thendefault, so the arms can't hide each other.
RustClientCodegen: setsx-rust-success-by-status/x-rust-error-by-statuson operations that have 2xx/3xx or 4xx/5xx responses. Operations without them keep the existingserde_json::from_str(&content).ok()line, which avoids generating a single-armmatch.- The public types are unchanged: the enums keep their variants and
#[serde(untagged)].
Behavior change worth noting: for a declared status without a body (e.g. petstore's Status400()), entity is now Some(StatusXXX()) instead of None. This partially addresses #10977.
Not covered
hyper/hyper0x: they don't build a typed error entity, so they are untouched.- Deserializing the enums directly with serde is still shape-based.
Testing
- New
RustClientCodegenTest#testResponseEntityVariantIsSelectedByStatusCode(reqwest and reqwest-trait) withsrc/test/resources/3_0/rust/response-variant-by-status.yaml: exact, bodyless, range anddefaulterror responses, multiple 2xx with the same schema, exact (bodyless) and range 3xx responses, and an operation with onlydefault. cargo build --all-targets --all-featuresandcargo clippyinsamples/client/petstore/rustandsamples/client/others/rust: builds, and no new lints in the generated api files.- A crate generated from the test spec, called against a local stub server, returns
Status409for 409,Status404()for a bodyless 404,Status5XXfor 503,DefaultResponsefor 429, andStatus201/Status204()on success. The pre-fix output returnedStatus400/Status200for the same calls.
cc @frol @farcaller @richardwhiuk @paladinzh @jacob-pro @dsteeley
PR checklist
- Read the contribution guidelines.
- Run the following to build the project and update samples:
(For Windows users, please run the script in WSL)./mvnw clean package || exit ./bin/generate-samples.sh ./bin/configs/*.yaml || exit ./bin/utils/export_docs_generators.sh || exit
Commit all changed files.
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example./bin/generate-samples.sh bin/configs/java*.
IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
Regenerated allrustclient configs with./bin/generate-samples.sh ./bin/configs/rust-reqwest-*.yaml ./bin/configs/rust-hyper-*.yaml ./bin/configs/rust-hyper0x-*.yaml. Only reqwest and reqwest-trait samples changed. No generator options changed, so no docs export was needed. - If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.