-
Notifications
You must be signed in to change notification settings - Fork 117
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
Implement actix schema generator for tagged enums #389
base: master
Are you sure you want to change the base?
Conversation
Hey @Siphalor, please correct me if I'm wrong, but anyOf is not part of openapi version 2? |
Sadly you seem to be right. It seems like I was over-enthusiastic and got lost in the JSON Schema spec without noticing that OpenAPIv2 doesn't support the latest draft. Well, maybe this is at least useful as a reference for the v3 implementation then :) |
Yes this is not all for naught, it is very useful indeed because we want to eventually output proper openapi v3. |
@@ -54,6 +54,16 @@ pub trait Schema: Sized { | |||
/// - `serde_json::Value` works for both JSON and YAML. | |||
fn enum_variants(&self) -> Option<&[serde_json::Value]>; | |||
|
|||
/// More complex enum variants, where each has it's own schema. | |||
fn any_of(&self) -> Option<&Vec<Resolvable<Self>>>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rather than just any_of I wonder if we could have a schema kind that would allow for all the other variants (oneOf, etc)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
example:
pub enum SchemaKind {
Type(crate::v2::models::DataType),
OneOf {
#[serde(rename = "oneOf")]
one_of: Vec<crate::v2::models::Resolvable<Self>>,
},
AllOf {
#[serde(rename = "allOf")]
all_of: Vec<crate::v2::models::Resolvable<Self>>,
},
AnyOf {
#[serde(rename = "anyOf")]
any_of: Vec<crate::v2::models::Resolvable<Self>>,
},
}
If you were so inclined, how about adding a new ApiV3Schema trait (eg in core/v3/schema.rs) containing these new additions? |
04a5510
to
33cd55f
Compare
Resolves #97.
About
This pr implements schema generation for complex enums with serde's tag system.
Supported are all types of tagging (see here), except for the enum tuples.
Tuple schema generation is currently not implemented correctly anyway, so that should go into another pr.
This pr currently uses
anyOf
instead ofoneOf
to cover some possible edge cases, where serde would just use the first variant that matches (especially visible inuntagged
enums).To be able to implement this, this pr adds
any_of
andconst
to the internal schema representation.Up for discussion
anyOf
vs.oneOf
as described above.external
) and that only have unit variants.This makes it effectively impossible to add descriptions for the enum variants.
Possible workarounds:
any_of
system.enum
system and just useany_of
for everything.Other notes