Skip to content

[Rust-Axum] Support enum $ref discriminator in oneOf - #25166

Merged
wing328 merged 5 commits into
OpenAPITools:masterfrom
unrealhoang:rust-axum-tagged-discriminator
Oct 7, 2026
Merged

wing328 merged 5 commits into
OpenAPITools:masterfrom
unrealhoang:rust-axum-tagged-discriminator

Conversation

@unrealhoang

@unrealhoang unrealhoang commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Description

In rust-axum, a oneOf with a discriminator falls back to #[serde(untagged)] when the discriminator property is a $ref to an enum (e.g. kind: { $ref: '#/components/schemas/PetKind' }) instead of a plain string. discriminating() only accepted string properties as the serde tag, so the variant was blocked and the parent's discriminator was dropped.

This PR accepts a required enum-ref discriminator property as the serde tag when every variant's discriminator value (from mapping, or the model name) is one of the enum's values:

  • The variant field gets #[serde(default = ...)] set to the matching enum variant (e.g. models::PetKind::Dog), because the tagged enum consumes the tag before the variant deserializes. It also gets #[serde(serialize_with = ...)], which always writes the variant's discriminator value. This is the same scheme already used for string discriminators.
  • The field is filled in by new() instead of being a constructor argument.
  • It keeps the current untagged behavior, and logs a warning, when:
    • a variant's discriminator value is not one of the enum's values, or
    • a variant's discriminator property is not required. This also applies to string discriminators, which previously generated an Option<String> field with a String default that didn't compile.

Tests

  • Added RustAxumServerCodegenTest#testDiscriminatorReferencingEnum, using new schemas in 3_0/rust-axum/rust-axum-oneof.yaml:
    • Pet (Dog/Cat): tagged.
    • Shape (Square's discriminator value is not an enum value): untagged.
    • Rodent (Mouse's discriminator property is optional): untagged.
  • Updated the rust-axum-oneof sample and added tests/discriminator_enum_ref.rs, a serde round-trip test covering tagged dispatch, unknown/missing tags being rejected, constructors filling in the tag, and the tag always being written from the variant.

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 support in rust-axum for a oneOf discriminator that is a $ref to an enum, so tagged serde dispatch is used instead of falling back to #[serde(untagged)] when the tag's values match the enum values.

Behavior

  • Variants get the tag field filled in by new() with a matching enum value, and serialization always writes the variant's discriminator value.
  • Falls back to untagged dispatch with a warning when a mapping value is not in the enum, or when the discriminator property is not required.
  • Fixes a generated-code compile error where an optional string discriminator produced an Option<String> field with a String default.

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

Review in cubic

@linxGnu linxGnu 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.

LGTM.

Great PR. Thank you for your contribution.

cc @wing328 for final review

@wing328 wing328 added this to the 7.27.0 milestone Oct 7, 2026

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

2 issues found across 50 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="modules/openapi-generator/src/main/resources/rust-axum/models.mustache">

<violation number="1" location="modules/openapi-generator/src/main/resources/rust-axum/models.mustache:1100">
P1: Required nullable enum discriminators still receive these bare-enum helpers, but the generated field type is `Nullable<Enum>`; the default, constructor initializer, and serializer therefore fail to type-check. Exclude nullable enum properties from tagged mode or handle `Nullable` consistently.</violation>
</file>

<file name="samples/server/petstore/rust-axum/output/rust-axum-discriminator-enum-ref/src/models.rs">

<violation number="1" location="samples/server/petstore/rust-axum/output/rust-axum-discriminator-enum-ref/src/models.rs:920">
P2: `Rodent` is untagged and tries `Hamster` before `Mouse`, but `Hamster` has no required field other than the shared `kind` enum, so a `{"kind":"mouse"}` payload deserializes into `Rodent::Hamster` with `kind=RodentKind::Mouse` instead of `Rodent::Mouse`. The untagged fallback can't actually discriminate these variants; a `kind`-value consistency check before falling back to untagged (or routing on the discriminator value) would avoid the silent misroute.</violation>
</file>

Reply to a comment to ask cubic a question or push back. It learns from your replies.

Turn on auto-fix | Re-trigger cubic

{{/isString}}
{{#vendorExtensions.x-discriminator-enum-variant}}
impl {{{classname}}} {
fn _name_for_{{{name}}}() -> {{{dataType}}} {

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.

P1: Required nullable enum discriminators still receive these bare-enum helpers, but the generated field type is Nullable<Enum>; the default, constructor initializer, and serializer therefore fail to type-check. Exclude nullable enum properties from tagged mode or handle Nullable consistently.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At modules/openapi-generator/src/main/resources/rust-axum/models.mustache, line 1100:

<comment>Required nullable enum discriminators still receive these bare-enum helpers, but the generated field type is `Nullable<Enum>`; the default, constructor initializer, and serializer therefore fail to type-check. Exclude nullable enum properties from tagged mode or handle `Nullable` consistently.</comment>

<file context>
@@ -1091,6 +1095,20 @@ impl {{{classname}}} {
 {{/isString}}
+{{#vendorExtensions.x-discriminator-enum-variant}}
+impl {{{classname}}} {
+    fn _name_for_{{{name}}}() -> {{{dataType}}} {
+        {{{dataType}}}::{{{.}}}
+    }
</file context>

}

#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(untagged)]

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.

P2: Rodent is untagged and tries Hamster before Mouse, but Hamster has no required field other than the shared kind enum, so a {"kind":"mouse"} payload deserializes into Rodent::Hamster with kind=RodentKind::Mouse instead of Rodent::Mouse. The untagged fallback can't actually discriminate these variants; a kind-value consistency check before falling back to untagged (or routing on the discriminator value) would avoid the silent misroute.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At samples/server/petstore/rust-axum/output/rust-axum-discriminator-enum-ref/src/models.rs, line 920:

<comment>`Rodent` is untagged and tries `Hamster` before `Mouse`, but `Hamster` has no required field other than the shared `kind` enum, so a `{"kind":"mouse"}` payload deserializes into `Rodent::Hamster` with `kind=RodentKind::Mouse` instead of `Rodent::Mouse`. The untagged fallback can't actually discriminate these variants; a `kind`-value consistency check before falling back to untagged (or routing on the discriminator value) would avoid the silent misroute.</comment>

<file context>
@@ -0,0 +1,1213 @@
+}
+
+#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
+#[serde(untagged)]
+#[allow(non_camel_case_types, clippy::large_enum_variant)]
+pub enum Rodent {
</file context>

@wing328
wing328 merged commit b7af909 into OpenAPITools:master Oct 7, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants