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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
//! Implements content object for request body and response.
use serde::{Deserialize, Serialize};
use serde_json::Value;
use super::encoding::Encoding;
use super::example::Example;
use super::{PropMap, RefOr, Schema};
/// Content holds request body content or response content.
#[derive(Serialize, Deserialize, Default, Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct Content {
/// Reference to a reusable Media Type Object, e.g. one defined under
/// `components.mediaTypes`. Added in OpenAPI 3.2.
///
/// `content` maps are typed as [`Content`] rather than `RefOr<Content>` so that existing
/// callers keep compiling; a [`Content`] carrying only this field serializes exactly as a
/// Reference Object. As with any Reference Object, sibling fields are ignored by consumers.
///
/// ```
/// # use salvo_oapi::Content;
/// let content = Content::from_ref("#/components/mediaTypes/FramePayload");
/// ```
#[serde(rename = "$ref", skip_serializing_if = "Option::is_none", default)]
pub ref_location: Option<String>,
/// Schema used in response body or request body.
#[serde(skip_serializing_if = "Option::is_none")]
pub schema: Option<RefOr<Schema>>,
/// Schema describing each item within a sequential media type, e.g. `text/event-stream` or
/// `application/jsonl`. Added in OpenAPI 3.2.
///
/// Unlike [`Content::schema`], which applies to the complete content, `item_schema` applies
/// to each item in the stream independently. Both may be used together.
///
/// See <https://spec.openapis.org/oas/v3.2.0.html#media-type-object>.
#[serde(skip_serializing_if = "Option::is_none", default)]
pub item_schema: Option<RefOr<Schema>>,
/// Example for request body or response body.
#[serde(skip_serializing_if = "Option::is_none")]
pub example: Option<Value>,
/// Examples of the request body or response body. [`Content::examples`] should match to
/// media type and specified schema if present. [`Content::examples`] and
/// [`Content::example`] are mutually exclusive. If both are defined `examples` will
/// override value in `example`.
#[serde(default, skip_serializing_if = "PropMap::is_empty")]
pub examples: PropMap<String, RefOr<Example>>,
/// A map between a property name and its encoding information.
///
/// The key, being the property name, MUST exist in the [`Content::schema`] as a property, with
/// `schema` being a [`Schema::Object`] and this object containing the same property key in
/// [`Object::properties`](crate::schema::Object::properties).
///
/// The encoding object SHALL only apply to `request_body` objects when the media type is
/// multipart or `application/x-www-form-urlencoded`.
///
/// Must not be combined with [`Content::prefix_encoding`] or [`Content::item_encoding`].
#[serde(skip_serializing_if = "PropMap::is_empty", default)]
pub encoding: PropMap<String, Encoding>,
/// Positional encoding information, applied to the array item at the same index. Added in
/// OpenAPI 3.2 and only applicable to `multipart` media types.
///
/// Requires either [`Content::item_schema`] or an array [`Content::schema`] to be present,
/// and must not be combined with [`Content::encoding`].
#[serde(skip_serializing_if = "Vec::is_empty", default)]
pub prefix_encoding: Vec<Encoding>,
/// A single encoding applied to all array items not covered by
/// [`Content::prefix_encoding`]. Added in OpenAPI 3.2 and only applicable to `multipart`
/// media types.
///
/// Requires either [`Content::item_schema`] or an array [`Content::schema`] to be present,
/// and must not be combined with [`Content::encoding`].
#[serde(skip_serializing_if = "Option::is_none", default)]
pub item_encoding: Option<Encoding>,
/// Optional extensions "x-something"
#[serde(skip_serializing_if = "PropMap::is_empty", flatten)]
pub extensions: PropMap<String, serde_json::Value>,
}
impl Content {
/// Construct a new [`Content`].
#[must_use]
pub fn new<I: Into<RefOr<Schema>>>(schema: I) -> Self {
Self {
schema: Some(schema.into()),
..Self::default()
}
}
/// Construct a [`Content`] that is purely a reference to a reusable Media Type Object.
/// Requires OpenAPI 3.2.
#[must_use]
pub fn from_ref<S: Into<String>>(ref_location: S) -> Self {
Self {
ref_location: Some(ref_location.into()),
..Self::default()
}
}
/// Set the `$ref` location for this [`Content`] and return `self`. Requires OpenAPI 3.2.
#[must_use]
pub fn ref_location<S: Into<String>>(mut self, ref_location: S) -> Self {
self.ref_location = Some(ref_location.into());
self
}
/// Add schema.
#[must_use]
pub fn schema<I: Into<RefOr<Schema>>>(mut self, component: I) -> Self {
self.schema = Some(component.into());
self
}
/// Add the schema describing each item of a sequential media type.
/// See [`Content::item_schema`]. Requires OpenAPI 3.2.
#[must_use]
pub fn item_schema<I: Into<RefOr<Schema>>>(mut self, component: I) -> Self {
self.item_schema = Some(component.into());
self
}
/// Add example of schema.
#[must_use]
pub fn example(mut self, example: Value) -> Self {
self.example = Some(example);
self
}
/// Add iterator of _`(N, V)`_ where `N` is name of example and `V` is [`Example`][example] to
/// [`Content`] of a request body or response body.
///
/// [`Content::examples`] and [`Content::example`] are mutually exclusive. If both are defined
/// `examples` will override value in `example`.
///
/// [example]: ../example/Example.html
#[must_use]
pub fn extend_examples<
E: IntoIterator<Item = (N, V)>,
N: Into<String>,
V: Into<RefOr<Example>>,
>(
mut self,
examples: E,
) -> Self {
self.examples.extend(
examples
.into_iter()
.map(|(name, example)| (name.into(), example.into())),
);
self
}
/// Add openapi extensions (`x-something`) for [`Content`].
#[must_use]
pub fn extensions(mut self, extensions: PropMap<String, serde_json::Value>) -> Self {
self.extensions = extensions;
self
}
/// Add an encoding.
///
/// The `property_name` MUST exist in the [`Content::schema`] as a property,
/// with `schema` being a [`Schema::Object`] and this object containing the same property
/// key in [`Object::properties`](crate::openapi::schema::Object::properties).
///
/// The encoding object SHALL only apply to `request_body` objects when the media type is
/// multipart or `application/x-www-form-urlencoded`.
#[must_use]
pub fn encoding<S: Into<String>, E: Into<Encoding>>(
mut self,
property_name: S,
encoding: E,
) -> Self {
self.encoding.insert(property_name.into(), encoding.into());
self
}
/// Set the positional encodings. See [`Content::prefix_encoding`]. Requires OpenAPI 3.2.
#[must_use]
pub fn prefix_encoding<I: IntoIterator<Item = Encoding>>(mut self, prefix_encoding: I) -> Self {
self.prefix_encoding = prefix_encoding.into_iter().collect();
self
}
/// Set the encoding applied to remaining array items. See [`Content::item_encoding`].
/// Requires OpenAPI 3.2.
#[must_use]
pub fn item_encoding<E: Into<Encoding>>(mut self, item_encoding: E) -> Self {
self.item_encoding = Some(item_encoding.into());
self
}
}
impl From<RefOr<Schema>> for Content {
fn from(schema: RefOr<Schema>) -> Self {
Self {
schema: Some(schema),
..Self::default()
}
}
}
#[cfg(test)]
mod tests {
use assert_json_diff::assert_json_eq;
use serde_json::{Map, json};
use super::*;
#[test]
fn test_build_content() {
let content = Content::new(RefOr::Ref(crate::Ref::from_schema_name("MySchema")))
.example(Value::Object(Map::from_iter([(
"schema".into(),
Value::String("MySchema".to_owned()),
)])))
.encoding(
"schema".to_owned(),
Encoding::default().content_type("text/plain"),
);
assert_json_eq!(
content,
json!({
"schema": {
"$ref": "#/components/schemas/MySchema"
},
"example": {
"schema": "MySchema"
},
"encoding": {
"schema": {
"contentType": "text/plain"
}
}
})
);
let content = content
.schema(RefOr::Ref(crate::Ref::from_schema_name("NewSchema")))
.extend_examples([(
"example1".to_owned(),
Example::new().value(Value::Object(Map::from_iter([(
"schema".into(),
Value::String("MySchema".to_owned()),
)]))),
)]);
assert_json_eq!(
content,
json!({
"schema": {
"$ref": "#/components/schemas/NewSchema"
},
"example": {
"schema": "MySchema"
},
"examples": {
"example1": {
"value": {
"schema": "MySchema"
}
}
},
"encoding": {
"schema": {
"contentType": "text/plain"
}
}
})
);
}
#[test]
fn content_ref_serializes_as_a_reference_object() {
let content = Content::from_ref("#/components/mediaTypes/FramePayload");
let value = serde_json::to_value(&content).expect("serialize");
assert_json_eq!(
&value,
json!({ "$ref": "#/components/mediaTypes/FramePayload" })
);
let parsed: Content = serde_json::from_value(value).expect("deserialize");
assert_eq!(parsed, content);
assert!(parsed.schema.is_none());
}
#[test]
fn test_content_openapi_3_2_streaming_fields() {
let content = Content::default()
.item_schema(crate::Ref::from_schema_name("Frame"))
.prefix_encoding([Encoding::default().content_type("text/html")])
.item_encoding(Encoding::default().content_type("image/*"));
let value = serde_json::to_value(&content).expect("serialize");
assert_json_eq!(
&value,
json!({
"itemSchema": { "$ref": "#/components/schemas/Frame" },
"prefixEncoding": [ { "contentType": "text/html" } ],
"itemEncoding": { "contentType": "image/*" }
})
);
let parsed: Content = serde_json::from_value(value).expect("deserialize");
assert_eq!(parsed, content);
}
}