mirror of
https://gitlab.com/msrd0/gotham-restful.git
synced 2025-05-10 00:20:43 +00:00
Replace methods with more flexible endpoints
This commit is contained in:
parent
0ac0f0f504
commit
b807ae2796
87 changed files with 1497 additions and 1512 deletions
|
@ -1,5 +1,5 @@
|
|||
use super::SECURITY_NAME;
|
||||
use crate::{resource::*, result::*, OpenapiSchema, RequestBody};
|
||||
use crate::{result::*, EndpointWithSchema, OpenapiSchema, RequestBody};
|
||||
use indexmap::IndexMap;
|
||||
use mime::Mime;
|
||||
use openapiv3::{
|
||||
|
@ -8,31 +8,40 @@ use openapiv3::{
|
|||
};
|
||||
|
||||
#[derive(Default)]
|
||||
struct OperationParams<'a> {
|
||||
path_params: Vec<(&'a str, ReferenceOr<Schema>)>,
|
||||
struct OperationParams {
|
||||
path_params: Option<OpenapiSchema>,
|
||||
query_params: Option<OpenapiSchema>
|
||||
}
|
||||
|
||||
impl<'a> OperationParams<'a> {
|
||||
fn add_path_params(&self, params: &mut Vec<ReferenceOr<Parameter>>) {
|
||||
for param in &self.path_params {
|
||||
impl OperationParams {
|
||||
fn add_path_params(path_params: Option<OpenapiSchema>, params: &mut Vec<ReferenceOr<Parameter>>) {
|
||||
let path_params = match path_params {
|
||||
Some(pp) => pp.schema,
|
||||
None => return
|
||||
};
|
||||
let path_params = match path_params {
|
||||
SchemaKind::Type(Type::Object(ty)) => ty,
|
||||
_ => panic!("Path Parameters needs to be a plain struct")
|
||||
};
|
||||
for (name, schema) in path_params.properties {
|
||||
let required = path_params.required.contains(&name);
|
||||
params.push(Item(Parameter::Path {
|
||||
parameter_data: ParameterData {
|
||||
name: (*param).0.to_string(),
|
||||
name,
|
||||
description: None,
|
||||
required: true,
|
||||
required,
|
||||
deprecated: None,
|
||||
format: ParameterSchemaOrContent::Schema((*param).1.clone()),
|
||||
format: ParameterSchemaOrContent::Schema(schema.unbox()),
|
||||
example: None,
|
||||
examples: IndexMap::new()
|
||||
},
|
||||
style: Default::default()
|
||||
}));
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
fn add_query_params(self, params: &mut Vec<ReferenceOr<Parameter>>) {
|
||||
let query_params = match self.query_params {
|
||||
fn add_query_params(query_params: Option<OpenapiSchema>, params: &mut Vec<ReferenceOr<Parameter>>) {
|
||||
let query_params = match query_params {
|
||||
Some(qp) => qp.schema,
|
||||
None => return
|
||||
};
|
||||
|
@ -61,51 +70,48 @@ impl<'a> OperationParams<'a> {
|
|||
|
||||
fn into_params(self) -> Vec<ReferenceOr<Parameter>> {
|
||||
let mut params: Vec<ReferenceOr<Parameter>> = Vec::new();
|
||||
self.add_path_params(&mut params);
|
||||
self.add_query_params(&mut params);
|
||||
Self::add_path_params(self.path_params, &mut params);
|
||||
Self::add_query_params(self.query_params, &mut params);
|
||||
params
|
||||
}
|
||||
}
|
||||
|
||||
pub struct OperationDescription<'a> {
|
||||
pub struct OperationDescription {
|
||||
operation_id: Option<String>,
|
||||
default_status: crate::StatusCode,
|
||||
accepted_types: Option<Vec<Mime>>,
|
||||
schema: ReferenceOr<Schema>,
|
||||
params: OperationParams<'a>,
|
||||
params: OperationParams,
|
||||
body_schema: Option<ReferenceOr<Schema>>,
|
||||
supported_types: Option<Vec<Mime>>,
|
||||
requires_auth: bool
|
||||
}
|
||||
|
||||
impl<'a> OperationDescription<'a> {
|
||||
pub fn new<Handler: ResourceMethod>(schema: ReferenceOr<Schema>) -> Self {
|
||||
impl OperationDescription {
|
||||
pub fn new<E: EndpointWithSchema>(schema: ReferenceOr<Schema>) -> Self {
|
||||
Self {
|
||||
operation_id: Handler::operation_id(),
|
||||
default_status: Handler::Res::default_status(),
|
||||
accepted_types: Handler::Res::accepted_types(),
|
||||
operation_id: E::operation_id(),
|
||||
default_status: E::Output::default_status(),
|
||||
accepted_types: E::Output::accepted_types(),
|
||||
schema,
|
||||
params: Default::default(),
|
||||
body_schema: None,
|
||||
supported_types: None,
|
||||
requires_auth: Handler::wants_auth()
|
||||
requires_auth: E::wants_auth()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_path_param(mut self, name: &'a str, schema: ReferenceOr<Schema>) -> Self {
|
||||
self.params.path_params.push((name, schema));
|
||||
self
|
||||
pub fn set_path_params(&mut self, params: OpenapiSchema) {
|
||||
self.params.path_params = Some(params);
|
||||
}
|
||||
|
||||
pub fn with_query_params(mut self, params: OpenapiSchema) -> Self {
|
||||
pub fn set_query_params(&mut self, params: OpenapiSchema) {
|
||||
self.params.query_params = Some(params);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_body<Body: RequestBody>(mut self, schema: ReferenceOr<Schema>) -> Self {
|
||||
pub fn set_body<Body: RequestBody>(&mut self, schema: ReferenceOr<Schema>) {
|
||||
self.body_schema = Some(schema);
|
||||
self.supported_types = Body::supported_types();
|
||||
self
|
||||
}
|
||||
|
||||
fn schema_to_content(types: Vec<Mime>, schema: ReferenceOr<Schema>) -> IndexMap<String, MediaType> {
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
use super::{builder::OpenapiBuilder, handler::OpenapiHandler, operation::OperationDescription};
|
||||
use crate::{resource::*, routing::*, OpenapiType};
|
||||
use gotham::{pipeline::chain::PipelineHandleChain, router::builder::*};
|
||||
use crate::{routing::*, EndpointWithSchema, OpenapiType, ResourceWithSchema};
|
||||
use gotham::{hyper::Method, pipeline::chain::PipelineHandleChain, router::builder::*};
|
||||
use once_cell::sync::Lazy;
|
||||
use regex::{Captures, Regex};
|
||||
use std::panic::RefUnwindSafe;
|
||||
|
||||
/// This trait adds the `get_openapi` method to an OpenAPI-aware router.
|
||||
|
@ -51,150 +53,62 @@ macro_rules! implOpenapiRouter {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a, 'b, C, P> DrawResources for OpenapiRouter<'a, $implType<'b, C, P>>
|
||||
impl<'a, 'b, C, P> DrawResourcesWithSchema for OpenapiRouter<'a, $implType<'b, C, P>>
|
||||
where
|
||||
C: PipelineHandleChain<P> + Copy + Send + Sync + 'static,
|
||||
P: RefUnwindSafe + Send + Sync + 'static
|
||||
{
|
||||
fn resource<R: Resource>(&mut self, path: &str) {
|
||||
fn resource<R: ResourceWithSchema>(&mut self, path: &str) {
|
||||
R::setup((self, path));
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'b, C, P> DrawResourceRoutes for (&mut OpenapiRouter<'a, $implType<'b, C, P>>, &str)
|
||||
impl<'a, 'b, C, P> DrawResourceRoutesWithSchema for (&mut OpenapiRouter<'a, $implType<'b, C, P>>, &str)
|
||||
where
|
||||
C: PipelineHandleChain<P> + Copy + Send + Sync + 'static,
|
||||
P: RefUnwindSafe + Send + Sync + 'static
|
||||
{
|
||||
fn read_all<Handler: ResourceReadAll>(&mut self) {
|
||||
let schema = (self.0).openapi_builder.add_schema::<Handler::Res>();
|
||||
fn endpoint<E: EndpointWithSchema + 'static>(&mut self) {
|
||||
let schema = (self.0).openapi_builder.add_schema::<E::Output>();
|
||||
let mut descr = OperationDescription::new::<E>(schema);
|
||||
if E::has_placeholders() {
|
||||
descr.set_path_params(E::Placeholders::schema());
|
||||
}
|
||||
if E::needs_params() {
|
||||
descr.set_query_params(E::Params::schema());
|
||||
}
|
||||
if E::needs_body() {
|
||||
let body_schema = (self.0).openapi_builder.add_schema::<E::Body>();
|
||||
descr.set_body::<E::Body>(body_schema);
|
||||
}
|
||||
|
||||
let path = format!("{}/{}", self.0.scope.unwrap_or_default(), self.1);
|
||||
static URI_PLACEHOLDER_REGEX: Lazy<Regex> =
|
||||
Lazy::new(|| Regex::new(r#"(^|/):(?P<name>[^/]+)(/|$)"#).unwrap());
|
||||
let uri: &str = &E::uri();
|
||||
let uri =
|
||||
URI_PLACEHOLDER_REGEX.replace_all(uri, |captures: &Captures<'_>| format!("{{{}}}", &captures["name"]));
|
||||
let path = if uri.is_empty() {
|
||||
format!("{}/{}", self.0.scope.unwrap_or_default(), self.1)
|
||||
} else {
|
||||
format!("{}/{}/{}", self.0.scope.unwrap_or_default(), self.1, uri)
|
||||
};
|
||||
|
||||
let op = descr.into_operation();
|
||||
let mut item = (self.0).openapi_builder.remove_path(&path);
|
||||
item.get = Some(OperationDescription::new::<Handler>(schema).into_operation());
|
||||
match E::http_method() {
|
||||
Method::GET => item.get = Some(op),
|
||||
Method::PUT => item.put = Some(op),
|
||||
Method::POST => item.post = Some(op),
|
||||
Method::DELETE => item.delete = Some(op),
|
||||
Method::OPTIONS => item.options = Some(op),
|
||||
Method::HEAD => item.head = Some(op),
|
||||
Method::PATCH => item.patch = Some(op),
|
||||
Method::TRACE => item.trace = Some(op),
|
||||
method => warn!("Ignoring unsupported method '{}' in OpenAPI Specification", method)
|
||||
};
|
||||
(self.0).openapi_builder.add_path(path, item);
|
||||
|
||||
(&mut *(self.0).router, self.1).read_all::<Handler>()
|
||||
}
|
||||
|
||||
fn read<Handler: ResourceRead>(&mut self) {
|
||||
let schema = (self.0).openapi_builder.add_schema::<Handler::Res>();
|
||||
let id_schema = (self.0).openapi_builder.add_schema::<Handler::ID>();
|
||||
|
||||
let path = format!("{}/{}/{{id}}", self.0.scope.unwrap_or_default(), self.1);
|
||||
let mut item = (self.0).openapi_builder.remove_path(&path);
|
||||
item.get = Some(
|
||||
OperationDescription::new::<Handler>(schema)
|
||||
.add_path_param("id", id_schema)
|
||||
.into_operation()
|
||||
);
|
||||
(self.0).openapi_builder.add_path(path, item);
|
||||
|
||||
(&mut *(self.0).router, self.1).read::<Handler>()
|
||||
}
|
||||
|
||||
fn search<Handler: ResourceSearch>(&mut self) {
|
||||
let schema = (self.0).openapi_builder.add_schema::<Handler::Res>();
|
||||
|
||||
let path = format!("{}/{}/search", self.0.scope.unwrap_or_default(), self.1);
|
||||
let mut item = (self.0).openapi_builder.remove_path(&path);
|
||||
item.get = Some(
|
||||
OperationDescription::new::<Handler>(schema)
|
||||
.with_query_params(Handler::Query::schema())
|
||||
.into_operation()
|
||||
);
|
||||
(self.0).openapi_builder.add_path(path, item);
|
||||
|
||||
(&mut *(self.0).router, self.1).search::<Handler>()
|
||||
}
|
||||
|
||||
fn create<Handler: ResourceCreate>(&mut self)
|
||||
where
|
||||
Handler::Res: 'static,
|
||||
Handler::Body: 'static
|
||||
{
|
||||
let schema = (self.0).openapi_builder.add_schema::<Handler::Res>();
|
||||
let body_schema = (self.0).openapi_builder.add_schema::<Handler::Body>();
|
||||
|
||||
let path = format!("{}/{}", self.0.scope.unwrap_or_default(), self.1);
|
||||
let mut item = (self.0).openapi_builder.remove_path(&path);
|
||||
item.post = Some(
|
||||
OperationDescription::new::<Handler>(schema)
|
||||
.with_body::<Handler::Body>(body_schema)
|
||||
.into_operation()
|
||||
);
|
||||
(self.0).openapi_builder.add_path(path, item);
|
||||
|
||||
(&mut *(self.0).router, self.1).create::<Handler>()
|
||||
}
|
||||
|
||||
fn change_all<Handler: ResourceChangeAll>(&mut self)
|
||||
where
|
||||
Handler::Res: 'static,
|
||||
Handler::Body: 'static
|
||||
{
|
||||
let schema = (self.0).openapi_builder.add_schema::<Handler::Res>();
|
||||
let body_schema = (self.0).openapi_builder.add_schema::<Handler::Body>();
|
||||
|
||||
let path = format!("{}/{}", self.0.scope.unwrap_or_default(), self.1);
|
||||
let mut item = (self.0).openapi_builder.remove_path(&path);
|
||||
item.put = Some(
|
||||
OperationDescription::new::<Handler>(schema)
|
||||
.with_body::<Handler::Body>(body_schema)
|
||||
.into_operation()
|
||||
);
|
||||
(self.0).openapi_builder.add_path(path, item);
|
||||
|
||||
(&mut *(self.0).router, self.1).change_all::<Handler>()
|
||||
}
|
||||
|
||||
fn change<Handler: ResourceChange>(&mut self)
|
||||
where
|
||||
Handler::Res: 'static,
|
||||
Handler::Body: 'static
|
||||
{
|
||||
let schema = (self.0).openapi_builder.add_schema::<Handler::Res>();
|
||||
let id_schema = (self.0).openapi_builder.add_schema::<Handler::ID>();
|
||||
let body_schema = (self.0).openapi_builder.add_schema::<Handler::Body>();
|
||||
|
||||
let path = format!("{}/{}/{{id}}", self.0.scope.unwrap_or_default(), self.1);
|
||||
let mut item = (self.0).openapi_builder.remove_path(&path);
|
||||
item.put = Some(
|
||||
OperationDescription::new::<Handler>(schema)
|
||||
.add_path_param("id", id_schema)
|
||||
.with_body::<Handler::Body>(body_schema)
|
||||
.into_operation()
|
||||
);
|
||||
(self.0).openapi_builder.add_path(path, item);
|
||||
|
||||
(&mut *(self.0).router, self.1).change::<Handler>()
|
||||
}
|
||||
|
||||
fn remove_all<Handler: ResourceRemoveAll>(&mut self) {
|
||||
let schema = (self.0).openapi_builder.add_schema::<Handler::Res>();
|
||||
|
||||
let path = format!("{}/{}", self.0.scope.unwrap_or_default(), self.1);
|
||||
let mut item = (self.0).openapi_builder.remove_path(&path);
|
||||
item.delete = Some(OperationDescription::new::<Handler>(schema).into_operation());
|
||||
(self.0).openapi_builder.add_path(path, item);
|
||||
|
||||
(&mut *(self.0).router, self.1).remove_all::<Handler>()
|
||||
}
|
||||
|
||||
fn remove<Handler: ResourceRemove>(&mut self) {
|
||||
let schema = (self.0).openapi_builder.add_schema::<Handler::Res>();
|
||||
let id_schema = (self.0).openapi_builder.add_schema::<Handler::ID>();
|
||||
|
||||
let path = format!("{}/{}/{{id}}", self.0.scope.unwrap_or_default(), self.1);
|
||||
let mut item = (self.0).openapi_builder.remove_path(&path);
|
||||
item.delete = Some(
|
||||
OperationDescription::new::<Handler>(schema)
|
||||
.add_path_param("id", id_schema)
|
||||
.into_operation()
|
||||
);
|
||||
(self.0).openapi_builder.add_path(path, item);
|
||||
|
||||
(&mut *(self.0).router, self.1).remove::<Handler>()
|
||||
(&mut *(self.0).router, self.1).endpoint::<E>()
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#[cfg(feature = "chrono")]
|
||||
use chrono::{Date, DateTime, FixedOffset, Local, NaiveDate, NaiveDateTime, Utc};
|
||||
use gotham::extractor::{NoopPathExtractor, NoopQueryStringExtractor};
|
||||
use indexmap::IndexMap;
|
||||
use openapiv3::{
|
||||
AdditionalProperties, ArrayType, IntegerType, NumberFormat, NumberType, ObjectType,
|
||||
|
@ -86,6 +87,20 @@ impl OpenapiType for () {
|
|||
}
|
||||
}
|
||||
|
||||
impl OpenapiType for NoopPathExtractor {
|
||||
fn schema() -> OpenapiSchema {
|
||||
warn!("You're asking for the OpenAPI Schema for gotham::extractor::NoopPathExtractor. This is probably not what you want.");
|
||||
<()>::schema()
|
||||
}
|
||||
}
|
||||
|
||||
impl OpenapiType for NoopQueryStringExtractor {
|
||||
fn schema() -> OpenapiSchema {
|
||||
warn!("You're asking for the OpenAPI Schema for gotham::extractor::NoopQueryStringExtractor. This is probably not what you want.");
|
||||
<()>::schema()
|
||||
}
|
||||
}
|
||||
|
||||
impl OpenapiType for bool {
|
||||
fn schema() -> OpenapiSchema {
|
||||
OpenapiSchema::new(SchemaKind::Type(Type::Boolean {}))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue