[rust-server] Fix client serialization of enum parameters in form request bodies#24367
[rust-server] Fix client serialization of enum parameters in form request bodies#24367holdeg wants to merge 1 commit into
Conversation
|
Tagging rust-server technical committee for review: @frol @farcaller @richardwhiuk @paladinzh @jacob-pro @dsteeley |
|
This is failing because |
1326822 to
05a4e33
Compare
|
OK, I've modified the |
There were previously scenarios where |
There was a problem hiding this comment.
Not expecting to see this change in the PR.
05a4e33 to
3d7feac
Compare
There was a problem hiding this comment.
Considering this case where we have an array of enums, which I think is valid.
In that scenario I think we would want all the items in the Array to be formatted with the Display impl as well?
Annoyingly, and apologies, I don't think there's an existing test for this case.
Example:
statuses:
type: array
items:
type: string
enum: [available, pending, sold]
Results in
if let Some(param_statuses) = param_statuses {
// style=form,explode=true
for param_statuses in param_statuses {
params.push(("statuses",
format!("{:?}", param_statuses) <-- This is still bugged
));
}
}
There was a problem hiding this comment.
I agree - I think this is valid OpenAPI which is still being rendered incorrectly. I've amended by checking if array items are enum-like (and if so, changing their format strings).
3d7feac to
8e0e07f
Compare
8e0e07f to
2136948
Compare
|
I've squashed all the commits into one. |
Fixes #24366.
Issue restatement
For endpoints with request bodies of type form (e.g. application/x-www-form-urlencoded) and enum properties, the generated Rust client invoked format!("{:?}", param_foo) where param_foo is the model generated in models.rs for the enum property. {:?} is the debug formatter, which uses the derived impl Debug on the enum model, which resolves to the name of the enum variant (as per the rust docs) - however, we should instead be serializing this with the impl Display on the enum model, which produces the defined allowable values from the API definition. The old incorrect serialization did not obey the OpenAPI spec and caused any server policing the value of the enum to reject requests created by clients created using this tool.
Fix
Check if a parameter is enum-like using
getIsEnumOrRef()when deciding how to format it viax-format-string, and instead format enums with{}(which will invoke theirimpl Display).This is
impl Displayimpl Display, populated from the openapi definition, and this is what should be serialized.PR checklist
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.
Summary by cubic
Fix Rust server generator to serialize enum values in
application/x-www-form-urlencodedbodies usingDisplay, including enums inside arrays (required and optional), so clients send allowed OpenAPI values. Also standardizes example server logging to use{:?}for all params and regenerates samples.{}viagetIsEnumOrRef()checks on both params and array items, not{:?}.Display(noDebug) serialization.Written for commit 2136948. Summary will update on new commits.