Repository navigation
[Rust-Axum] Support enum $ref discriminator in oneOf - #25166
Conversation
There was a problem hiding this comment.
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}}} { |
There was a problem hiding this comment.
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)] |
There was a problem hiding this comment.
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>
Description
In
rust-axum, aoneOfwith adiscriminatorfalls back to#[serde(untagged)]when the discriminator property is a$refto an enum (e.g.kind: { $ref: '#/components/schemas/PetKind' }) instead of a plain string.discriminating()only acceptedstringproperties 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:#[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.new()instead of being a constructor argument.untaggedbehavior, and logs a warning, when:required. This also applies to string discriminators, which previously generated anOption<String>field with aStringdefault that didn't compile.Tests
RustAxumServerCodegenTest#testDiscriminatorReferencingEnum, using new schemas in3_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.rust-axum-oneofsample and addedtests/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
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.
@linxGnu (rust-axum)
Summary by cubic
Adds support in
rust-axumfor aoneOfdiscriminator that is a$refto 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
new()with a matching enum value, and serialization always writes the variant's discriminator value.required.Option<String>field with aStringdefault.Written for commit 3700837. Summary will update on new commits.