A focused Docker Compose management web application.
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix: openapi schema for LuminaryProject

Brooke 295d6f5f 8158738d

+52 -28
+12 -26
packages/node/src/api/response.rs
··· 1 1 use salvo::Response; 2 2 use salvo::http::StatusCode; 3 - use salvo::oapi::Ref; 4 - use salvo::oapi::naming::assign_name; 5 3 use salvo::oapi::schema::{Array, BasicType, Object, OneOf, Schema}; 6 4 use salvo::oapi::{Components, EndpointOutRegister, Operation, RefOr, ToSchema}; 7 5 use salvo::writing::Scribe; 8 6 use serde::Serialize; 7 + 8 + use crate::schema_ref_or; 9 9 10 10 /// A unified response type for Luminary API endpoints, consisting of [LuminarySuccessResponse] and [LuminaryFailResponse]. 11 11 pub type LuminaryResponse<T> = Result<LuminarySuccessResponse<T>, LuminaryFailResponse>; ··· 18 18 19 19 impl<T: Serialize + ToSchema + 'static> ToSchema for LuminarySuccessResponse<T> { 20 20 fn to_schema(components: &mut Components) -> RefOr<Schema> { 21 - let name = assign_name::<Self>(Default::default()); 22 - let ref_or = RefOr::Ref(Ref::new(format!("#/components/schemas/{}", name))); 23 - 24 - if !components.schemas.contains_key(&name) { 25 - components.schemas.insert(name.clone(), ref_or.clone()); 26 - 27 - let schema = Schema::Object(Box::new( 21 + return schema_ref_or!( 22 + components, 23 + Schema::Object(Box::new( 28 24 Object::new() 29 25 .property( 30 26 "success", ··· 33 29 .required("success") 34 30 .property("data", T::to_schema(components)) 35 31 .required("data"), 36 - )); 37 - components.schemas.insert(name, schema); 38 - } 39 - 40 - ref_or 32 + )) 33 + ); 41 34 } 42 35 } 43 36 ··· 94 87 95 88 impl ToSchema for LuminaryFailResponse { 96 89 fn to_schema(components: &mut Components) -> RefOr<Schema> { 97 - let name = assign_name::<Self>(Default::default()); 98 - let ref_or = RefOr::Ref(Ref::new(format!("#/components/schemas/{}", name))); 99 - 100 - if !components.schemas.contains_key(&name) { 101 - components.schemas.insert(name.clone(), ref_or.clone()); 102 - 103 - let schema = Schema::Object(Box::new( 90 + return schema_ref_or!( 91 + components, 92 + Schema::Object(Box::new( 104 93 Object::new() 105 94 .property( 106 95 "success", ··· 109 98 .required("success") 110 99 .property("error", Array::new().items(Object::with_type(BasicType::String))) 111 100 .required("error"), 112 - )); 113 - components.schemas.insert(name, schema); 114 - } 115 - 116 - ref_or 101 + )) 102 + ); 117 103 } 118 104 } 119 105
+23 -2
packages/node/src/core/model.rs
··· 4 4 5 5 use bytes::{Bytes, BytesMut}; 6 6 use luminary_macros::hashmap_schema; 7 - use salvo::oapi::ToSchema; 7 + use salvo::oapi::{BasicType, Components, Object, RefOr, Schema, ToSchema}; 8 8 use serde::{Serialize, ser::SerializeStruct}; 9 9 use tokio::sync::{RwLock, broadcast}; 10 + 11 + use crate::schema_ref_or; 10 12 11 13 /// A collection of Luminary projects, keyed by project name. 12 14 #[hashmap_schema] ··· 21 23 /// Represents a Luminary project, consisting of a docker compose project. 22 24 /// 23 25 /// This is derived entirely from that state of its services. 24 - #[derive(Debug, Clone, PartialEq, ToSchema)] 26 + #[derive(Debug, Clone, PartialEq)] 25 27 pub struct LuminaryProject { 26 28 /// The name of this project 27 29 pub name: String, ··· 55 57 state.serialize_field("busy", &self.busy())?; 56 58 state.serialize_field("services", &self.services)?; 57 59 state.end() 60 + } 61 + } 62 + 63 + impl ToSchema for LuminaryProject { 64 + fn to_schema(components: &mut Components) -> RefOr<Schema> { 65 + return schema_ref_or!( 66 + components, 67 + Schema::Object(Box::new( 68 + Object::new() 69 + .property("name", Object::with_type(BasicType::String)) 70 + .required("name") 71 + .property("status", LuminaryStatus::to_schema(components)) 72 + .required("status") 73 + .property("busy", Object::with_type(BasicType::Boolean)) 74 + .required("busy") 75 + .property("services", LuminaryServiceList::to_schema(components)) 76 + .required("services"), 77 + )) 78 + ); 58 79 } 59 80 } 60 81
+17
packages/node/src/util.rs
··· 20 20 .expect("User can not be obtained from a unprotected endpoint.") 21 21 }; 22 22 } 23 + 24 + /// Helper macro to create or reference an OpenAPI schema. 25 + #[macro_export] 26 + macro_rules! schema_ref_or { 27 + ($components:expr, $block:expr) => {{ 28 + let name = salvo::oapi::naming::assign_name::<Self>(Default::default()); 29 + let ref_or = salvo::oapi::RefOr::Ref(salvo::oapi::Ref::new(format!("#/components/schemas/{}", name))); 30 + 31 + if !$components.schemas.contains_key(&name) { 32 + $components.schemas.insert(name.clone(), ref_or.clone()); 33 + let schema = $block; 34 + $components.schemas.insert(name, schema) 35 + } 36 + 37 + ref_or 38 + }}; 39 + }