1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
//! Implements [OpenAPI Header Object][header] types.
//!
//! [header]: https://spec.openapis.org/oas/latest.html#header-object
use serde::{Deserialize, Serialize};
use serde_json::Value;
use super::parameter::ParameterStyle;
use super::{BasicType, Content, Deprecated, Example, Object, PropMap, RefOr, Schema};
/// Implements [OpenAPI Header Object][header] for response headers and for individual parts in
/// `multipart` representations.
///
/// A Header Object follows the structure of the [`Parameter`](crate::Parameter) object minus
/// `name` and `in`, and describes its value either through [`Header::schema`] or through
/// [`Header::content`].
///
/// [header]: https://spec.openapis.org/oas/latest.html#header-object
#[non_exhaustive]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct Header {
/// Additional description of the header value.
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
/// Determines whether this header is mandatory. Defaults to `false`.
#[serde(skip_serializing_if = "Option::is_none")]
pub required: Option<bool>,
/// Declares the header deprecated and to be transitioned out of usage.
#[serde(skip_serializing_if = "Option::is_none")]
pub deprecated: Option<Deprecated>,
/// Schema of header type. Mutually exclusive with [`Header::content`].
#[serde(skip_serializing_if = "Option::is_none")]
pub schema: Option<RefOr<Schema>>,
/// Describes how the header value is serialized. The only legal value for headers is
/// [`ParameterStyle::Simple`], which is also the default.
#[serde(skip_serializing_if = "Option::is_none")]
pub style: Option<ParameterStyle>,
/// When `true`, `array` or `object` header values generate a single header whose value is a
/// comma-separated list. Defaults to `false`.
#[serde(skip_serializing_if = "Option::is_none")]
pub explode: Option<bool>,
/// Example of the header's potential value.
#[serde(skip_serializing_if = "Option::is_none")]
pub example: Option<Value>,
/// Examples of the header's potential value, indexed by name. Mutually exclusive with
/// [`Header::example`].
#[serde(skip_serializing_if = "PropMap::is_empty", default)]
pub examples: PropMap<String, RefOr<Example>>,
/// A map containing the representations for the header, keyed by media type. Per spec the
/// map must contain exactly one entry. Mutually exclusive with [`Header::schema`].
#[serde(skip_serializing_if = "PropMap::is_empty", default)]
pub content: PropMap<String, Content>,
/// Optional extensions "x-something"
#[serde(skip_serializing_if = "PropMap::is_empty", flatten)]
pub extensions: PropMap<String, serde_json::Value>,
}
impl Header {
/// Construct a new [`Header`] with custom schema. If you wish to construct a default
/// header with `String` type you can use [`Header::default`] function.
///
/// # Examples
///
/// Creates a new [`Header`] with an integer type.
/// ```
/// # use salvo_oapi::{Header, Object, BasicType};
/// let header = Header::new(Object::with_type(BasicType::Integer));
/// ```
///
/// Create a new [`Header`] with default type `String`
/// ```
/// # use salvo_oapi::Header;
/// let header = Header::default();
/// ```
#[must_use]
pub fn new<C: Into<RefOr<Schema>>>(component: C) -> Self {
Self {
schema: Some(component.into()),
..Default::default()
}
}
/// Construct a [`Header`] that describes its value with a media type instead of a schema.
///
/// ```
/// # use salvo_oapi::{Content, Header, Object, BasicType};
/// let header = Header::with_content(
/// "application/linkset",
/// Content::new(Object::with_type(BasicType::String)),
/// );
/// ```
#[must_use]
pub fn with_content<S: Into<String>, C: Into<Content>>(media_type: S, content: C) -> Self {
let mut header = Self {
schema: None,
..Default::default()
};
header.content.insert(media_type.into(), content.into());
header
}
/// Add schema of header.
#[must_use]
pub fn schema<I: Into<RefOr<Schema>>>(mut self, component: I) -> Self {
self.schema = Some(component.into());
self
}
/// Add additional description for header.
#[must_use]
pub fn description<S: Into<String>>(mut self, description: S) -> Self {
self.description = Some(description.into());
self
}
/// Declare whether the header is mandatory.
#[must_use]
pub fn required(mut self, required: bool) -> Self {
self.required = Some(required);
self
}
/// Declare the header deprecated.
#[must_use]
pub fn deprecated<D: Into<Deprecated>>(mut self, deprecated: D) -> Self {
self.deprecated = Some(deprecated.into());
self
}
/// Set the serialization style of the header. Only [`ParameterStyle::Simple`] is legal.
#[must_use]
pub fn style(mut self, style: ParameterStyle) -> Self {
self.style = Some(style);
self
}
/// Define whether `array` or `object` header values are exploded.
#[must_use]
pub fn explode(mut self, explode: bool) -> Self {
self.explode = Some(explode);
self
}
/// Add an example of the header's potential value.
#[must_use]
pub fn example(mut self, example: Value) -> Self {
self.example = Some(example);
self
}
/// Insert a named [`Example`] (or a [`Ref`](crate::Ref) to one) into [`Header::examples`].
#[must_use]
pub fn add_example<N: Into<String>, E: Into<RefOr<Example>>>(
mut self,
name: N,
example: E,
) -> Self {
self.examples.insert(name.into(), example.into());
self
}
/// Insert a single media-type entry into [`Header::content`].
///
/// Per spec the `content` map must contain exactly one entry. Mutually exclusive with
/// [`Header::schema`].
#[must_use]
pub fn content<S: Into<String>, C: Into<Content>>(mut self, media_type: S, content: C) -> Self {
self.content.insert(media_type.into(), content.into());
self
}
/// Add openapi extension (`x-something`) for [`Header`].
#[must_use]
pub fn add_extension<K: Into<String>>(mut self, key: K, value: serde_json::Value) -> Self {
self.extensions.insert(key.into(), value);
self
}
}
impl Default for Header {
fn default() -> Self {
Self {
description: Default::default(),
required: Default::default(),
deprecated: Default::default(),
schema: Some(Object::with_type(BasicType::String).into()),
style: Default::default(),
explode: Default::default(),
example: Default::default(),
examples: Default::default(),
content: Default::default(),
extensions: Default::default(),
}
}
}
#[cfg(test)]
mod tests {
use assert_json_diff::assert_json_eq;
use serde_json::json;
use super::*;
#[test]
fn test_build_header() {
let header = Header::new(Object::with_type(BasicType::String));
assert_json_eq!(
header,
json!({
"schema": {
"type": "string"
}
})
);
let header = header
.description("test description")
.schema(Object::with_type(BasicType::Number));
assert_json_eq!(
header,
json!({
"description": "test description",
"schema": {
"type": "number"
}
})
);
}
#[test]
fn header_full_surface_round_trips() {
let header = Header::new(Object::with_type(BasicType::String))
.description("rate limit")
.required(true)
.deprecated(crate::Deprecated::False)
.style(ParameterStyle::Simple)
.explode(false)
.example(json!("100"))
.add_extension("x-vendor", json!("acme"));
let value = serde_json::to_value(&header).expect("serialize");
assert_json_eq!(
&value,
json!({
"description": "rate limit",
"required": true,
"deprecated": false,
"schema": { "type": "string" },
"style": "simple",
"explode": false,
"example": "100",
"x-vendor": "acme"
})
);
let parsed: Header = serde_json::from_value(value).expect("deserialize");
assert_eq!(parsed, header);
}
#[test]
fn header_with_content_omits_schema() {
let header = Header::with_content(
"application/linkset",
Content::new(Object::with_type(BasicType::String)),
);
let value = serde_json::to_value(&header).expect("serialize");
assert_json_eq!(
&value,
json!({
"content": {
"application/linkset": { "schema": { "type": "string" } }
}
})
);
let parsed: Header = serde_json::from_value(value).expect("deserialize");
assert_eq!(parsed, header);
}
}