Skip to content

Commit

Permalink
feat: add support for PathBuf type (#502)
Browse files Browse the repository at this point in the history
  • Loading branch information
AzHicham authored May 30, 2023
1 parent 306c89f commit e5e12eb
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
3 changes: 2 additions & 1 deletion core/src/v2/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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<T>);
impl_schema_array!(HashSet<T>);
Expand Down
52 changes: 51 additions & 1 deletion tests/test_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ use validator::Validate;

use std::{
collections::{BTreeMap, HashMap, HashSet},
path::PathBuf,
sync::mpsc,
thread,
};
Expand Down Expand Up @@ -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<Output = Result<web::Json<Vec<Pet>>, Error>> {
fut_ok(web::Json(vec![]))
Expand All @@ -4571,12 +4579,18 @@ fn test_example() {
fut_ok(web::Json(vec![]))
}

#[api_v2_operation]
fn echo_files() -> impl Future<Output = Result<web::Json<Vec<ImageFile>>, 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()
},
Expand Down Expand Up @@ -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": {
Expand Down Expand Up @@ -4661,6 +4688,21 @@ fn test_example() {
}
}
}
},
"/files": {
"get": {
"responses": {
"200": {
"description": "OK",
"schema": {
"items": {
"$ref": "#/definitions/ImageFile"
},
"type": "array"
}
}
}
}
}
},
"swagger": "2.0"
Expand All @@ -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,
}
}
}
Expand Down Expand Up @@ -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(),
})
}

Expand Down Expand Up @@ -4965,11 +5011,15 @@ fn test_module_path_in_definition_name() {
},
"b": {
"type": "boolean"
},
"c": {
"type": "string"
}
},
"required": [
"a",
"b"
"b",
"c"
],
"type": "object"
}
Expand Down

0 comments on commit e5e12eb

Please sign in to comment.