salvo_oapi/openapi/
header.rs1use serde::{Deserialize, Serialize};
6
7use super::{BasicType, Object, RefOr, Schema};
8
9#[non_exhaustive]
13#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
14pub struct Header {
15 pub schema: RefOr<Schema>,
17
18 #[serde(skip_serializing_if = "Option::is_none")]
20 pub description: Option<String>,
21}
22
23impl Header {
24 #[must_use]
41 pub fn new<C: Into<RefOr<Schema>>>(component: C) -> Self {
42 Self {
43 schema: component.into(),
44 ..Default::default()
45 }
46 }
47 #[must_use]
49 pub fn schema<I: Into<RefOr<Schema>>>(mut self, component: I) -> Self {
50 self.schema = component.into();
51 self
52 }
53
54 #[must_use]
56 pub fn description<S: Into<String>>(mut self, description: S) -> Self {
57 self.description = Some(description.into());
58 self
59 }
60}
61
62impl Default for Header {
63 fn default() -> Self {
64 Self {
65 description: Default::default(),
66 schema: Object::with_type(BasicType::String).into(),
67 }
68 }
69}
70
71#[cfg(test)]
72mod tests {
73 use assert_json_diff::assert_json_eq;
74 use serde_json::json;
75
76 use super::*;
77
78 #[test]
79 fn test_build_header() {
80 let header = Header::new(Object::with_type(BasicType::String));
81 assert_json_eq!(
82 header,
83 json!({
84 "schema": {
85 "type": "string"
86 }
87 })
88 );
89
90 let header = header
91 .description("test description")
92 .schema(Object::with_type(BasicType::Number));
93 assert_json_eq!(
94 header,
95 json!({
96 "description": "test description",
97 "schema": {
98 "type": "number"
99 }
100 })
101 );
102 }
103}