Repository: netwo-io/apistos
Pull request #81 (2024-05-06 08:34:46)

Add test for all kind of query


Patch:
diff --git a/apistos/Cargo.toml b/apistos/Cargo.toml
index 51dad66..de7ffff 100644
--- a/apistos/Cargo.toml
+++ b/apistos/Cargo.toml
@@ -35,6 +35,9 @@ apistos-swagger-ui = { path = "../apistos-swagger-ui", version = "0.2.2", option
 
 
 [dev-dependencies]
+actix-web-lab = { workspace = true }
+garde-actix-web = { workspace = true }
+
 apistos-models = { path = "../apistos-models", version = "0.2.2", features = ["deserialize"] }
 apistos-rapidoc = { path = "../apistos-rapidoc", version = "0.2.2" }
 apistos-redoc = { path = "../apistos-redoc", version = "0.2.2" }
diff --git a/apistos/src/lib.rs b/apistos/src/lib.rs
index b1b1ec1..3db2547 100644
--- a/apistos/src/lib.rs
+++ b/apistos/src/lib.rs
@@ -168,3 +168,9 @@ pub mod actix;
 pub mod app;
 pub mod spec;
 pub mod web;
+
+#[cfg(test)]
+mod test {
+  use actix_web_lab as _;
+  use garde_actix_web as _;
+}


Test patch:
diff --git a/apistos/tests/default_parameters.rs b/apistos/tests/default_parameters.rs
index f35bd12..c18b2ff 100644
--- a/apistos/tests/default_parameters.rs
+++ b/apistos/tests/default_parameters.rs
@@ -175,11 +175,13 @@ async fn default_parameters() {
 
 // Imports bellow aim at making clippy happy. Those dependencies are necessary for integration-test.
 use actix_service as _;
+use actix_web_lab as _;
 use apistos_core as _;
 use apistos_plugins as _;
 use apistos_rapidoc as _;
 use apistos_redoc as _;
 use apistos_swagger_ui as _;
+use garde_actix_web as _;
 use indexmap as _;
 use log as _;
 use md5 as _;
diff --git a/apistos/tests/operation_id.rs b/apistos/tests/operation_id.rs
index c81738e..d8f20c2 100644
--- a/apistos/tests/operation_id.rs
+++ b/apistos/tests/operation_id.rs
@@ -138,11 +138,13 @@ async fn default_operation_id() {
 
 // Imports bellow aim at making clippy happy. Those dependencies are necessary for integration-test.
 use actix_service as _;
+use actix_web_lab as _;
 use apistos_core as _;
 use apistos_plugins as _;
 use apistos_rapidoc as _;
 use apistos_redoc as _;
 use apistos_swagger_ui as _;
+use garde_actix_web as _;
 use indexmap as _;
 use log as _;
 use md5 as _;
diff --git a/apistos/tests/parametric_handlers.rs b/apistos/tests/parametric_handlers.rs
index ac23534..23b38c5 100644
--- a/apistos/tests/parametric_handlers.rs
+++ b/apistos/tests/parametric_handlers.rs
@@ -180,11 +180,13 @@ async fn path_parameter_replacement() {
 
 // Imports bellow aim at making clippy happy. Those dependencies are necessary for integration-test.
 use actix_service as _;
+use actix_web_lab as _;
 use apistos_core as _;
 use apistos_plugins as _;
 use apistos_rapidoc as _;
 use apistos_redoc as _;
 use apistos_swagger_ui as _;
+use garde_actix_web as _;
 use indexmap as _;
 use log as _;
 use md5 as _;
diff --git a/apistos/tests/path_parameters.rs b/apistos/tests/path_parameters.rs
index 2860258..12173a0 100644
--- a/apistos/tests/path_parameters.rs
+++ b/apistos/tests/path_parameters.rs
@@ -130,11 +130,13 @@ async fn path_parameter_replacement() {
 
 // Imports bellow aim at making clippy happy. Those dependencies are necessary for integration-test.
 use actix_service as _;
+use actix_web_lab as _;
 use apistos_core as _;
 use apistos_plugins as _;
 use apistos_rapidoc as _;
 use apistos_redoc as _;
 use apistos_swagger_ui as _;
+use garde_actix_web as _;
 use indexmap as _;
 use log as _;
 use md5 as _;
diff --git a/apistos/tests/query_parameters.rs b/apistos/tests/query_parameters.rs
index 2870458..fe8ceda 100644
--- a/apistos/tests/query_parameters.rs
+++ b/apistos/tests/query_parameters.rs
@@ -2,8 +2,14 @@
 #![allow(clippy::panic)]
 
 use actix_web::web::Query;
+#[cfg(feature = "lab_query")]
+use actix_web_lab::extract::Query as LabQuery;
 use apistos_core::ApiComponent;
 use apistos_gen::ApiComponent;
+#[cfg(all(feature = "lab_query", feature = "garde"))]
+use garde_actix_web::web::LabQuery as GardeLabQuery;
+#[cfg(feature = "garde")]
+use garde_actix_web::web::Query as GardeQuery;
 use schemars::JsonSchema;
 use serde::{Deserialize, Serialize};
 
@@ -57,6 +63,159 @@ async fn query_parameters() {
   assert_eq!(offset_parameter.required, Some(false));
 }
 
+#[cfg(feature = "lab_query")]
+#[actix_web::test]
+async fn lab_query_parameters() {
+  #[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, ApiComponent, JsonSchema)]
+  pub(crate) enum StatusQuery {
+    Active,
+    Inactive,
+  }
+
+  #[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, ApiComponent, JsonSchema)]
+  pub(crate) struct PaginationQuery {
+    pub(crate) limit: u32,
+    pub(crate) offset: Option<u32>,
+  }
+
+  #[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, ApiComponent, JsonSchema)]
+  pub(crate) struct StructQuery {
+    pub(crate) test: Option<String>,
+    pub(crate) status: Option<StatusQuery>,
+    #[serde(flatten)]
+    pub(crate) pagination: PaginationQuery,
+  }
+
+  let parameters = <LabQuery<StructQuery> as ApiComponent>::parameters();
+  assert_eq!(parameters.len(), 4);
+
+  let test_parameter = parameters
+    .iter()
+    .find(|p| p.name == *"test")
+    .expect("Unable to retrieve test parameter");
+  assert_eq!(test_parameter.required, Some(false));
+
+  let status_parameter = parameters
+    .iter()
+    .find(|p| p.name == *"status")
+    .expect("Unable to retrieve status parameter");
+  assert_eq!(status_parameter.required, Some(false));
+
+  let limit_parameter = parameters
+    .iter()
+    .find(|p| p.name == *"limit")
+    .expect("Unable to retrieve limit parameter");
+  assert_eq!(limit_parameter.required, Some(true));
+
+  let offset_parameter = parameters
+    .iter()
+    .find(|p| p.name == *"offset")
+    .expect("Unable to retrieve offset parameter");
+  assert_eq!(offset_parameter.required, Some(false));
+}
+
+#[cfg(feature = "garde")]
+#[actix_web::test]
+async fn garde_query_parameters() {
+  #[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, ApiComponent, JsonSchema)]
+  pub(crate) enum StatusQuery {
+    Active,
+    Inactive,
+  }
+
+  #[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, ApiComponent, JsonSchema)]
+  pub(crate) struct PaginationQuery {
+    pub(crate) limit: u32,
+    pub(crate) offset: Option<u32>,
+  }
+
+  #[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, ApiComponent, JsonSchema)]
+  pub(crate) struct StructQuery {
+    pub(crate) test: Option<String>,
+    pub(crate) status: Option<StatusQuery>,
+    #[serde(flatten)]
+    pub(crate) pagination: PaginationQuery,
+  }
+
+  let parameters = <GardeQuery<StructQuery> as ApiComponent>::parameters();
+  assert_eq!(parameters.len(), 4);
+
+  let test_parameter = parameters
+    .iter()
+    .find(|p| p.name == *"test")
+    .expect("Unable to retrieve test parameter");
+  assert_eq!(test_parameter.required, Some(false));
+
+  let status_parameter = parameters
+    .iter()
+    .find(|p| p.name == *"status")
+    .expect("Unable to retrieve status parameter");
+  assert_eq!(status_parameter.required, Some(false));
+
+  let limit_parameter = parameters
+    .iter()
+    .find(|p| p.name == *"limit")
+    .expect("Unable to retrieve limit parameter");
+  assert_eq!(limit_parameter.required, Some(true));
+
+  let offset_parameter = parameters
+    .iter()
+    .find(|p| p.name == *"offset")
+    .expect("Unable to retrieve offset parameter");
+  assert_eq!(offset_parameter.required, Some(false));
+}
+
+#[cfg(all(feature = "lab_query", feature = "garde"))]
+#[actix_web::test]
+async fn garde_lab_query_parameters() {
+  #[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, ApiComponent, JsonSchema)]
+  pub(crate) enum StatusQuery {
+    Active,
+    Inactive,
+  }
+
+  #[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, ApiComponent, JsonSchema)]
+  pub(crate) struct PaginationQuery {
+    pub(crate) limit: u32,
+    pub(crate) offset: Option<u32>,
+  }
+
+  #[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, ApiComponent, JsonSchema)]
+  pub(crate) struct StructQuery {
+    pub(crate) test: Option<String>,
+    pub(crate) status: Option<StatusQuery>,
+    #[serde(flatten)]
+    pub(crate) pagination: PaginationQuery,
+  }
+
+  let parameters = <GardeLabQuery<StructQuery> as ApiComponent>::parameters();
+  assert_eq!(parameters.len(), 4);
+
+  let test_parameter = parameters
+    .iter()
+    .find(|p| p.name == *"test")
+    .expect("Unable to retrieve test parameter");
+  assert_eq!(test_parameter.required, Some(false));
+
+  let status_parameter = parameters
+    .iter()
+    .find(|p| p.name == *"status")
+    .expect("Unable to retrieve status parameter");
+  assert_eq!(status_parameter.required, Some(false));
+
+  let limit_parameter = parameters
+    .iter()
+    .find(|p| p.name == *"limit")
+    .expect("Unable to retrieve limit parameter");
+  assert_eq!(limit_parameter.required, Some(true));
+
+  let offset_parameter = parameters
+    .iter()
+    .find(|p| p.name == *"offset")
+    .expect("Unable to retrieve offset parameter");
+  assert_eq!(offset_parameter.required, Some(false));
+}
+
 #[actix_web::test]
 async fn query_parameters_with_flatten_enums() {
   #[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, ApiComponent, JsonSchema)]
diff --git a/apistos/tests/tags.rs b/apistos/tests/tags.rs
index 57be187..dea3694 100644
--- a/apistos/tests/tags.rs
+++ b/apistos/tests/tags.rs
@@ -142,11 +142,13 @@ async fn tags() {
 
 // Imports bellow aim at making clippy happy. Those dependencies are necessary for integration-test.
 use actix_service as _;
+use actix_web_lab as _;
 use apistos_core as _;
 use apistos_plugins as _;
 use apistos_rapidoc as _;
 use apistos_redoc as _;
 use apistos_swagger_ui as _;
+use garde_actix_web as _;
 use indexmap as _;
 use log as _;
 use md5 as _;