mirror of
https://gitlab.com/msrd0/gotham-restful.git
synced 2025-02-23 04:52:28 +00:00
infer query parameters into openapi spec
This commit is contained in:
parent
92410ddd5b
commit
de7c3cb015
1 changed files with 32 additions and 10 deletions
|
@ -21,8 +21,8 @@ use log::error;
|
||||||
use mime::{APPLICATION_JSON, TEXT_PLAIN};
|
use mime::{APPLICATION_JSON, TEXT_PLAIN};
|
||||||
use openapiv3::{
|
use openapiv3::{
|
||||||
Components, MediaType, OpenAPI, Operation, Parameter, ParameterData, ParameterSchemaOrContent, PathItem,
|
Components, MediaType, OpenAPI, Operation, Parameter, ParameterData, ParameterSchemaOrContent, PathItem,
|
||||||
PathStyle, Paths, ReferenceOr, ReferenceOr::Item, ReferenceOr::Reference, RequestBody, Response, Responses,
|
Paths, ReferenceOr, ReferenceOr::Item, ReferenceOr::Reference, RequestBody, Response, Responses, Schema,
|
||||||
Schema, Server, StatusCode
|
SchemaKind, Server, StatusCode, Type
|
||||||
};
|
};
|
||||||
use serde::de::DeserializeOwned;
|
use serde::de::DeserializeOwned;
|
||||||
use std::panic::RefUnwindSafe;
|
use std::panic::RefUnwindSafe;
|
||||||
|
@ -221,24 +221,46 @@ impl<'a> OperationParams<'a>
|
||||||
example: None,
|
example: None,
|
||||||
examples: IndexMap::new()
|
examples: IndexMap::new()
|
||||||
},
|
},
|
||||||
style: PathStyle::default(),
|
style: Default::default(),
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_query_params(&self, params : &mut Vec<ReferenceOr<Parameter>>)
|
fn add_query_params(self, params : &mut Vec<ReferenceOr<Parameter>>)
|
||||||
{
|
{
|
||||||
let query_params = match &self.query_params {
|
let query_params = match self.query_params {
|
||||||
Some(qp) => qp,
|
Some(qp) => qp.schema,
|
||||||
None => return
|
None => return
|
||||||
};
|
};
|
||||||
// TODO
|
let query_params = match query_params {
|
||||||
|
SchemaKind::Type(Type::Object(ty)) => ty,
|
||||||
|
_ => panic!("Query Parameters needs to be a plain struct")
|
||||||
|
};
|
||||||
|
for (name, schema) in query_params.properties
|
||||||
|
{
|
||||||
|
let required = query_params.required.contains(&name);
|
||||||
|
params.push(Item(Parameter::Query {
|
||||||
|
parameter_data: ParameterData {
|
||||||
|
name,
|
||||||
|
description: None,
|
||||||
|
required,
|
||||||
|
deprecated: None,
|
||||||
|
format: ParameterSchemaOrContent::Schema(schema.unbox()),
|
||||||
|
example: None,
|
||||||
|
examples: IndexMap::new()
|
||||||
|
},
|
||||||
|
allow_reserved: false,
|
||||||
|
style: Default::default(),
|
||||||
|
allow_empty_value: None
|
||||||
|
}))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn params(&self) -> Vec<ReferenceOr<Parameter>>
|
fn into_params(self) -> Vec<ReferenceOr<Parameter>>
|
||||||
{
|
{
|
||||||
let mut params : Vec<ReferenceOr<Parameter>> = Vec::new();
|
let mut params : Vec<ReferenceOr<Parameter>> = Vec::new();
|
||||||
self.add_path_params(&mut params);
|
self.add_path_params(&mut params);
|
||||||
|
self.add_query_params(&mut params);
|
||||||
params
|
params
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -270,7 +292,7 @@ fn new_operation(default_status : hyper::StatusCode, schema : ReferenceOr<Schema
|
||||||
description: None,
|
description: None,
|
||||||
external_documentation: None,
|
external_documentation: None,
|
||||||
operation_id: None, // TODO
|
operation_id: None, // TODO
|
||||||
parameters: params.params(),
|
parameters: params.into_params(),
|
||||||
request_body,
|
request_body,
|
||||||
responses: Responses {
|
responses: Responses {
|
||||||
default: None,
|
default: None,
|
||||||
|
@ -353,7 +375,7 @@ macro_rules! implOpenapiRouter {
|
||||||
|
|
||||||
let path = format!("/{}/search", &self.1);
|
let path = format!("/{}/search", &self.1);
|
||||||
let mut item = (self.0).1.remove_path(&self.1);
|
let mut item = (self.0).1.remove_path(&self.1);
|
||||||
item.get = Some(new_operation(Res::default_status(), schema, OperationParams::default(), None)); // TODO
|
item.get = Some(new_operation(Res::default_status(), schema, OperationParams::from_query_params(Query::schema()), None));
|
||||||
(self.0).1.add_path(path, item);
|
(self.0).1.add_path(path, item);
|
||||||
|
|
||||||
(&mut *(self.0).0, self.1.to_string()).search::<Handler, Query, Res>()
|
(&mut *(self.0).0, self.1.to_string()).search::<Handler, Query, Res>()
|
||||||
|
|
Loading…
Add table
Reference in a new issue