mirror of
https://gitlab.com/msrd0/gotham-restful.git
synced 2025-04-19 22:44:38 +00:00
respect mime types in openapi router
This commit is contained in:
parent
dd9e10a154
commit
0863d41084
1 changed files with 22 additions and 22 deletions
|
@ -19,7 +19,7 @@ use gotham::{
|
|||
use hyper::Body;
|
||||
use indexmap::IndexMap;
|
||||
use log::error;
|
||||
use mime::{APPLICATION_JSON, TEXT_PLAIN};
|
||||
use mime::{Mime, APPLICATION_JSON, TEXT_PLAIN};
|
||||
use openapiv3::{
|
||||
Components, MediaType, OpenAPI, Operation, Parameter, ParameterData, ParameterSchemaOrContent, PathItem,
|
||||
Paths, ReferenceOr, ReferenceOr::Item, ReferenceOr::Reference, RequestBody as OARequestBody, Response, Responses, Schema,
|
||||
|
@ -172,15 +172,18 @@ pub trait GetOpenapi
|
|||
fn get_openapi(&mut self, path : &str);
|
||||
}
|
||||
|
||||
fn schema_to_content(schema : ReferenceOr<Schema>) -> IndexMap<String, MediaType>
|
||||
fn schema_to_content(types : Vec<Mime>, schema : ReferenceOr<Schema>) -> IndexMap<String, MediaType>
|
||||
{
|
||||
let mut content : IndexMap<String, MediaType> = IndexMap::new();
|
||||
content.insert(APPLICATION_JSON.to_string(), MediaType {
|
||||
schema: Some(schema),
|
||||
for ty in types
|
||||
{
|
||||
content.insert(ty.to_string(), MediaType {
|
||||
schema: Some(schema.clone()),
|
||||
example: None,
|
||||
examples: IndexMap::new(),
|
||||
encoding: IndexMap::new()
|
||||
});
|
||||
}
|
||||
content
|
||||
}
|
||||
|
||||
|
@ -266,12 +269,9 @@ impl<'a> OperationParams<'a>
|
|||
}
|
||||
}
|
||||
|
||||
fn new_operation(default_status : hyper::StatusCode, schema : ReferenceOr<Schema>, params : OperationParams, body_schema : Option<ReferenceOr<Schema>>) -> Operation
|
||||
fn new_operation(default_status : hyper::StatusCode, accepted_types : Option<Vec<Mime>>, schema : ReferenceOr<Schema>, params : OperationParams, body_schema : Option<ReferenceOr<Schema>>, supported_types : Option<Vec<Mime>>) -> Operation
|
||||
{
|
||||
let content = match default_status.as_u16() {
|
||||
204 => IndexMap::new(),
|
||||
_ => schema_to_content(schema)
|
||||
};
|
||||
let content = schema_to_content(accepted_types.unwrap_or_default(), schema);
|
||||
|
||||
let mut responses : IndexMap<StatusCode, ReferenceOr<Response>> = IndexMap::new();
|
||||
responses.insert(StatusCode::Code(default_status.as_u16()), Item(Response {
|
||||
|
@ -283,7 +283,7 @@ fn new_operation(default_status : hyper::StatusCode, schema : ReferenceOr<Schema
|
|||
|
||||
let request_body = body_schema.map(|schema| Item(OARequestBody {
|
||||
description: None,
|
||||
content: schema_to_content(schema),
|
||||
content: schema_to_content(supported_types.unwrap_or_default(), schema),
|
||||
required: true
|
||||
}));
|
||||
|
||||
|
@ -344,7 +344,7 @@ macro_rules! implOpenapiRouter {
|
|||
|
||||
let path = format!("/{}", &self.1);
|
||||
let mut item = (self.0).1.remove_path(&path);
|
||||
item.get = Some(new_operation(Res::default_status(), schema, OperationParams::default(), None));
|
||||
item.get = Some(new_operation(Res::default_status(), Res::accepted_types(), schema, OperationParams::default(), None, None));
|
||||
(self.0).1.add_path(path, item);
|
||||
|
||||
(&mut *(self.0).0, self.1.to_string()).read_all::<Handler, Res>()
|
||||
|
@ -360,7 +360,7 @@ macro_rules! implOpenapiRouter {
|
|||
|
||||
let path = format!("/{}/{{id}}", &self.1);
|
||||
let mut item = (self.0).1.remove_path(&path);
|
||||
item.get = Some(new_operation(Res::default_status(), schema, OperationParams::from_path_params(vec!["id"]), None));
|
||||
item.get = Some(new_operation(Res::default_status(), Res::accepted_types(), schema, OperationParams::from_path_params(vec!["id"]), None, None));
|
||||
(self.0).1.add_path(path, item);
|
||||
|
||||
(&mut *(self.0).0, self.1.to_string()).read::<Handler, ID, Res>()
|
||||
|
@ -376,7 +376,7 @@ macro_rules! implOpenapiRouter {
|
|||
|
||||
let path = format!("/{}/search", &self.1);
|
||||
let mut item = (self.0).1.remove_path(&self.1);
|
||||
item.get = Some(new_operation(Res::default_status(), schema, OperationParams::from_query_params(Query::schema()), None));
|
||||
item.get = Some(new_operation(Res::default_status(), Res::accepted_types(), schema, OperationParams::from_query_params(Query::schema()), None, None));
|
||||
(self.0).1.add_path(path, item);
|
||||
|
||||
(&mut *(self.0).0, self.1.to_string()).search::<Handler, Query, Res>()
|
||||
|
@ -393,7 +393,7 @@ macro_rules! implOpenapiRouter {
|
|||
|
||||
let path = format!("/{}", &self.1);
|
||||
let mut item = (self.0).1.remove_path(&path);
|
||||
item.post = Some(new_operation(Res::default_status(), schema, OperationParams::default(), Some(body_schema)));
|
||||
item.post = Some(new_operation(Res::default_status(), Res::accepted_types(), schema, OperationParams::default(), Some(body_schema), Body::supported_types()));
|
||||
(self.0).1.add_path(path, item);
|
||||
|
||||
(&mut *(self.0).0, self.1.to_string()).create::<Handler, Body, Res>()
|
||||
|
@ -410,7 +410,7 @@ macro_rules! implOpenapiRouter {
|
|||
|
||||
let path = format!("/{}", &self.1);
|
||||
let mut item = (self.0).1.remove_path(&path);
|
||||
item.put = Some(new_operation(Res::default_status(), schema, OperationParams::default(), Some(body_schema)));
|
||||
item.put = Some(new_operation(Res::default_status(), Res::accepted_types(), schema, OperationParams::default(), Some(body_schema), Body::supported_types()));
|
||||
(self.0).1.add_path(path, item);
|
||||
|
||||
(&mut *(self.0).0, self.1.to_string()).update_all::<Handler, Body, Res>()
|
||||
|
@ -428,7 +428,7 @@ macro_rules! implOpenapiRouter {
|
|||
|
||||
let path = format!("/{}/{{id}}", &self.1);
|
||||
let mut item = (self.0).1.remove_path(&path);
|
||||
item.put = Some(new_operation(Res::default_status(), schema, OperationParams::from_path_params(vec!["id"]), Some(body_schema)));
|
||||
item.put = Some(new_operation(Res::default_status(), Res::accepted_types(), schema, OperationParams::from_path_params(vec!["id"]), Some(body_schema), Body::supported_types()));
|
||||
(self.0).1.add_path(path, item);
|
||||
|
||||
(&mut *(self.0).0, self.1.to_string()).update::<Handler, ID, Body, Res>()
|
||||
|
@ -443,7 +443,7 @@ macro_rules! implOpenapiRouter {
|
|||
|
||||
let path = format!("/{}", &self.1);
|
||||
let mut item = (self.0).1.remove_path(&path);
|
||||
item.delete = Some(new_operation(Res::default_status(), schema, OperationParams::default(), None));
|
||||
item.delete = Some(new_operation(Res::default_status(), Res::accepted_types(), schema, OperationParams::default(), None, None));
|
||||
(self.0).1.add_path(path, item);
|
||||
|
||||
(&mut *(self.0).0, self.1.to_string()).delete_all::<Handler, Res>()
|
||||
|
@ -459,7 +459,7 @@ macro_rules! implOpenapiRouter {
|
|||
|
||||
let path = format!("/{}/{{id}}", &self.1);
|
||||
let mut item = (self.0).1.remove_path(&path);
|
||||
item.delete = Some(new_operation(Res::default_status(), schema, OperationParams::from_path_params(vec!["id"]), None));
|
||||
item.delete = Some(new_operation(Res::default_status(), Res::accepted_types(), schema, OperationParams::from_path_params(vec!["id"]), None, None));
|
||||
(self.0).1.add_path(path, item);
|
||||
|
||||
(&mut *(self.0).0, self.1.to_string()).delete::<Handler, ID, Res>()
|
||||
|
|
Loading…
Add table
Reference in a new issue