Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions utoipa-gen/src/component/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -855,10 +855,17 @@ impl<'e> EnumSchema<'e> {
parent: &'e Root<'e>,
variants: &'e Punctuated<Variant, Comma>,
) -> Result<Self, Diagnostics> {
if variants
let all_unit_not_partially_tagged = variants
.iter()
.all(|variant| matches!(variant.fields, Fields::Unit))
{
.map(|variant| {
Ok(matches!(variant.fields, Fields::Unit)
&& !serde::parse_value(&variant.attrs)?.untagged)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

suggestion: I read the whole related code more carefully so from what I understood https://github.com/fnimick/utoipa/blob/58eb1dbda4a04831e899eadd1be943e6d03296cb/utoipa-gen/src/component/schema/enums.rs#L63C13-L63C70 is executed the duplicated per-variant parse. Do you think we can eliminate this? If not, we can keep it as it is.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done

})
.collect::<Result<Vec<bool>, Diagnostics>>()?
.into_iter()
.all(|is_plain| is_plain);

if all_unit_not_partially_tagged {
#[cfg(feature = "repr")]
let mut features = {
if parent
Expand Down
24 changes: 21 additions & 3 deletions utoipa-gen/src/component/schema/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,13 @@ impl MixedEnumContent {
generics: root.generics,
};

let tokens_with_schema_references = match &serde_container.enum_repr {
let enum_repr = if variant_serde_rules.untagged {
&SerdeEnumRepr::Untagged
} else {
&serde_container.enum_repr
};

let tokens_with_schema_references = match enum_repr {
SerdeEnumRepr::ExternallyTagged => {
let (enum_features, variant_features) =
MixedEnumContent::split_enum_features(variant_features);
Expand Down Expand Up @@ -596,7 +602,13 @@ impl MixedEnumContent {
generics: root.generics,
};

let tokens_with_schema_reference = match &serde_container.enum_repr {
let enum_repr = if variant_serde_rules.untagged {
&SerdeEnumRepr::Untagged
} else {
&serde_container.enum_repr
};

let tokens_with_schema_reference = match enum_repr {
SerdeEnumRepr::ExternallyTagged => {
let (enum_features, variant_features) =
MixedEnumContent::split_enum_features(variant_features);
Expand Down Expand Up @@ -682,7 +694,13 @@ impl MixedEnumContent {
);
let name = renamed.unwrap_or(Cow::Owned(name));

match &serde_container.enum_repr {
let enum_repr = if variant_serde_rules.untagged {
&SerdeEnumRepr::Untagged
} else {
&serde_container.enum_repr
};

match enum_repr {
SerdeEnumRepr::ExternallyTagged => EnumSchema::<PlainSchema>::new(name.as_ref())
.features(variant_features)
.to_token_stream(),
Expand Down
5 changes: 5 additions & 0 deletions utoipa-gen/src/component/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub struct SerdeValue {
pub rename: Option<String>,
pub default: bool,
pub flatten: bool,
pub untagged: bool,
pub skip_serializing_if: bool,
pub double_option: bool,
}
Expand Down Expand Up @@ -67,6 +68,7 @@ impl SerdeValue {
.unwrap_or(false);
}
TokenTree::Ident(ident) if ident == "flatten" => value.flatten = true,
TokenTree::Ident(ident) if ident == "untagged" => value.untagged = true,
TokenTree::Ident(ident) if ident == "rename" => {
if let Some((literal, _)) = parse_next_lit_str(next) {
value.rename = Some(literal)
Expand Down Expand Up @@ -241,6 +243,9 @@ pub fn parse_value(attributes: &[Attribute]) -> Result<SerdeValue, Diagnostics>
if value.flatten {
acc.flatten = value.flatten;
}
if value.untagged {
acc.untagged = value.untagged;
}
if value.default {
acc.default = value.default;
}
Expand Down
Loading