openapiv3_1/
xml.rs

1//! Implements [OpenAPI Xml Object][xml_object] types.
2//!
3//! [xml_object]: https://spec.openapis.org/oas/latest.html#xml-object
4use std::borrow::Cow;
5
6use serde_derive::{Deserialize, Serialize};
7
8/// Implements [OpenAPI Xml Object][xml_object].
9///
10/// Can be used to modify xml output format of specific [OpenAPI Schema Object][schema_object] which are
11/// implemented in [`schema`][schema] module.
12///
13/// [xml_object]: https://spec.openapis.org/oas/latest.html#xml-object
14/// [schema_object]: https://spec.openapis.org/oas/latest.html#schema-object
15/// [schema]: ../schema/index.html
16#[non_exhaustive]
17#[derive(Serialize, Deserialize, Default, Clone, PartialEq, Eq, bon::Builder)]
18#[cfg_attr(feature = "debug", derive(Debug))]
19#[builder(on(_, into))]
20pub struct Xml {
21    /// Used to replace the name of attribute or type used in schema property.
22    /// When used with [`Xml::wrapped`] attribute the name will be used as a wrapper name
23    /// for wrapped array instead of the item or type name.
24    #[serde(skip_serializing_if = "Option::is_none")]
25    pub name: Option<Cow<'static, str>>,
26
27    /// Valid uri definition of namespace used in xml.
28    #[serde(skip_serializing_if = "Option::is_none")]
29    pub namespace: Option<Cow<'static, str>>,
30
31    /// Prefix for xml element [`Xml::name`].
32    #[serde(skip_serializing_if = "Option::is_none")]
33    pub prefix: Option<Cow<'static, str>>,
34
35    /// Flag deciding will this attribute translate to element attribute instead of xml element.
36    #[serde(skip_serializing_if = "Option::is_none")]
37    pub attribute: Option<bool>,
38
39    /// Flag only usable with array definition. If set to true the output xml will wrap the array of items
40    /// `<pets><pet></pet></pets>` instead of unwrapped `<pet></pet>`.
41    #[serde(skip_serializing_if = "Option::is_none")]
42    pub wrapped: Option<bool>,
43}
44
45impl Xml {
46    /// Construct a new [`Xml`] object.
47    pub fn new() -> Self {
48        Self { ..Default::default() }
49    }
50}
51
52#[cfg(test)]
53#[cfg_attr(coverage_nightly, coverage(off))]
54mod tests {
55    use super::Xml;
56
57    #[test]
58    fn xml_new() {
59        let xml = Xml::new();
60
61        assert!(xml.name.is_none());
62        assert!(xml.namespace.is_none());
63        assert!(xml.prefix.is_none());
64        assert!(xml.attribute.is_none());
65        assert!(xml.wrapped.is_none());
66    }
67}