A focused Docker Compose management web application.
0
fork

Configure Feed

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

refactor: make helper function for schema merging

Brooke 027c48db 295d6f5f

+31 -26
+31 -26
packages/node/src/api/response.rs
··· 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>; 12 12 13 + /// Registers the given schema as a 200 response, merging with any existing schema. 14 + fn register_or_merge(operation: &mut Operation, schema: RefOr<Schema>) { 15 + if let Some(RefOr::Type(response)) = operation.responses.get_mut("200") { 16 + if let Some(content) = response.contents.get_mut("application/json") { 17 + let existing = std::mem::take(&mut content.schema); 18 + 19 + content.schema = match existing { 20 + RefOr::Type(Schema::OneOf(one_of)) => one_of.item(schema).into(), 21 + other => OneOf::new().item(other).item(schema).into(), 22 + }; 23 + } else { 24 + response.contents.insert( 25 + "application/json".to_owned(), 26 + salvo::oapi::Content::new(OneOf::new().item(schema)), 27 + ); 28 + } 29 + 30 + return; 31 + } 32 + 33 + operation.responses.insert( 34 + "200", 35 + salvo::oapi::Response::new("Response").add_content( 36 + "application/json", 37 + salvo::oapi::Content::new(OneOf::new().item(schema)), 38 + ), 39 + ); 40 + } 41 + 13 42 /// A successful response containing type `T`. 14 43 #[derive(Debug)] 15 44 pub struct LuminarySuccessResponse<T: Serialize> { ··· 63 92 impl<T: Serialize + Send + ToSchema + 'static> EndpointOutRegister for LuminarySuccessResponse<T> { 64 93 fn register(components: &mut Components, operation: &mut Operation) { 65 94 let schema = Self::to_schema(components); 66 - 67 - if let Some(RefOr::Type(existing)) = operation.responses.get_mut("200") { 68 - if let Some(content) = existing.contents.get_mut("application/json") { 69 - let other = std::mem::replace(&mut content.schema, RefOr::Type(Schema::default())); 70 - content.schema = OneOf::new().item(other).item(schema).into(); 71 - } 72 - } else { 73 - operation.responses.insert( 74 - "200", 75 - salvo::oapi::Response::new("Response") 76 - .add_content("application/json", salvo::oapi::Content::new(schema)), 77 - ); 78 - } 95 + register_or_merge(operation, schema); 79 96 } 80 97 } 81 98 ··· 131 148 impl EndpointOutRegister for LuminaryFailResponse { 132 149 fn register(components: &mut Components, operation: &mut Operation) { 133 150 let schema = Self::to_schema(components); 134 - 135 - if let Some(RefOr::Type(existing)) = operation.responses.get_mut("200") { 136 - if let Some(content) = existing.contents.get_mut("application/json") { 137 - let other = std::mem::replace(&mut content.schema, RefOr::Type(Schema::default())); 138 - content.schema = OneOf::new().item(other).item(schema).into(); 139 - } 140 - } else { 141 - operation.responses.insert( 142 - "200", 143 - salvo::oapi::Response::new("Response") 144 - .add_content("application/json", salvo::oapi::Content::new(schema)), 145 - ); 146 - } 151 + register_or_merge(operation, schema); 147 152 } 148 153 }