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

Question Compilation error: ToSchema called Option::unwrap() on a None value #543

Closed
AnatolyBuga opened this issue Mar 25, 2023 · 11 comments · Fixed by #545
Closed

Question Compilation error: ToSchema called Option::unwrap() on a None value #543

AnatolyBuga opened this issue Mar 25, 2023 · 11 comments · Fixed by #545

Comments

@AnatolyBuga
Copy link

I am getting an error on

this function takes 1 argument but 0 arguments were supplied
    --> ultibi\ultibi_core\src\datarequest.rs:34:10
     |
34   | #[derive(ToSchema)]
     |          ^^^^^^^^ an argument is missing
     |
note: associated function defined here
    --> C:\Users\Anato\.cargo\registry\src\github.com-1ecc6299db9ec823\utoipa-3.1.2\src\openapi\schema.rs:1057:12
     |
1057 |     pub fn items<I: Into<RefOr<Schema>>>(mut self, component: I) -> Self {
     |            ^^^^^
     = note: this error originates in the derive macro `ToSchema` (in Nightly builds, run with -Z macro-backtrace for more info)
help: provide the argument
     |
34   | #[derive(ToSchema(/* value */))]

As far as I can see ToShema derive doesn't take any arguments. Any thoughts?

@AnatolyBuga
Copy link
Author

This is my code, although alot of type aliases - they are mostly strings and maps:

use utoipa::ToSchema;
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash)]
#[serde(tag = "type")]
#[derive(ToSchema)]
pub struct AggregationRequest {
    // general fields
    #[serde(default)]
    pub name: Option<String>,
    /// Measure: (Name, Action) where Name will be looked up in
    /// MeasuresMap of the DataSet
    pub measures: Vec<(MeasureName, AggregationName)>,
    pub groupby: Vec<String>,
    #[serde(default)]
    pub filters: AndOrFltrChain,
    #[serde(default)]
    pub overrides: Vec<Override>,
    #[serde(default, alias = "additionalRows")]
    pub add_row: AdditionalRows,
    #[serde(default)]
    pub calc_params: CPM,
    /// drop rows where all results are NULL or 0
    #[serde(default)]
    pub hide_zeros: bool,
    /// Show totals
    #[serde(default)]
    pub totals: bool,
}

@juhaku
Copy link
Owner

juhaku commented Mar 25, 2023

This is coming from the tuple syntax within your struct and is related to #330, #391, #176 and is fixed in #541.

   /// Measure: (Name, Action) where Name will be looked up in
   /// MeasuresMap of the DataSet
   pub measures: Vec<(MeasureName, AggregationName)>,

Tuple support will be available after next release, but can be accessed now by pointing the cargo dependency to the master for time being.

@AnatolyBuga
Copy link
Author

@juhaku great, thank you very much.

@AnatolyBuga
Copy link
Author

@juhaku , sorry, another issue followed directly from that one. Happy to put it in a separate issue though if that helps.
AggregationRequest defined as above.

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash)]
#[serde(untagged)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
pub enum ComputeRequest {
    Aggregation(AggregationRequest),
    Breakdown,
}

This fails to compile with:

error: proc-macro derive panicked
  --> ultibi\ultibi_core\src\datarequest.rs:19:40
   |
19 | #[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
   |                                        ^^^^^^^^^^^^^^^^
   |
   = help: message: called `Option::unwrap()` on a `None` value

error: could not compile `ultibi_core` due to previous error

@AnatolyBuga AnatolyBuga reopened this Mar 26, 2023
@juhaku
Copy link
Owner

juhaku commented Mar 26, 2023

Happy to put it in a separate issue though if that helps.

We can continue on this thread as well. 🙂

Interesting, that still does not tell enough what is going on. How does the AggregationRequest look? what attribute is defined over it?

@AnatolyBuga
Copy link
Author

@juhaku thanks for quick reply.

I will put together a proper reproducible example.

@AnatolyBuga
Copy link
Author

@juhaku , here we go:

use serde::Deserialize;
use serde::Serialize;

pub fn example() {
    ()
}


#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash, utoipa::ToSchema)]
#[serde(tag = "type")]
pub struct AggregationRequest {
    #[serde(default)]
    pub name: Option<String>,
    pub measures: Vec<(String, String)>
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash, utoipa::ToSchema)]
#[serde(untagged)]
pub enum ComputeRequest {
    Aggregation(AggregationRequest),
    Breakdown,
}

And:

serde = { version="*", features = ["derive", "rc"] }
serde_json = { version="*" }
utoipa = { git ="https://github.com/juhaku/utoipa", features = ["actix_extras"] }

@AnatolyBuga AnatolyBuga changed the title Question Compilation error: ToSchema this function takes 1 argument but 0 arguments were supplied Question Compilation error: ToSchema called Option::unwrap() on a None value Mar 26, 2023
@juhaku
Copy link
Owner

juhaku commented Mar 26, 2023

Thanks for the example 🙂 , this is a bug introduced here: #533

This makes me wonder why the parsing is getting to this point where it is now giving error.

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash, utoipa::ToSchema)]
#[serde(untagged)]
pub enum ComputeRequest {
    Aggregation(AggregationRequest),
    Breakdown, /// <-- this causes the error, 
}

I need to investigate what is going on here, this scenario should be possible to my knowledge 🤔

@juhaku
Copy link
Owner

juhaku commented Mar 26, 2023

Moreover it is caused by the #[serde(untagged)] attribute. I need to check whether there is something that can be done to fix it or whether this scenario is actually not possible with OpenAPI. Without #[serde(untagged)] it will work as it should.

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash, utoipa::ToSchema)]
#[serde(untagged)] // <-- here
pub enum ComputeRequest {
    Aggregation(AggregationRequest),
    Breakdown, /// <-- this causes the error, 
}

@juhaku
Copy link
Owner

juhaku commented Mar 26, 2023

@AnatolyBuga And there you go, fix is on the way landing to master soon by #545.

@AnatolyBuga
Copy link
Author

@juhaku thank you so much for promptly fixing this, and for the whole amazing library too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants