Rust Axum allow oneOf multi values mapping

Merged
UN
2mo ago
+1286-88

Description

This PR adds rust-axum support for a discriminator mapping with more than one value for one schema.

#[serde(tag = "...")] removes the tag before serde deserializes the variant. Because of this, the struct cannot know the tag value in the input. Serialization always writes one fixed value.

Implement

  • When more than one value maps to one variant, the union uses the new impl_deserialize_tagged! macro. The macro deserializes the input into a serde_json::Value and reads the tag. Then it deserializes the variant from the full content, with the tag.
  • Fix: the generator looked for variants by schema name. When the schema name and the class name were different, the generator did not find the variant.
  • Fix: a string discriminator field wrote the class name, not the mapping value.
  • A nullable discriminator property now gives an untagged union.

Tests

  • New Rust tests in tests/discriminator_multi_mapping.rs. They examine the tag position, an unknown tag, a missing tag, and the round trip.
  • New assertions in RustAxumServerCodegenTest.
  • All rust-axum samples are generated again.

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.

@linxGnu (rust-axum)


Summary by cubic

Adds rust-axum support for discriminator mappings where several values map to one variant. #[serde(tag = "...")] consumes the tag before deserializing a variant, so the variant could not restore which mapping value it came from. Unions with multi-valued mappings are now deserialized through the new impl_deserialize_tagged! macro, which keeps the tag in the content.

New Features

  • The macro buffers the input into serde_json::Value, matches the tag against the mapping values, and errors on unknown or missing tags.
  • Unions whose variants each map to a single value keep using #[serde(tag = "...")].

Bug Fixes

  • Look up variants by class name instead of schema name when resolving discriminators.
  • Write the mapping value into string discriminator fields instead of the class name.
  • A nullable discriminator property falls back to an untagged union.