[rust-server] Fix client serialization of enum parameters in form request bodies
Fixes https://github.com/OpenAPITools/openapi-generator/issues/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 via x-format-string, and instead format enums with {} (which will invoke their impl Display).
This is
- safe because all models
impl Display - correct, because all enums have allowedValues in their
impl Display, populated from the openapi definition, and this is what should be serialized.
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. - If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.
Summary by cubic
Fix Rust server generator to serialize enum values in application/x-www-form-urlencoded bodies using Display, 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.
- Bug Fixes
- Enum-like form params now use
{}viagetIsEnumOrRef()checks on both params and array items, not{:?}. - Added test verifying required and optional array-of-enum form params use
Display(noDebug) serialization.
- Enum-like form params now use