-
-
Notifications
You must be signed in to change notification settings - Fork 231
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
Schemars not understanding #[serde(serialize_with = "serialize_function")] macro #15
Comments
I can't think of any robust way for schemars to work out the schema itself here, as it can't really "look inside" the serialize_function. What I could certainly do though is allow you to specify another function which returns the schema you want, which would look something like: pub enum Example {
#[schemars(schema_with = "schema_function")]
#[serde(serialize_with = "serialize_function")]
Function(String, functions::QueryFn),
}
fn schema_function() -> Schema {
// ...
} Would that solve your problem? |
It should already be possible to do something similar using pub enum Example {
#[schemars(with = "T")]
#[serde(serialize_with = "serialize_function")]
Function(String, functions::QueryFn),
} ... where However, when I just tried that out I realised that schemars currently doesn't respect attributes set on enum variants! I'll try to get that fixed soon |
Thanks for the information. I think being able to define custom schema_function's would exactly solve the problem. |
@GREsau, I know there is a I've written custom serializers and deserializers for std::io::Error and std::io::ErrorKind such that I can ship these across the wire. I obviously cannot add JsonSchema as a derive to these types and my serialization of ErrorKind is a giant mapping of serialize_unit_variant while the io::Error just encapsulates the description, os code, and error kind fields. If I could provide a custom schema function, I'd be able to list out the error kind variants myself. The alternative would be to create an equivalent enum that maps to/from io::ErrorKind that derives JsonSchema and plug that enum into my other structures. |
The pub enum Example {
#[schemars(schema_with = "schema_function")]
#[serde(serialize_with = "serialize_function")]
Function(String, functions::QueryFn),
}
fn schema_function(gen: &mut SchemaGenerator) -> Schema {
// ...
} Could you give it a try and let me know if it works as you expect? |
Trying to serialize a struct like this
I get
The text was updated successfully, but these errors were encountered: