Skip to content

[rust-server] Fix client serialization of enum parameters in form request bodies#24367

Open
holdeg wants to merge 1 commit into
OpenAPITools:masterfrom
holdeg:holdeg/rust-codegen-form-enum-serialization
Open

[rust-server] Fix client serialization of enum parameters in form request bodies#24367
holdeg wants to merge 1 commit into
OpenAPITools:masterfrom
holdeg:holdeg/rust-codegen-form-enum-serialization

Conversation

@holdeg

@holdeg holdeg commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

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 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:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    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 {} via getIsEnumOrRef() checks on both params and array items, not {:?}.
    • Added test verifying required and optional array-of-enum form params use Display (no Debug) serialization.

Written for commit 2136948. Summary will update on new commits.

Review in cubic

@holdeg

holdeg commented Jul 20, 2026

Copy link
Copy Markdown
Contributor Author

Tagging rust-server technical committee for review: @frol @farcaller @richardwhiuk @paladinzh @jacob-pro @dsteeley

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 9 files

Re-trigger cubic

@holdeg

holdeg commented Jul 20, 2026

Copy link
Copy Markdown
Contributor Author

This is failing because petstore-with-fake-endpoints-models-for-testing generates some code using the format string, which does not work correctly for enum models wrapped in Options. I think that's improper so taking a look at how it should do that better.

@holdeg
holdeg force-pushed the holdeg/rust-codegen-form-enum-serialization branch from 1326822 to 05a4e33 Compare July 20, 2026 17:29
@holdeg

holdeg commented Jul 20, 2026

Copy link
Copy Markdown
Contributor Author

OK, I've modified the example-server-operation.mustache file to no longer use x-format-string - it was the only other user of this variable. I think this is proper (and doesn't represent a problem for servers).

@dsteeley

Copy link
Copy Markdown
Contributor

OK, I've modified the example-server-operation.mustache file to no longer use x-format-string - it was the only other user of this variable. I think this is proper (and doesn't represent a problem for servers).

There were previously scenarios where example-server-operation.mustache using x-format-string would log out using {} rather than {:?}. This represents a change in behaviour here but given it's examples and logging I think that's acceptable.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not expecting to see this change in the PR.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this was because at the point I branched off master, we had dac3977 but not the followup 5d0a050 (which my commit was reimplementing). I've rebased so this should disappear.

@holdeg
holdeg force-pushed the holdeg/rust-codegen-form-enum-serialization branch from 05a4e33 to 3d7feac Compare July 21, 2026 08:16

@dsteeley dsteeley Jul 21, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
  ));
  }
  }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

@holdeg
holdeg force-pushed the holdeg/rust-codegen-form-enum-serialization branch from 3d7feac to 8e0e07f Compare July 21, 2026 09:54
@holdeg
holdeg force-pushed the holdeg/rust-codegen-form-enum-serialization branch from 8e0e07f to 2136948 Compare July 21, 2026 10:03
@holdeg

holdeg commented Jul 21, 2026

Copy link
Copy Markdown
Contributor Author

I've squashed all the commits into one.

@dsteeley dsteeley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look good to me. Thanks for fixing this issue up.
Over to @wing328 for final pass and merge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants