Skip to content

Commit

Permalink
Rename auto_types feature flag (#665)
Browse files Browse the repository at this point in the history
Rename `auto_types` feature flag to `auto_into_responses` since it
actually is only used for automatic IntoResponses definition. This
feature is higly experiemental and should be used with cauntion. It is
more likely that the final implementation will look different thus
changes are to be expected.

Simultaneusly allow automatic resolvation for request bodies without
extra feature flag. Supported only with `actix_extras` feature by now.
  • Loading branch information
juhaku authored Jul 4, 2023
1 parent 7cf45ce commit 2979ce9
Show file tree
Hide file tree
Showing 14 changed files with 27 additions and 35 deletions.
6 changes: 3 additions & 3 deletions scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ if [[ "$crate" == "utoipa" ]]; then
elif [[ "$crate" == "utoipa-gen" ]]; then
cargo test -p utoipa-gen --features utoipa/actix_extras,chrono,decimal,utoipa/uuid,utoipa/time,time,utoipa/repr

cargo test -p utoipa-gen --test path_derive_auto_types --features auto_types
cargo test -p utoipa-gen --test path_derive_auto_into_responses --features auto_into_responses
cargo test -p utoipa-gen --test path_derive_actix --test path_parameter_derive_actix --features actix_extras
cargo test -p utoipa-gen --test path_derive_auto_types_actix --features actix_extras,auto_types
cargo test -p utoipa-gen --test path_derive_auto_into_responses_actix --features actix_extras,utoipa/auto_into_responses

cargo test -p utoipa-gen --test path_derive_rocket --features rocket_extras

cargo test -p utoipa-gen --test path_derive_axum_test --features axum_extras
cargo test -p utoipa-gen --test path_derive_auto_types_axum --features axum_extras,auto_types
cargo test -p utoipa-gen --test path_derive_auto_into_responses_axum --features axum_extras,utoipa/auto_into_responses
elif [[ "$crate" == "utoipa-swagger-ui" ]]; then
cargo test -p utoipa-swagger-ui --features actix-web,rocket,axum
fi
4 changes: 3 additions & 1 deletion utoipa-gen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,6 @@ time = []
smallvec = []
repr = []
indexmap = []
auto_types = []

# EXPERIEMENTAL! use with cauntion
auto_into_responses = []
2 changes: 1 addition & 1 deletion utoipa-gen/src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use syn::{punctuated::Punctuated, token::Comma, ItemFn};
use crate::component::{ComponentSchema, ComponentSchemaProps, TypeTree};
use crate::path::{PathOperation, PathTypeTree};

#[cfg(feature = "auto_types")]
#[cfg(feature = "auto_into_responses")]
pub mod auto_types;

#[cfg(feature = "actix_extras")]
Expand Down
14 changes: 2 additions & 12 deletions utoipa-gen/src/ext/actix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ fn split_path_args_and_request(
impl Iterator<Item = TypeTree>,
impl Iterator<Item = TypeTree>,
) {
let (path_args, _body_types): (Vec<FnArg>, Vec<FnArg>) = value_args
let (path_args, body_types): (Vec<FnArg>, Vec<FnArg>) = value_args
.into_iter()
.filter(|arg| {
arg.ty.is("Path") || arg.ty.is("Json") || arg.ty.is("Form") || arg.ty.is("Bytes")
Expand All @@ -98,17 +98,7 @@ fn split_path_args_and_request(
unreachable!("Value arguments does not have ValueType::Object arguments")
}
}),
{
#[cfg(feature = "auto_types")]
{
_body_types.into_iter().map(|json| json.ty)
}

#[cfg(not(feature = "auto_types"))]
{
std::iter::empty()
}
},
body_types.into_iter().map(|json| json.ty),
)
}

Expand Down
9 changes: 4 additions & 5 deletions utoipa-gen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1296,14 +1296,14 @@ pub fn path(attr: TokenStream, item: TokenStream) -> TokenStream {
feature = "actix_extras",
feature = "rocket_extras",
feature = "axum_extras",
feature = "auto_types"
feature = "auto_into_responses"
))]
let mut path_attribute = path_attribute;

let ast_fn = syn::parse::<ItemFn>(item).unwrap_or_abort();
let fn_name = &*ast_fn.sig.ident.to_string();

#[cfg(feature = "auto_types")]
#[cfg(feature = "auto_into_responses")]
{
if let Some(responses) = ext::auto_types::parse_fn_operation_responses(&ast_fn) {
path_attribute.responses_from_into_responses(responses);
Expand Down Expand Up @@ -1334,14 +1334,13 @@ pub fn path(attr: TokenStream, item: TokenStream) -> TokenStream {
{
use ext::ArgumentResolver;
let args = resolved_path.as_mut().map(|path| mem::take(&mut path.args));
let (arguments, into_params_types, _body) =
let (arguments, into_params_types, body) =
PathOperations::resolve_arguments(&ast_fn.sig.inputs, args);

path_attribute.update_parameters(arguments);
path_attribute.update_parameters_parameter_in(into_params_types);

#[cfg(feature = "auto_types")]
path_attribute.update_request_body(_body);
path_attribute.update_request_body(body);
}

let path = Path::new(path_attribute, fn_name)
Expand Down
10 changes: 6 additions & 4 deletions utoipa-gen/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,20 +105,23 @@ impl<'p> PathAttr<'p> {
}
}

#[cfg(feature = "auto_types")]
#[cfg(feature = "auto_into_responses")]
pub fn responses_from_into_responses(&mut self, ty: &'p syn::TypePath) {
self.responses
.push(Response::IntoResponses(Cow::Borrowed(ty)))
}

#[cfg(feature = "auto_types")]
#[cfg(any(
feature = "actix_extras",
feature = "rocket_extras",
feature = "axum_extras"
))]
pub fn update_request_body(&mut self, request_body: Option<crate::ext::RequestBody<'p>>) {
self.request_body = request_body.map(RequestBody::Ext);
use std::mem;

self.request_body = request_body
.map(RequestBody::Ext)
.or(mem::take(&mut self.request_body));
}

#[cfg(any(
Expand All @@ -143,7 +146,6 @@ impl<'p> PathAttr<'p> {
Parameter::Struct(parameter) => Some(parameter),
})
.for_each(|parameter| {

if let Some(into_params_argument) =
into_params_types
.iter_mut()
Expand Down
2 changes: 0 additions & 2 deletions utoipa-gen/src/path/request_body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use super::{PathType, PathTypeTree};
#[cfg_attr(feature = "debug", derive(Debug))]
pub enum RequestBody<'r> {
Parsed(RequestBodyAttr<'r>),
#[cfg(feature = "auto_types")]
#[cfg(any(
feature = "actix_extras",
feature = "rocket_extras",
Expand All @@ -27,7 +26,6 @@ impl ToTokens for RequestBody<'_> {
fn to_tokens(&self, tokens: &mut TokenStream2) {
match self {
Self::Parsed(parsed) => parsed.to_tokens(tokens),
#[cfg(feature = "auto_types")]
#[cfg(any(
feature = "actix_extras",
feature = "rocket_extras",
Expand Down
2 changes: 0 additions & 2 deletions utoipa-gen/tests/path_derive.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![cfg(not(feature = "auto_types"))]

use std::collections::BTreeMap;

use assert_json_diff::{assert_json_eq, assert_json_matches, CompareMode, Config, NumericMode};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg(feature = "auto_types")]
#![cfg(feature = "auto_into_responses")]

use assert_json_diff::assert_json_eq;
use utoipa::OpenApi;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg(all(feature = "auto_types", feature = "actix_extras"))]
#![cfg(all(feature = "auto_into_responses", feature = "actix_extras"))]

use actix_web::web::{Form, Json};
use std::fmt::Display;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg(all(feature = "auto_types", feature = "axum_extras"))]
#![cfg(all(feature = "auto_into_responses", feature = "axum_extras"))]

use assert_json_diff::assert_json_eq;
use utoipa::OpenApi;
Expand Down
1 change: 0 additions & 1 deletion utoipa-gen/tests/path_parameter_derive_actix.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![cfg(feature = "actix_extras")]
#![cfg(not(feature = "auto_types"))]

use utoipa::OpenApi;

Expand Down
4 changes: 3 additions & 1 deletion utoipa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ indexmap = ["utoipa-gen/indexmap"]
openapi_extensions = []
repr = ["utoipa-gen/repr"]
preserve_order = []
auto_types = ["utoipa-gen/auto_types"]
preserve_path_order = []

# EXPERIEMENTAL! use with cauntion
auto_into_responses = ["utoipa-gen/auto_into_responses"]

[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0" }
Expand Down
2 changes: 2 additions & 0 deletions utoipa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,7 @@ pub trait IntoResponses {
fn responses() -> BTreeMap<String, openapi::RefOr<openapi::response::Response>>;
}

#[cfg(feature = "auto_into_responses")]
impl<T: IntoResponses, E: IntoResponses> IntoResponses for Result<T, E> {
fn responses() -> BTreeMap<String, openapi::RefOr<openapi::response::Response>> {
let mut responses = T::responses();
Expand All @@ -887,6 +888,7 @@ impl<T: IntoResponses, E: IntoResponses> IntoResponses for Result<T, E> {
}
}

#[cfg(feature = "auto_into_responses")]
impl IntoResponses for () {
fn responses() -> BTreeMap<String, openapi::RefOr<openapi::response::Response>> {
BTreeMap::new()
Expand Down

0 comments on commit 2979ce9

Please sign in to comment.