From e5e12ebb5c6da4bf2f887f18976023a471bad4e4 Mon Sep 17 00:00:00 2001 From: Hicham Azimani Date: Tue, 30 May 2023 02:45:28 +0200 Subject: [PATCH] feat: add support for PathBuf type (#502) --- core/src/v2/schema.rs | 3 ++- tests/test_app.rs | 52 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/core/src/v2/schema.rs b/core/src/v2/schema.rs index 99a36eb82..0baa2ae76 100644 --- a/core/src/v2/schema.rs +++ b/core/src/v2/schema.rs @@ -170,6 +170,7 @@ impl<'a, T: TypedData> TypedData for &'a T { impl_type_simple!(char, DataType::String); impl_type_simple!(String, DataType::String); +impl_type_simple!(PathBuf, DataType::String); impl_type_simple!(bool, DataType::Boolean); impl_type_simple!(f32, DataType::Number, DataTypeFormat::Float); impl_type_simple!(f64, DataType::Number, DataTypeFormat::Double); @@ -472,7 +473,7 @@ macro_rules! impl_schema_map { } use crate::v2::models::Parameter; -use std::collections::*; +use std::{collections::*, path::PathBuf}; impl_schema_array!(Vec); impl_schema_array!(HashSet); diff --git a/tests/test_app.rs b/tests/test_app.rs index accf67b44..e69049d89 100644 --- a/tests/test_app.rs +++ b/tests/test_app.rs @@ -58,6 +58,7 @@ use validator::Validate; use std::{ collections::{BTreeMap, HashMap, HashSet}, + path::PathBuf, sync::mpsc, thread, }; @@ -4561,6 +4562,13 @@ fn test_example() { name: String, } + #[derive(Deserialize, Serialize, Apiv2Schema)] + struct ImageFile { + /// filename. + #[openapi(example = "batman.png")] + path: PathBuf, + } + #[api_v2_operation] fn echo_pets() -> impl Future>, Error>> { fut_ok(web::Json(vec![])) @@ -4571,12 +4579,18 @@ fn test_example() { fut_ok(web::Json(vec![])) } + #[api_v2_operation] + fn echo_files() -> impl Future>, Error>> { + fut_ok(web::Json(vec![])) + } + run_and_check_app( || { App::new() .wrap_api() .route("/pets", web::get().to(echo_pets)) .route("/cars", web::get().to(echo_cars)) + .route("/files", web::get().to(echo_files)) .with_json_spec_at("/api/spec") .build() }, @@ -4625,6 +4639,19 @@ fn test_example() { "name" ], "type": "object" + }, + "ImageFile": { + "properties": { + "path": { + "description": "filename.", + "example": "batman.png", + "type": "string" + } + }, + "required": [ + "path", + ], + "type": "object" } }, "info": { @@ -4661,6 +4688,21 @@ fn test_example() { } } } + }, + "/files": { + "get": { + "responses": { + "200": { + "description": "OK", + "schema": { + "items": { + "$ref": "#/definitions/ImageFile" + }, + "type": "array" + } + } + } + } } }, "swagger": "2.0" @@ -4681,10 +4723,13 @@ mod module_path_in_definition_name { } pub mod other_bar { + use std::path::PathBuf; + #[derive(serde::Serialize, paperclip::actix::Apiv2Schema)] pub struct Baz { pub a: String, pub b: bool, + pub c: PathBuf, } } } @@ -4919,6 +4964,7 @@ fn test_module_path_in_definition_name() { web::Json(module_path_in_definition_name::foo::other_bar::Baz { a: String::default(), b: true, + c: PathBuf::default(), }) } @@ -4965,11 +5011,15 @@ fn test_module_path_in_definition_name() { }, "b": { "type": "boolean" + }, + "c": { + "type": "string" } }, "required": [ "a", - "b" + "b", + "c" ], "type": "object" }