1
0
Fork 0
mirror of https://gitlab.com/msrd0/gotham-restful.git synced 2025-02-22 20:52:27 +00:00

fix ui when openapi is enabled

This commit is contained in:
Dominic 2021-02-21 18:31:44 +01:00
parent 30edd349ed
commit c640efcb88
Signed by: msrd0
GPG key ID: DCC8C247452E98F9
9 changed files with 64 additions and 32 deletions

View file

@ -1,4 +1,4 @@
use crate::{OpenapiSchema, OpenapiType}; use crate::OpenapiSchema;
use indexmap::IndexMap; use indexmap::IndexMap;
use openapiv3::{ use openapiv3::{
Components, OpenAPI, PathItem, ReferenceOr, Components, OpenAPI, PathItem, ReferenceOr,
@ -83,8 +83,7 @@ impl OpenapiBuilder {
} }
} }
pub fn add_schema<T: OpenapiType>(&mut self) -> ReferenceOr<Schema> { pub fn add_schema(&mut self, mut schema: OpenapiSchema) -> ReferenceOr<Schema> {
let mut schema = T::schema();
match schema.name.clone() { match schema.name.clone() {
Some(name) => { Some(name) => {
let reference = Reference { let reference = Reference {
@ -105,6 +104,7 @@ impl OpenapiBuilder {
#[allow(dead_code)] #[allow(dead_code)]
mod test { mod test {
use super::*; use super::*;
use crate::OpenapiType;
#[derive(OpenapiType)] #[derive(OpenapiType)]
struct Message { struct Message {
@ -142,7 +142,7 @@ mod test {
#[test] #[test]
fn add_schema() { fn add_schema() {
let mut builder = OpenapiBuilder::new(info()); let mut builder = OpenapiBuilder::new(info());
builder.add_schema::<Option<Messages>>(); builder.add_schema(<Option<Messages>>::schema());
let openapi = openapi(builder); let openapi = openapi(builder);
assert_eq!( assert_eq!(

View file

@ -184,12 +184,11 @@ impl OperationDescription {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;
use crate::{OpenapiType, ResourceResult};
#[test] #[test]
fn no_content_schema_to_content() { fn no_content_schema_to_content() {
let types = NoContent::accepted_types(); let types = NoContent::accepted_types();
let schema = <NoContent as OpenapiType>::schema(); let schema = <NoContent as ResourceResult>::schema();
let content = OperationDescription::schema_to_content(types.or_all_types(), Item(schema.into_schema())); let content = OperationDescription::schema_to_content(types.or_all_types(), Item(schema.into_schema()));
assert!(content.is_empty()); assert!(content.is_empty());
} }
@ -197,7 +196,7 @@ mod test {
#[test] #[test]
fn raw_schema_to_content() { fn raw_schema_to_content() {
let types = Raw::<&str>::accepted_types(); let types = Raw::<&str>::accepted_types();
let schema = <Raw<&str> as OpenapiType>::schema(); let schema = <Raw<&str> as ResourceResult>::schema();
let content = OperationDescription::schema_to_content(types.or_all_types(), Item(schema.into_schema())); let content = OperationDescription::schema_to_content(types.or_all_types(), Item(schema.into_schema()));
assert_eq!(content.len(), 1); assert_eq!(content.len(), 1);
let json = serde_json::to_string(&content.values().nth(0).unwrap()).unwrap(); let json = serde_json::to_string(&content.values().nth(0).unwrap()).unwrap();

View file

@ -1,5 +1,5 @@
use super::{builder::OpenapiBuilder, handler::OpenapiHandler, operation::OperationDescription}; use super::{builder::OpenapiBuilder, handler::OpenapiHandler, operation::OperationDescription};
use crate::{routing::*, EndpointWithSchema, OpenapiType, ResourceWithSchema}; use crate::{routing::*, EndpointWithSchema, OpenapiType, ResourceResult, ResourceWithSchema};
use gotham::{hyper::Method, pipeline::chain::PipelineHandleChain, router::builder::*}; use gotham::{hyper::Method, pipeline::chain::PipelineHandleChain, router::builder::*};
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use regex::{Captures, Regex}; use regex::{Captures, Regex};
@ -69,7 +69,7 @@ macro_rules! implOpenapiRouter {
P: RefUnwindSafe + Send + Sync + 'static P: RefUnwindSafe + Send + Sync + 'static
{ {
fn endpoint<E: EndpointWithSchema + 'static>(&mut self) { fn endpoint<E: EndpointWithSchema + 'static>(&mut self) {
let schema = (self.0).openapi_builder.add_schema::<E::Output>(); let schema = (self.0).openapi_builder.add_schema(E::Output::schema());
let mut descr = OperationDescription::new::<E>(schema); let mut descr = OperationDescription::new::<E>(schema);
if E::has_placeholders() { if E::has_placeholders() {
descr.set_path_params(E::Placeholders::schema()); descr.set_path_params(E::Placeholders::schema());
@ -78,7 +78,7 @@ macro_rules! implOpenapiRouter {
descr.set_query_params(E::Params::schema()); descr.set_query_params(E::Params::schema());
} }
if E::needs_body() { if E::needs_body() {
let body_schema = (self.0).openapi_builder.add_schema::<E::Body>(); let body_schema = (self.0).openapi_builder.add_schema(E::Body::schema());
descr.set_body::<E::Body>(body_schema); descr.set_body::<E::Body>(body_schema);
} }

View file

@ -65,13 +65,6 @@ pub trait ResourceResult {
} }
} }
#[cfg(feature = "openapi")]
impl<Res: ResourceResult> crate::OpenapiType for Res {
fn schema() -> OpenapiSchema {
Self::schema()
}
}
/// The default json returned on an 500 Internal Server Error. /// The default json returned on an 500 Internal Server Error.
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
pub(crate) struct ResourceError { pub(crate) struct ResourceError {

View file

@ -1,7 +1,7 @@
use super::{handle_error, IntoResponseError, ResourceResult}; use super::{handle_error, IntoResponseError, ResourceResult};
#[cfg(feature = "openapi")]
use crate::OpenapiSchema;
use crate::{FromBody, RequestBody, ResourceType, Response}; use crate::{FromBody, RequestBody, ResourceType, Response};
#[cfg(feature = "openapi")]
use crate::{OpenapiSchema, OpenapiType};
use futures_core::future::Future; use futures_core::future::Future;
use futures_util::{future, future::FutureExt}; use futures_util::{future, future::FutureExt};
@ -89,6 +89,16 @@ impl<T: for<'a> From<&'a [u8]>> FromBody for Raw<T> {
impl<T> RequestBody for Raw<T> where Raw<T>: FromBody + ResourceType {} impl<T> RequestBody for Raw<T> where Raw<T>: FromBody + ResourceType {}
#[cfg(feature = "openapi")]
impl<T> OpenapiType for Raw<T> {
fn schema() -> OpenapiSchema {
OpenapiSchema::new(SchemaKind::Type(Type::String(StringType {
format: VariantOrUnknownOrEmpty::Item(StringFormat::Binary),
..Default::default()
})))
}
}
impl<T: Into<Body>> ResourceResult for Raw<T> impl<T: Into<Body>> ResourceResult for Raw<T>
where where
Self: Send Self: Send
@ -101,10 +111,7 @@ where
#[cfg(feature = "openapi")] #[cfg(feature = "openapi")]
fn schema() -> OpenapiSchema { fn schema() -> OpenapiSchema {
OpenapiSchema::new(SchemaKind::Type(Type::String(StringType { <Self as OpenapiType>::schema()
format: VariantOrUnknownOrEmpty::Item(StringFormat::Binary),
..Default::default()
})))
} }
} }

View file

@ -1,3 +1,14 @@
error[E0277]: the trait bound `FooBody: OpenapiType` is not satisfied
--> $DIR/invalid_body_ty.rs:15:16
|
15 | fn endpoint(_: FooBody) {
| ^^^^^^^ the trait `OpenapiType` is not implemented for `FooBody`
|
::: $WORKSPACE/src/endpoint.rs
|
| type Body: RequestBody + Send;
| ----------- required by this bound in `gotham_restful::EndpointWithSchema::Body`
error[E0277]: the trait bound `for<'de> FooBody: serde::de::Deserialize<'de>` is not satisfied error[E0277]: the trait bound `for<'de> FooBody: serde::de::Deserialize<'de>` is not satisfied
--> $DIR/invalid_body_ty.rs:15:16 --> $DIR/invalid_body_ty.rs:15:16
| |
@ -7,7 +18,7 @@ error[E0277]: the trait bound `for<'de> FooBody: serde::de::Deserialize<'de>` is
::: $WORKSPACE/src/endpoint.rs ::: $WORKSPACE/src/endpoint.rs
| |
| type Body: RequestBody + Send; | type Body: RequestBody + Send;
| ----------- required by this bound in `gotham_restful::Endpoint::Body` | ----------- required by this bound in `gotham_restful::EndpointWithSchema::Body`
| |
= note: required because of the requirements on the impl of `serde::de::DeserializeOwned` for `FooBody` = note: required because of the requirements on the impl of `serde::de::DeserializeOwned` for `FooBody`
= note: required because of the requirements on the impl of `RequestBody` for `FooBody` = note: required because of the requirements on the impl of `gotham_restful::RequestBody` for `FooBody`

View file

@ -7,7 +7,7 @@ error[E0277]: the trait bound `for<'de> FooParams: serde::de::Deserialize<'de>`
::: $WORKSPACE/src/endpoint.rs ::: $WORKSPACE/src/endpoint.rs
| |
| type Params: QueryStringExtractor<Body> + Sync; | type Params: QueryStringExtractor<Body> + Sync;
| -------------------------- required by this bound in `gotham_restful::Endpoint::Params` | -------------------------- required by this bound in `gotham_restful::EndpointWithSchema::Params`
error[E0277]: the trait bound `FooParams: StateData` is not satisfied error[E0277]: the trait bound `FooParams: StateData` is not satisfied
--> $DIR/invalid_params_ty.rs:15:16 --> $DIR/invalid_params_ty.rs:15:16
@ -18,7 +18,7 @@ error[E0277]: the trait bound `FooParams: StateData` is not satisfied
::: $WORKSPACE/src/endpoint.rs ::: $WORKSPACE/src/endpoint.rs
| |
| type Params: QueryStringExtractor<Body> + Sync; | type Params: QueryStringExtractor<Body> + Sync;
| -------------------------- required by this bound in `gotham_restful::Endpoint::Params` | -------------------------- required by this bound in `gotham_restful::EndpointWithSchema::Params`
error[E0277]: the trait bound `FooParams: StaticResponseExtender` is not satisfied error[E0277]: the trait bound `FooParams: StaticResponseExtender` is not satisfied
--> $DIR/invalid_params_ty.rs:15:16 --> $DIR/invalid_params_ty.rs:15:16
@ -29,4 +29,15 @@ error[E0277]: the trait bound `FooParams: StaticResponseExtender` is not satisfi
::: $WORKSPACE/src/endpoint.rs ::: $WORKSPACE/src/endpoint.rs
| |
| type Params: QueryStringExtractor<Body> + Sync; | type Params: QueryStringExtractor<Body> + Sync;
| -------------------------- required by this bound in `gotham_restful::Endpoint::Params` | -------------------------- required by this bound in `gotham_restful::EndpointWithSchema::Params`
error[E0277]: the trait bound `FooParams: OpenapiType` is not satisfied
--> $DIR/invalid_params_ty.rs:15:16
|
15 | fn endpoint(_: FooParams) {
| ^^^^^^^^^ the trait `OpenapiType` is not implemented for `FooParams`
|
::: $WORKSPACE/src/endpoint.rs
|
| #[openapi_bound("Params: crate::OpenapiType")]
| ---------------------------- required by this bound in `gotham_restful::EndpointWithSchema::Params`

View file

@ -7,7 +7,7 @@ error[E0277]: the trait bound `for<'de> FooPlaceholders: serde::de::Deserialize<
::: $WORKSPACE/src/endpoint.rs ::: $WORKSPACE/src/endpoint.rs
| |
| type Placeholders: PathExtractor<Body> + Sync; | type Placeholders: PathExtractor<Body> + Sync;
| ------------------- required by this bound in `gotham_restful::Endpoint::Placeholders` | ------------------- required by this bound in `gotham_restful::EndpointWithSchema::Placeholders`
error[E0277]: the trait bound `FooPlaceholders: StateData` is not satisfied error[E0277]: the trait bound `FooPlaceholders: StateData` is not satisfied
--> $DIR/invalid_placeholders_ty.rs:15:16 --> $DIR/invalid_placeholders_ty.rs:15:16
@ -18,7 +18,7 @@ error[E0277]: the trait bound `FooPlaceholders: StateData` is not satisfied
::: $WORKSPACE/src/endpoint.rs ::: $WORKSPACE/src/endpoint.rs
| |
| type Placeholders: PathExtractor<Body> + Sync; | type Placeholders: PathExtractor<Body> + Sync;
| ------------------- required by this bound in `gotham_restful::Endpoint::Placeholders` | ------------------- required by this bound in `gotham_restful::EndpointWithSchema::Placeholders`
error[E0277]: the trait bound `FooPlaceholders: StaticResponseExtender` is not satisfied error[E0277]: the trait bound `FooPlaceholders: StaticResponseExtender` is not satisfied
--> $DIR/invalid_placeholders_ty.rs:15:16 --> $DIR/invalid_placeholders_ty.rs:15:16
@ -29,4 +29,15 @@ error[E0277]: the trait bound `FooPlaceholders: StaticResponseExtender` is not s
::: $WORKSPACE/src/endpoint.rs ::: $WORKSPACE/src/endpoint.rs
| |
| type Placeholders: PathExtractor<Body> + Sync; | type Placeholders: PathExtractor<Body> + Sync;
| ------------------- required by this bound in `gotham_restful::Endpoint::Placeholders` | ------------------- required by this bound in `gotham_restful::EndpointWithSchema::Placeholders`
error[E0277]: the trait bound `FooPlaceholders: OpenapiType` is not satisfied
--> $DIR/invalid_placeholders_ty.rs:15:16
|
15 | fn endpoint(_: FooPlaceholders) {
| ^^^^^^^^^^^^^^^ the trait `OpenapiType` is not implemented for `FooPlaceholders`
|
::: $WORKSPACE/src/endpoint.rs
|
| #[openapi_bound("Placeholders: crate::OpenapiType")]
| ---------------------------------- required by this bound in `gotham_restful::EndpointWithSchema::Placeholders`

View file

@ -7,4 +7,4 @@ error[E0277]: the trait bound `FooResponse: ResourceResult` is not satisfied
::: $WORKSPACE/src/endpoint.rs ::: $WORKSPACE/src/endpoint.rs
| |
| type Output: ResourceResult + Send; | type Output: ResourceResult + Send;
| -------------- required by this bound in `gotham_restful::Endpoint::Output` | -------------- required by this bound in `gotham_restful::EndpointWithSchema::Output`