-
Notifications
You must be signed in to change notification settings - Fork 202
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
Conversation
@@ -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| { |
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.
@juhaku that looks kinda naive. Isn't there a way to properly specify which features are supported by this specific case?
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.
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.
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.
@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.
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.
This is good for me 🎉
Though docs should be updated with the added feature at ToSchema
macro by changing the doc comment here
Line 58 in d020f92
#[proc_macro_derive(ToSchema, attributes(schema, aliases))] |
@juhaku I guess this section would be the right place to document the feature? |
Yes that chapter would be the right place for the doc. It should describe that it is used directly for the variant field and not for the variant itself. And perhaps an example of this would be good to add as well under the examples section a little bit further down. |
b89461d
to
8f294f0
Compare
Implementation for #727
The following is now supported:
and will produce the following schema: