-
Notifications
You must be signed in to change notification settings - Fork 219
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
Parameters as struct implementing IntoParams for warp (and other frameworks) #150
Conversation
As a solution to this I decided to implement #151 which will allow a container attribute for |
I ended up implementing the |
@juhaku I think this PR is ready for review |
977e94f
to
39943cf
Compare
`IntoParams`, implementing juhaku#129
as PascalCase instead of lowercase, to match the style parsing.
actix test failure)
62fed6e
to
df5f5ae
Compare
The reason was to have as short syntax as possible but still being clear enough. Indeed I agree these 2 parsing syntaxes could be unified for clarity to the api consumers not having to guess the syntax. The I guess the next release will anyway be breaking release since I want to and need to make some changes to the actix-web support so it would work the same way that the type with IntoParams need to be defined in |
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.
Super, 🥇 This is indeed pretty good base for getting IntoParams available to all the frameworks. Just some minor things to clarify :)
/// `#[utoipa::path(params(...))]` attribute. | ||
#[cfg_attr(feature = "debug", derive(Debug))] | ||
#[derive(Default)] | ||
struct Params<'p> { |
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.
Just wondering is this extra Params type necessary?
@@ -27,10 +31,35 @@ use super::property::Property; | |||
#[cfg_attr(feature = "debug", derive(Debug))] | |||
pub enum Parameter<'a> { | |||
Value(ParameterValue<'a>), | |||
/// Identifier for a struct that implements `IntoParams` trait. | |||
Struct(ExprPath), | |||
#[cfg(any(feature = "actix_extras", feature = "rocket_extras"))] | |||
TokenStream(TokenStream), |
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.
Here the TokenStream
type was actually for that same purpse as now the Struct
exists. The TokenStream
will be later on used in extension code in similar manner <#struct as utoipa::IntoParams>::into_params()
Im just wondering could these two types somehow be combined to avoid less fragmentation?
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.
Is TokenStream
a little too permissive for this? I'll admit I haven't taken a proper look at the actix/rocket side of the equation.
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.
For the purpose it is used in acitx-web extras https://github.com/juhaku/utoipa/blob/master/utoipa-gen/src/ext/actix.rs#L67 it is kind of necessary to have it to allow more than a expression path. Note it creates assert trait for the IntoParmas which could also be implemented here as well I think? 🤔
utoipa-gen/src/path/parameter.rs
Outdated
match &*style.to_string() { | ||
"Path" => Ok(Self::Path), | ||
"Query" => Ok(Self::Query), | ||
"Header" => Ok(Self::Header), | ||
"Cookie" => Ok(Self::Cookie), | ||
_ => Err(Error::new(style.span(), EXPECTED_STYLE)), |
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.
Should we define one way how the parameter_in
will be parsed, since now it is parsed with 2 different syntax one here with capitalized first letter, and in tuple syntax all lowercase. It would be better if there was only one way of doing things for clarity to the users.
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.
If we change the other location to CamelCase that will be a breaking change, but it's also easier to recognise it as being one of the enum variants. I agree with you that it should be consistent, which direction would you like to take it?
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.
Considering this library is already 1.0 I guess the aim is to reduce breaking changes where possible, perhaps I'll change this to lower case too?
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.
We can have it Pascal case. It will be breaking change but then it's going to be one. I consider the major release as a "how many mistakes have been done" and it can be increased according to the semver. But for sure it if it is possible to avoid breaking releases then it should be avoided as much as possible. But in this case it is fine for me. Then I can introduce other breaking changes as well with next release. So next release will then be 2.0.0
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.
Okay sounds good I'll try to share the parsing then for this.
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.
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.
Super that looks good
help = "Try defining `#[into_params(names(...))]` over your type: {}", ident, | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
struct Param<'a>(&'a Field, Option<&'a String>); | ||
#[cfg_attr(feature = "debug", derive(Debug))] | ||
pub struct FieldParamContainerAttributes<'a> { |
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.
It is pretty good idea to have possibility to define the style
in struct level for all parameter fields, but after all since it is parameter specific attribute. Is it still possible to override this struct level style with style in parameter level with param(style = ...)
?
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.
https://github.com/juhaku/utoipa/pull/150/files#diff-bf76ef193c3d97fa4bbda36c667b8f030fe35fbbf0690dc8442bf231e7487043R284-R292 here the style
container attribute will be overridden by the field attribute
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.
Line 284-292 in into_params.rs
this file
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.
Good
Co-authored-by: Juha Kukkonen <juha7kukkonen@gmail.com>
Co-authored-by: Juha Kukkonen <juha7kukkonen@gmail.com>
Co-authored-by: Juha Kukkonen <juha7kukkonen@gmail.com>
@juhaku I did try that and I think it works, but it involves having a different name for the parameter in |
I think it can be |
parsing logic, this is a breaking change to use Pascal case for these: `Path`, `Query`, `Header`, `Cookie`
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.
Awesome 🏆
params(Type)
forpath
whereType
implementsIntoParams
, closing Serde Deserialize parameters #129.todo-warp
to show this feature in use.IntoParams
includingstyle
andparameter_in
. For example:#[param(style = Form, parameter_in = Query)]
.TODO:
ParameterIn
which is currently documented as an "internal trait". Perhaps this trait could also be derived?Fix todo-warp projectedit: looks like this is a regression onmaster
todo-warp example broken #173This work was sponsored by Arctoris.