Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: tagged enum with flatten fields #1208

Merged
merged 4 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 7 additions & 1 deletion utoipa-gen/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog - utoipa-gen

## Unreleased

### Fixed

* Fix tagged enum with flatten fields (https://github.com/juhaku/utoipa/pull/1208)

## 5.2.0 - Nov 2024

### Fixed
Expand Down Expand Up @@ -454,7 +460,7 @@
* Add support for multiple examples in response (https://github.com/juhaku/utoipa/pull/403)
* Add Example type to OpenApi types (https://github.com/juhaku/utoipa/pull/402)
* Add derive info support for derive OpenApi (https://github.com/juhaku/utoipa/pull/400)
* Add `merge` functionality for `OpenApi` (https://github.com/juhaku/utoipa/pull/397)
* Add `merge` functionality for `OpenApi` (https://github.com/juhaku/utoipa/pull/397)
* Add derive servers attribute for OpenApi (https://github.com/juhaku/utoipa/pull/395)
* Add support for unit sructs (https://github.com/juhaku/utoipa/pull/392)
* Add support for `schema_with` custom fn reference (https://github.com/juhaku/utoipa/pull/390)
Expand Down
11 changes: 10 additions & 1 deletion utoipa-gen/src/component/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ pub struct NamedStructSchema {
pub schema_as: Option<As>,
fields_references: Vec<SchemaReference>,
bound: Option<Bound>,
is_all_of: bool,
}

#[cfg_attr(feature = "debug", derive(Debug))]
Expand Down Expand Up @@ -385,6 +386,7 @@ impl NamedStructSchema {
.flatten()
.collect::<Vec<_>>();

let mut object_tokens_empty = true;
let object_tokens = fields_vec
.iter()
.filter(|(_, field_rules, ..)| !field_rules.skip && !field_rules.flatten)
Expand Down Expand Up @@ -415,6 +417,7 @@ impl NamedStructSchema {
_field,
field_schema,
)| {
object_tokens_empty = false;
let rename_to = field_rules
.rename
.as_deref()
Expand Down Expand Up @@ -513,8 +516,13 @@ impl NamedStructSchema {
tokens.extend(quote! {
utoipa::openapi::AllOfBuilder::new()
#flattened_tokens
.item(#object_tokens)

});
if !object_tokens_empty {
tokens.extend(quote! {
.item(#object_tokens)
});
}
true
}
} else {
Expand Down Expand Up @@ -552,6 +560,7 @@ impl NamedStructSchema {
schema_as,
fields_references,
bound,
is_all_of: all_of,
})
}

Expand Down
24 changes: 19 additions & 5 deletions utoipa-gen/src/component/schema/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,12 +517,26 @@ impl MixedEnumContent {
MixedEnumContent::split_enum_features(variant_features);
let schema = NamedStructSchema::new(root, fields, variant_features)?;

let schema_tokens = schema.to_token_stream();
let mut schema_tokens = schema.to_token_stream();
(
EnumSchema::<ObjectSchema>::tagged(schema_tokens)
.tag(tag, PlainSchema::for_name(name.as_ref()))
.features(enum_features)
.to_token_stream(),
if schema.is_all_of {
let object_builder_tokens =
quote! { utoipa::openapi::schema::Object::builder() };
let enum_schema_tokens =
EnumSchema::<ObjectSchema>::tagged(object_builder_tokens)
.tag(tag, PlainSchema::for_name(name.as_ref()))
.features(enum_features)
.to_token_stream();
schema_tokens.extend(quote! {
.item(#enum_schema_tokens)
});
schema_tokens
} else {
EnumSchema::<ObjectSchema>::tagged(schema_tokens)
.tag(tag, PlainSchema::for_name(name.as_ref()))
.features(enum_features)
.to_token_stream()
},
schema.fields_references,
)
}
Expand Down
49 changes: 49 additions & 0 deletions utoipa-gen/tests/schema_derive_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1089,6 +1089,55 @@ fn derive_simple_enum_serde_tag() {
);
}

#[test]
fn derive_simple_enum_serde_tag_with_flatten_content() {
#[derive(Serialize, ToSchema)]
#[allow(unused)]
struct Foo {
name: &'static str,
}

let value: Value = api_doc! {
#[derive(Serialize)]
#[serde(tag = "tag")]
enum Bar {
One {
#[serde(flatten)]
foo: Foo,
},
}
};

assert_json_eq!(
value,
json!({
"oneOf": [
{
"allOf": [
{
"$ref": "#/components/schemas/Foo",
},
{
"type": "object",
"properties": {
"tag": {
"type": "string",
"enum": [
"One",
],
},
},
"required": [
"tag",
],
},
],
},
],
})
);
}

#[test]
fn derive_simple_enum_serde_untagged() {
let value: Value = api_doc! {
Expand Down