Skip to content

Commit

Permalink
Fix unnamed enum with tag having single field (#262)
Browse files Browse the repository at this point in the history
Fix regression occurred since last release where enum with single
unnamed field caused error "Unnamed (tuple) enum variants are
unsupported for internally tagged enums using the `tag = ` serde
attribute" but this error should only happen when enum has multiple
unnamed fields.
  • Loading branch information
juhaku committed Aug 31, 2022
1 parent 9a73ea6 commit 14157de
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
27 changes: 21 additions & 6 deletions utoipa-gen/src/component/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,13 +572,28 @@ impl ComplexEnum<'_> {
.required(#tag)
}
}
Fields::Unnamed(_) => {
abort!(
variant,
"Unnamed (tuple) enum variants are unsupported for internally tagged enums using the `tag = ` serde attribute";
Fields::Unnamed(unnamed_fields) => {
dbg!(unnamed_fields);
if unnamed_fields.unnamed.len() == 1 {
let unnamed_enum = UnnamedStructSchema {
attributes: &variant.attrs,
fields: &unnamed_fields.unnamed,
};

help = "Try using a different serde enum representation";
);
quote! {
utoipa::openapi::schema::ObjectBuilder::new()
#variant_title
.property(#variant_name, #unnamed_enum)
}
} else {
abort!(
variant,
"Unnamed (tuple) enum variants are unsupported for internally tagged enums using the `tag = ` serde attribute";

help = "Try using a different serde enum representation";
note = "See more about enum limitations here: `https://serde.rs/enum-representations.html#internally-tagged`"
);
}
}
Fields::Unit => {
let variant_tokens = Self::unit_variant_tokens(variant_name, None);
Expand Down
30 changes: 30 additions & 0 deletions utoipa-gen/tests/schema_derive_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,36 @@ fn derive_complex_unnamed_field_reference_with_comment() {
);
}

#[test]
fn derive_enum_with_unnamed_single_field_with_tag() {
#[derive(Serialize)]
struct ReferenceValue(String);

let value: Value = api_doc! {
#[derive(Serialize)]
#[serde(tag = "enum")]
enum EnumWithReference {
Value(ReferenceValue),
}
};

assert_json_eq!(
value,
json!({
"oneOf": [
{
"type": "object",
"properties": {
"Value": {
"$ref": "#/components/schemas/ReferenceValue",
},
},
},
],
})
);
}

/// Derive a complex enum with named and unnamed fields.
#[test]
fn derive_complex_enum() {
Expand Down

0 comments on commit 14157de

Please sign in to comment.