From 752c8b13f63c2b61b356560271914b5b94b535df Mon Sep 17 00:00:00 2001 From: Mattias Eriksson Date: Mon, 12 Dec 2022 15:54:06 +0100 Subject: [PATCH 1/3] Strip template patterns from paths, and add it to parameters --- core/src/v2/models.rs | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/core/src/v2/models.rs b/core/src/v2/models.rs index d8cd61535..02db3b8d8 100644 --- a/core/src/v2/models.rs +++ b/core/src/v2/models.rs @@ -1,5 +1,6 @@ //! Models used by OpenAPI v2. + pub use super::extensions::{ Coder, Coders, MediaRange, JSON_CODER, JSON_MIME, YAML_CODER, YAML_MIME, }; @@ -9,6 +10,7 @@ use crate::error::ValidationError; use once_cell::sync::Lazy; use paperclip_macros::api_v2_schema_struct; use regex::{Captures, Regex}; +use serde::ser::{Serializer, SerializeMap}; #[cfg(feature = "actix-base")] use actix_web::http::Method; @@ -136,6 +138,28 @@ pub type ResolvableApi = Api, ResolvableResponse, R /// OpenAPI v2 spec with defaults. pub type DefaultApiRaw = Api; +fn strip_templates_from_paths(tree: &BTreeMap>, serializer: S) -> Result { + let len = tree.len(); + let mut map = serializer.serialize_map(Some(len))?; + for (k,v) in tree { + let path = strip_pattern_from_template(&k); + map.serialize_entry(&path, v)?; + } + map.end() +} + +fn strip_pattern_from_template(path: &str) -> String { + let mut clean_path = path.to_string(); + for cap in PATH_TEMPLATE_REGEX.captures_iter(&path) { + let name_only = cap[1].split_once(':').map(|t| t.0.to_string()).unwrap_or(cap[1].to_string()); + if cap[1] != name_only { + clean_path = clean_path.replace(format!("{{{}}}", &cap[1]).as_str(), format!("{{{}}}", name_only).as_str()); + } + + } + clean_path +} + /// OpenAPI v2 (swagger) spec generic over parameter and schema. /// /// @@ -144,6 +168,7 @@ pub struct Api { pub swagger: Version, #[serde(default = "BTreeMap::new")] pub definitions: BTreeMap, + #[serde(serialize_with = "strip_templates_from_paths")] pub paths: BTreeMap>, #[serde(skip_serializing_if = "Option::is_none")] pub host: Option, @@ -707,7 +732,12 @@ impl Operation, Response> { .rev() { if let Some(n) = names.pop() { - p.name = n.split_once(':').map(|t| t.0.to_string()).unwrap_or(n); + if let Some((name, pattern)) = n.split_once(':') { + p.name = name.to_string(); + p.pattern = Some(pattern.to_string()); + } else { + p.name = n; + } } else { break; } From ce67c97492ff2d8192457f44041f65adbd38aa92 Mon Sep 17 00:00:00 2001 From: Mattias Eriksson Date: Mon, 12 Dec 2022 15:55:27 +0100 Subject: [PATCH 2/3] fmt --- core/src/v2/models.rs | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/core/src/v2/models.rs b/core/src/v2/models.rs index 02db3b8d8..99745cec3 100644 --- a/core/src/v2/models.rs +++ b/core/src/v2/models.rs @@ -1,6 +1,5 @@ //! Models used by OpenAPI v2. - pub use super::extensions::{ Coder, Coders, MediaRange, JSON_CODER, JSON_MIME, YAML_CODER, YAML_MIME, }; @@ -10,7 +9,7 @@ use crate::error::ValidationError; use once_cell::sync::Lazy; use paperclip_macros::api_v2_schema_struct; use regex::{Captures, Regex}; -use serde::ser::{Serializer, SerializeMap}; +use serde::ser::{SerializeMap, Serializer}; #[cfg(feature = "actix-base")] use actix_web::http::Method; @@ -138,10 +137,13 @@ pub type ResolvableApi = Api, ResolvableResponse, R /// OpenAPI v2 spec with defaults. pub type DefaultApiRaw = Api; -fn strip_templates_from_paths(tree: &BTreeMap>, serializer: S) -> Result { +fn strip_templates_from_paths( + tree: &BTreeMap>, + serializer: S, +) -> Result { let len = tree.len(); let mut map = serializer.serialize_map(Some(len))?; - for (k,v) in tree { + for (k, v) in tree { let path = strip_pattern_from_template(&k); map.serialize_entry(&path, v)?; } @@ -149,15 +151,20 @@ fn strip_templates_from_paths String { - let mut clean_path = path.to_string(); - for cap in PATH_TEMPLATE_REGEX.captures_iter(&path) { - let name_only = cap[1].split_once(':').map(|t| t.0.to_string()).unwrap_or(cap[1].to_string()); - if cap[1] != name_only { - clean_path = clean_path.replace(format!("{{{}}}", &cap[1]).as_str(), format!("{{{}}}", name_only).as_str()); - } - - } - clean_path + let mut clean_path = path.to_string(); + for cap in PATH_TEMPLATE_REGEX.captures_iter(&path) { + let name_only = cap[1] + .split_once(':') + .map(|t| t.0.to_string()) + .unwrap_or(cap[1].to_string()); + if cap[1] != name_only { + clean_path = clean_path.replace( + format!("{{{}}}", &cap[1]).as_str(), + format!("{{{}}}", name_only).as_str(), + ); + } + } + clean_path } /// OpenAPI v2 (swagger) spec generic over parameter and schema. From bb77a6d2fbe4134afe5c184390103cd8d86c364e Mon Sep 17 00:00:00 2001 From: Mattias Eriksson Date: Mon, 12 Dec 2022 16:06:36 +0100 Subject: [PATCH 3/3] Clippy --- core/src/v2/models.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/src/v2/models.rs b/core/src/v2/models.rs index 99745cec3..44c16421b 100644 --- a/core/src/v2/models.rs +++ b/core/src/v2/models.rs @@ -144,7 +144,7 @@ fn strip_templates_from_paths String { let mut clean_path = path.to_string(); - for cap in PATH_TEMPLATE_REGEX.captures_iter(&path) { + for cap in PATH_TEMPLATE_REGEX.captures_iter(path) { let name_only = cap[1] .split_once(':') .map(|t| t.0.to_string()) - .unwrap_or(cap[1].to_string()); + .unwrap_or_else(|| cap[1].to_string()); if cap[1] != name_only { clean_path = clean_path.replace( format!("{{{}}}", &cap[1]).as_str(),