From d025183a091203ec736441ad57394fd478ca6b2f Mon Sep 17 00:00:00 2001 From: Dominic Date: Sun, 13 Oct 2019 23:12:12 +0200 Subject: [PATCH] attempt to add query params to openapi --- gotham_restful/src/openapi/router.rs | 81 +++++++++++++++++++++------- 1 file changed, 62 insertions(+), 19 deletions(-) diff --git a/gotham_restful/src/openapi/router.rs b/gotham_restful/src/openapi/router.rs index bcc86f7..01da531 100644 --- a/gotham_restful/src/openapi/router.rs +++ b/gotham_restful/src/openapi/router.rs @@ -177,7 +177,67 @@ fn schema_to_content(schema : ReferenceOr) -> IndexMap, path_params : Vec<&str>, body_schema : Option>) -> Operation +#[derive(Default)] +struct OperationParams<'a> +{ + path_params : Vec<&'a str>, + query_params : Option +} + +impl<'a> OperationParams<'a> +{ + fn new(path_params : Vec<&'a str>, query_params : Option) -> Self + { + Self { path_params, query_params } + } + + fn from_path_params(path_params : Vec<&'a str>) -> Self + { + Self::new(path_params, None) + } + + fn from_query_params(query_params : OpenapiSchema) -> Self + { + Self::new(Vec::new(), Some(query_params)) + } + + fn add_path_params(&self, params : &mut Vec>) + { + for param in self.path_params + { + params.push(Item(Parameter::Path { + parameter_data: ParameterData { + name: param.to_string(), + description: None, + required: true, + deprecated: None, + format: ParameterSchemaOrContent::Schema(Item(String::to_schema().to_schema())), + example: None, + examples: IndexMap::new() + }, + style: PathStyle::default(), + })); + } + } + + fn add_query_params(&self, params : &mut Vec>) + { + let query_params = match self.query_params { + Some(qp) => qp, + None => return + }; + // TODO + } + + fn params(&self) -> Vec> + { + let mut params : Vec> = Vec::new(); + self.add_path_params(&mut params); + params + } +} + +fn new_operation(default_status : hyper::StatusCode, schema : ReferenceOr, params : OperationParams, body_schema : Option>) -> Operation { let content = match default_status.as_u16() { 204 => IndexMap::new(), @@ -192,23 +252,6 @@ fn new_operation(default_status : hyper::StatusCode, schema : ReferenceOr> = Vec::new(); - for param in path_params - { - params.push(Item(Parameter::Path { - parameter_data: ParameterData { - name: param.to_string(), - description: None, - required: true, - deprecated: None, - format: ParameterSchemaOrContent::Schema(Item(String::to_schema().to_schema())), - example: None, - examples: IndexMap::new() - }, - style: PathStyle::default(), - })); - } - let request_body = body_schema.map(|schema| Item(RequestBody { description: None, content: schema_to_content(schema), @@ -221,7 +264,7 @@ fn new_operation(default_status : hyper::StatusCode, schema : ReferenceOr