Skip to content

Commit

Permalink
Update docs and actix parameter resolving
Browse files Browse the repository at this point in the history
  • Loading branch information
juhaku committed Apr 18, 2022
1 parent 53d743e commit d4e0657
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 66 deletions.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,22 @@ and the `ipa` is _api_ reversed. Aaand... `ipa` is also awesome type of beer :be
## Features

* **default** Default enabled features are **json**.
* **json** Enables **serde_json** what allow to use json values in OpenAPI specification values. This is
enabled by default.
* **yaml** Enables **serde_yaml** serialization of OpenApi objects.
* **json** Enables **serde_json** serialization of OpenAPI objects which also allows usage of JSON within
OpenAPI values e.g. within `example` value. This is enabled by default.
* **yaml** Enables **serde_yaml** serialization of OpenAPI objects.
* **actix_extras** Enhances [actix-web](https://github.com/actix/actix-web/) intgration with being able to
parse `path` and `path parameters` from actix web path attribute macros. See
[docs](https://docs.rs/utoipa/0.1.2/utoipa/attr.path.html#actix_extras-support-for-actix-web) or [examples](./examples) for more details.
* **rocket_extras** Enhances [rocket](https://github.com/SergioBenitez/Rocket) framework integration with being
able to parse `path`, `path and query parameters` from rocket path attribute macros. See [docs](https://docs.rs/utoipa/0.1.2/utoipa/attr.path.html#rocket_extras-support-for-rocket)
or [examples](./examples) for more details.
* **debug** Add extra traits such as debug traits to openapi definitions and elsewhere.
* **chrono_types** Add support for [chrono](https://crates.io/crates/chrono) `DateTime`, `Date` and `Duration` types. By default these types
are parsed to `string` types without additional format. If you want to have formats added to the types
use *chrono_types_with_format* feature. This is useful because OpenAPI 3.1 spec does not have date-time formats.
* **chrono_types_with_format** Add support to [chrono](https://crates.io/crates/chrono) types described above with additional `format`
information type. `date-time` for `DateTime` and `date` for `Date` according
* **chrono_types** Add support for [chrono](https://crates.io/crates/chrono) `DateTime`, `Date` and `Duration`
types. By default these types are parsed to `string` types without additional format. If you want to have
formats added to the types use *chrono_types_with_format* feature. This is useful because OpenAPI 3.1 spec
does not have date-time formats.
* **chrono_types_with_format** Add support to [chrono](https://crates.io/crates/chrono) types described above
with additional `format` information type. `date-time` for `DateTime` and `date` for `Date` according
[RFC3339](https://xml2rfc.ietf.org/public/rfc/html/rfc3339.html#anchor14) as `ISO-8601`.
* **decimal** Add support for [rust_decimal](https://crates.io/crates/rust_decimal) `Decimal` type. **By default**
it is interpreted as `String`. If you wish to change the format you need to override the type.
Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@
//! # Features
//!
//! * **default** Default enabled features are **json**.
//! * **json** Enables **serde_json** what allow to use json values in OpenAPI specification values.
//! This is enabled by default.
//! * **yaml** Enables **serde_yaml** serialization of OpenApi objects.
//! * **json** Enables **serde_json** serialization of OpenAPI objects which also allows usage of JSON within
//! OpenAPI values e.g. within `example` value. This is enabled by default.
//! * **yaml** Enables **serde_yaml** serialization of OpenAPI objects.
//! * **actix_extras** Enhances [actix-web](https://github.com/actix/actix-web/) intgration with being able to
//! parse `path` and `path parameters` from actix web path attribute macros. See [actix extras support][actix_path] or
//! [examples](https://github.com/juhaku/utoipa/tree/master/examples) for more details.
Expand Down
7 changes: 0 additions & 7 deletions utoipa-gen/src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@ pub struct Argument<'a> {
pub is_option: bool,
}

impl Argument<'_> {
#[deprecated(since = "0.2.0", note = "There should not be nameless arguments")]
pub fn has_name(&self) -> bool {
self.name.is_some()
}
}

#[cfg_attr(feature = "debug", derive(Debug))]
#[derive(PartialEq)]
pub enum ArgumentIn {
Expand Down
37 changes: 4 additions & 33 deletions utoipa-gen/src/ext/actix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl ArgumentResolver for PathOperations {

resolved_args
.into_iter()
.zip(types.into_iter())
.zip(types)
.map(|(resolved_arg, ty)| {
let name = match resolved_arg {
ResolvedArg::Path(path) => path.name,
Expand Down Expand Up @@ -57,35 +57,7 @@ impl PathOperations {
}
}

fn get_argument_names(pat_type: &PatType) -> Vec<&Ident> {
match pat_type.pat.as_ref() {
Pat::Ident(pat_ident) => {
vec![&pat_ident.ident]
}
Pat::TupleStruct(tuple) => tuple
.pat
.elems
.iter()
.flat_map(|pat| match pat {
Pat::Ident(pat_ident) => vec![&pat_ident.ident],
Pat::Tuple(tuple) => tuple
.elems
.iter()
.map(|pat| match pat {
Pat::Ident(pat_ident) => &pat_ident.ident,
_ => abort_call_site!(
"unexpected pat ident in Pat::Tuple expected Pat::Ident"
),
})
.collect(),
_ => abort_call_site!("unexpected pat type expected Pat::Ident"),
})
.collect::<Vec<_>>(),
_ => abort_call_site!("unexpected pat type expected Pat::Ident or Pat::Tuple"),
}
}

fn get_argument_types(path_segment: &PathSegment) -> Vec<&Ident> {
fn get_argument_types(path_segment: &PathSegment) -> impl Iterator<Item = &Ident> {
match &path_segment.arguments {
PathArguments::AngleBracketed(angle_bracketed) => angle_bracketed
.args
Expand All @@ -104,8 +76,7 @@ impl PathOperations {
)
}
})
.flat_map(|type_path| type_path.path.get_ident())
.collect::<Vec<_>>(),
.flat_map(|type_path| type_path.path.get_ident()),
_ => {
abort_call_site!("unexpected argument type, expected Path<...> with angle brakets")
}
Expand Down Expand Up @@ -168,9 +139,9 @@ impl Parse for Path {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
let path = input.parse::<LitStr>()?.value();

// ignore rest of the tokens from actix-web path attribute macro
input.step(|cursor| {
let mut rest = *cursor;
// ignore rest of the tokens from actix_web path attribute macro
while let Some((tt, next)) = rest.token_tree() {
rest = next;
}
Expand Down
26 changes: 11 additions & 15 deletions utoipa-gen/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,25 +89,21 @@ impl<'p> PathAttr<'p> {
}
});

// add argument to the parameters if argument has a name and it does not exists in parameters
arguments
.into_iter()
.filter(|argument| argument.has_name())
.for_each(|argument| {
// cannot use filter() for mutli borrow situation. :(
if !parameters.iter().any(|parameter| {
argument.name.as_ref() == Some(&Cow::Borrowed(&*parameter.name))
}) {
// if parameters does not contain argument
parameters.push(argument.into())
}
});
// add argument to the parameters if argument does not exists in parameters
arguments.into_iter().for_each(|argument| {
// cannot use filter() for mutli borrow situation. :(
if !parameters.iter().any(|parameter| {
argument.name.as_ref() == Some(&Cow::Borrowed(&*parameter.name))
}) {
// if parameters does not contain argument
parameters.push(argument.into())
}
});
} else {
// no parameters at all, add arguments to the parameters if argument has a name
// no parameters at all, add arguments to the parameters
let mut params = Vec::with_capacity(arguments.len());
arguments
.into_iter()
.filter(|argument| argument.has_name())
.map(Parameter::from)
.for_each(|parameter| params.push(parameter));
self.params = Some(params);
Expand Down

0 comments on commit d4e0657

Please sign in to comment.