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

Add support for inlined enum variants #963

Merged
merged 2 commits into from
Aug 7, 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: 8 additions & 0 deletions utoipa-gen/src/component/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,14 @@ impl ToTokensDiagnostics for UnnamedStructSchema<'_> {

if fields_len == 1 {
if let Some(ref mut features) = unnamed_struct_features {
let inline =
features::parse_schema_features_with(&first_field.attrs, |input| {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@juhaku that looks kinda naive. Isn't there a way to properly specify which features are supported by this specific case?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What I mean is I'd rather have something like this:

Ok(EnumUnnamedFieldVariantFeatures(parse_features!(

But those features are for the variant themselves, so Inline would be:

    #[derive(ToSchema)]
    enum Card {
        #[schema(inline)]
        Number(Number),
        #[schema(inline)]
        Color(Color),
    }

Which IMO does not fit with the rest of the API and is counter-intuitive.

But I haven't found the features struct for the unnamed field.

Copy link
Owner

Choose a reason for hiding this comment

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

@juhaku that looks kinda naive. Isn't there a way to properly specify which features are supported by this specific case?

No features have been originally defined for unnamed field enum variants itself thus there is no such struct. Though if there is just a single feature to be parsed the parse_with is just fine but if there would be multiple features or the set of features is known to be needed in multiple locations then a feature struct is preferred.

Which IMO does not fit with the rest of the API and is counter-intuitive.

Yeah, I agree the API would be counter intuitive and the features defined directly for the unnamed enum variant field is cleaner approach.

But I haven't found the features struct for the unnamed field.

Such struct does not exists by design but could be added if needed.

Ok(parse_features!(input as super::features::Inline))
})?
.unwrap_or_default();

features.extend(inline);

if pop_feature!(features => Feature::Default(crate::features::Default(None)))
.is_some()
{
Expand Down
24 changes: 24 additions & 0 deletions utoipa-gen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,30 @@ use self::{
/// _`rename`_ attribute. It behaves similarly to serde's _`rename`_ attribute. If both _serde_
/// _`rename`_ and _schema_ _`rename`_ are defined __serde__ will take precedence.
///
/// ## Enum Unnamed Variant Field Configuration Options
///
/// * `inline` If the type of this field implements [`ToSchema`][to_schema], then the schema definition
/// will be inlined. **warning:** Don't use this for recursive data types!
///
/// _**Inline unnamed field variant schemas.**_
/// ```rust
/// # use utoipa::ToSchema;
/// # #[derive(ToSchema)]
/// # enum Number {
/// # One,
/// # }
/// #
/// # #[derive(ToSchema)]
/// # enum Color {
/// # Spade,
/// # }
/// #[derive(ToSchema)]
/// enum Card {
/// Number(#[schema(inline)] Number),
/// Color(#[schema(inline)] Color),
/// }
/// ```
///
/// # Unnamed Field Struct Optional Configuration Options for `#[schema(...)]`
/// * `description = ...` Can be literal string or Rust expression e.g. _`const`_ reference or
/// `include_str!(...)` statement. This can be used to override **default** description what is
Expand Down
80 changes: 80 additions & 0 deletions utoipa-gen/tests/schema_derive_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2788,6 +2788,86 @@ fn derive_struct_with_nullable_and_required() {
)
}

#[test]
fn derive_enum_with_inline_variant() {
#[allow(dead_code)]
#[derive(ToSchema)]
enum Number {
One,
Two,
Three,
Four,
Five,
Six,
Seven,
Height,
Nine,
}

#[allow(dead_code)]
#[derive(ToSchema)]
enum Color {
Spade,
Heart,
Club,
Diamond,
}

let card = api_doc! {
enum Card {
Number(#[schema(inline)] Number),
Color(#[schema(inline)] Color),
}
};

assert_json_eq!(
card,
json!({
"oneOf": [
{
"properties": {
"Number": {
"enum": [
"One",
"Two",
"Three",
"Four",
"Five",
"Six",
"Seven",
"Height",
"Nine",
],
"type": "string",
},
},
"required": [
"Number",
],
"type": "object",
},
{
"properties": {
"Color": {
"enum": [
"Spade",
"Heart",
"Club",
"Diamond",
],
"type": "string",
},
},
"required": [
"Color",
],
"type": "object",
},
],
})
);
}

#[test]
fn derive_struct_xml() {
let user = api_doc! {
Expand Down
Loading