1- use apistos_models :: paths :: { Operation , ParameterDefinition , ParameterIn } ;
2- use apistos_models :: reference_or :: ReferenceOr ;
1+ use std :: borrow :: Cow ;
2+
33use once_cell:: sync:: Lazy ;
44use regex:: { Captures , Regex } ;
5- use schemars:: schema:: { Schema , SchemaObject , StringValidation } ;
6- use std:: borrow:: Cow ;
5+ use serde_json:: json;
6+
7+ use apistos_models:: paths:: { Operation , ParameterDefinition , ParameterIn } ;
8+ use apistos_models:: reference_or:: ReferenceOr ;
9+ use apistos_models:: { ApistosSchema , Schema } ;
10+
11+ use crate :: internal:: get_oas_version;
712
813/// Regex that can be used to fetch templated path parameters.
914#[ allow( clippy:: expect_used) ]
@@ -15,6 +20,8 @@ pub(crate) trait OperationUpdater {
1520
1621impl OperationUpdater for Operation {
1722 fn update_path_parameter_name_from_path ( & mut self , path : & str ) {
23+ let oas_version = get_oas_version ( ) ;
24+
1825 let mut param_names = vec ! [ ] ;
1926 PATH_TEMPLATE_REGEX . replace_all ( path, |c : & Captures | {
2027 param_names. push ( c[ 1 ] . to_owned ( ) ) ;
@@ -31,15 +38,18 @@ impl OperationUpdater for Operation {
3138 if let Some ( n) = param_names. pop ( ) {
3239 if let Some ( ( name, pattern) ) = n. split_once ( ':' ) {
3340 param. name = name. to_string ( ) ;
34- param. definition = Some ( ParameterDefinition :: Schema ( ReferenceOr :: Object ( Schema :: Object (
35- SchemaObject {
36- string : Some ( Box :: new ( StringValidation {
37- pattern : Some ( pattern. to_string ( ) ) ,
38- ..Default :: default ( )
39- } ) ) ,
40- ..Default :: default ( )
41- } ,
42- ) ) ) )
41+
42+ let parameter_definition = Schema :: try_from ( json ! ( {
43+ "type" : "string" ,
44+ "pattern" : pattern
45+ } ) )
46+ . map_err ( |err| {
47+ log:: warn!( "Error generating json schema: {err:?}" ) ;
48+ err
49+ } )
50+ . map ( |sch| ApistosSchema :: new ( sch, oas_version) )
51+ . unwrap_or_default ( ) ;
52+ param. definition = Some ( ParameterDefinition :: Schema ( ReferenceOr :: Object ( parameter_definition) ) )
4353 } else {
4454 param. name = n;
4555 }
@@ -54,10 +64,10 @@ impl OperationUpdater for Operation {
5464mod test {
5565 #![ allow( clippy:: panic) ]
5666
57- use crate :: internal:: actix:: utils:: OperationUpdater ;
5867 use apistos_models:: paths:: { Operation , Parameter , ParameterDefinition , ParameterIn } ;
5968 use apistos_models:: reference_or:: ReferenceOr ;
60- use apistos_models:: Schema ;
69+
70+ use crate :: internal:: actix:: utils:: OperationUpdater ;
6171
6272 #[ test]
6373 fn simple_path_parameter_name_replacement ( ) {
@@ -139,13 +149,14 @@ mod test {
139149 let def = p. definition . clone ( ) . expect ( "missing parameter definition" ) ;
140150 match def {
141151 ParameterDefinition :: Schema ( sch) => match sch {
142- ReferenceOr :: Object ( obj) => match obj {
143- Schema :: Bool ( _) => panic ! ( "expected schema object" ) ,
144- Schema :: Object ( obj) => {
145- let str_obj = obj. string . expect ( "should be a string schema" ) ;
146- assert_eq ! ( str_obj. pattern, Some ( ".+" . to_string( ) ) ) ;
147- }
148- } ,
152+ ReferenceOr :: Object ( obj) => {
153+ let pattern = obj
154+ . inner ( )
155+ . as_object ( )
156+ . and_then ( |obj| obj. get ( "pattern" ) )
157+ . and_then ( |_type| _type. as_str ( ) ) ;
158+ assert_eq ! ( pattern, Some ( ".+" ) ) ;
159+ }
149160 ReferenceOr :: Reference { .. } => panic ! ( "expected schema object" ) ,
150161 } ,
151162 ParameterDefinition :: Content ( _) => panic ! ( "expected schema" ) ,
0 commit comments