chore: update routes to use utoipa
This commit is contained in:
@@ -5,8 +5,9 @@ use std::fmt::Display;
|
||||
extern crate serde;
|
||||
|
||||
#[cfg(feature = "utoipa")]
|
||||
#[macro_use]
|
||||
extern crate utoipa;
|
||||
mod utoipa_impl;
|
||||
#[cfg(feature = "utoipa")]
|
||||
pub use crate::utoipa_impl::*;
|
||||
|
||||
#[cfg(feature = "rocket")]
|
||||
pub mod rocket;
|
||||
@@ -19,7 +20,7 @@ pub type Result<T, E = Error> = std::result::Result<T, E>;
|
||||
|
||||
/// Error information
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[cfg_attr(feature = "utoipa", derive(ToSchema))]
|
||||
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Error {
|
||||
/// Type of error and additional information
|
||||
@@ -41,7 +42,7 @@ impl std::error::Error for Error {}
|
||||
/// Possible error types
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[cfg_attr(feature = "serde", serde(tag = "type"))]
|
||||
#[cfg_attr(feature = "utoipa", derive(ToSchema))]
|
||||
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum ErrorType {
|
||||
/// This error was not labeled :(
|
||||
|
||||
52
crates/core/result/src/utoipa_impl.rs
Normal file
52
crates/core/result/src/utoipa_impl.rs
Normal file
@@ -0,0 +1,52 @@
|
||||
use utoipa::{Modify, openapi::{Content, OpenApi, Ref, RefOr, ResponseBuilder}};
|
||||
|
||||
pub struct ErrorAddon;
|
||||
|
||||
impl Modify for ErrorAddon {
|
||||
fn modify(&self, utoipa: &mut OpenApi) {
|
||||
let response = ResponseBuilder::new()
|
||||
.description("An error occurred")
|
||||
.content(
|
||||
"application/json",
|
||||
Content::new(Some(RefOr::Ref(Ref::from_schema_name("Error")))),
|
||||
)
|
||||
.build();
|
||||
|
||||
for path in utoipa.paths.paths.values_mut() {
|
||||
if let Some(route) = path.get.as_mut() {
|
||||
route
|
||||
.responses
|
||||
.responses
|
||||
.insert("default".to_string(), RefOr::T(response.clone()));
|
||||
};
|
||||
|
||||
if let Some(route) = path.delete.as_mut() {
|
||||
route
|
||||
.responses
|
||||
.responses
|
||||
.insert("default".to_string(), RefOr::T(response.clone()));
|
||||
};
|
||||
|
||||
if let Some(route) = path.patch.as_mut() {
|
||||
route
|
||||
.responses
|
||||
.responses
|
||||
.insert("default".to_string(), RefOr::T(response.clone()));
|
||||
};
|
||||
|
||||
if let Some(route) = path.post.as_mut() {
|
||||
route
|
||||
.responses
|
||||
.responses
|
||||
.insert("default".to_string(), RefOr::T(response.clone()));
|
||||
};
|
||||
|
||||
if let Some(route) = path.put.as_mut() {
|
||||
route
|
||||
.responses
|
||||
.responses
|
||||
.insert("default".to_string(), RefOr::T(response.clone()));
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user