Implement support for serde "partially untagged" enums#1521
Open
foxfriends wants to merge 1 commit into
Open
Conversation
See here: https://serde.rs/variant-attrs.html#untagged > Irrespective of the enum representation, serialize and deserialize this variant as untagged, i.e. simply as the variant's data with no record of the variant name. For a simple example: ```rust enum SomeEnum { Tagged(u32) #[serde(untagged)] Untagged(u32) } ``` This would serialize to an optionally tagged integer: ```json {"Tagged": 3} 3 ``` With resulting JSON Schema: ```json { "oneOf": [ {"type": "object", "properties": {"Tagged": {"type": "integer"} }, "required": ["Tagged"] }, {"type": "integer"} ] } ``` --- Somewhat related, but __NOT__ implemented in this PR (as it seems like it will take some deeper refactoring of the "plain enum" handling): ```rust enum SomeEnum { One, Two, #[serde(untagged)] Null } ``` For this enum, serde would emit JSON values `"One"`, `"Two"` and `null`, but utoipa still generates a JSON schema for an enum: ```json { "enum": ["One", "Two", "Null"], "type": "string" } ```
foxfriends
commented
Feb 14, 2026
Comment on lines
+1369
to
+1371
| #[test] | ||
| #[ignore = "not implemented"] | ||
| fn derive_unit_variants_serde_partially_untagged() { |
Author
There was a problem hiding this comment.
I left this ignored test here to bring attention to the unimplemented functionality for plain enums
foxfriends
commented
Feb 14, 2026
Comment on lines
+1346
to
+1367
| #[test] | ||
| fn mixed_enum_serde_partially_untagged_named_fields_proof() { | ||
| #[derive(Serialize)] | ||
| enum Foo { | ||
| One { | ||
| n: i32, | ||
| }, | ||
| #[serde(untagged)] | ||
| Two { | ||
| m: i32, | ||
| }, | ||
| } | ||
|
|
||
| assert_eq!( | ||
| serde_json::to_string(&Foo::One { n: 3 }).unwrap(), | ||
| r#"{"One":{"n":3}}"# | ||
| ); | ||
| assert_eq!( | ||
| serde_json::to_string(&Foo::Two { m: 3 }).unwrap(), | ||
| r#"{"m":3}"# | ||
| ); | ||
| } |
Author
There was a problem hiding this comment.
I also included these "proof" tests, to validate this behaviour of serde, since it is not as widely known as functionality. Can be removed if not wanted, as this doesn't actually test utoipa functionality, only serde functionality.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
See here: https://serde.rs/variant-attrs.html#untagged
For a simple example:
This would serialize to an optionally tagged integer:
{"Tagged": 3} 3With resulting JSON Schema:
{ "oneOf": [ {"type": "object", "properties": {"Tagged": {"type": "integer"} }, "required": ["Tagged"] }, {"type": "integer"} ] }Somewhat related, but NOT implemented in this PR (as it seems like it will take some deeper refactoring of the "plain enum" handling):
For this enum, serde would emit JSON values
"One","Two"andnull, but utoipa still generates a JSON schema for an enum:{ "enum": ["One", "Two", "Null"], "type": "string" }