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
use serde::{Deserialize, Serialize};
use serde_json::Value;
use crate::{Deprecated, RefOr, Schema, SchemaType, Xml};
/// Array represents [`Vec`] or [`slice`] type of items.
///
/// See [`Schema::Array`] for more details.
#[non_exhaustive]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct Array {
/// Type will always be [`SchemaType::Array`]
#[serde(rename = "type")]
pub schema_type: SchemaType,
/// Changes the [`Array`] title.
#[serde(skip_serializing_if = "Option::is_none")]
pub title: Option<String>,
/// Schema representing the array items type.
pub items: Box<RefOr<Schema>>,
/// Description of the [`Array`]. Markdown syntax is supported.
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
/// Marks the [`Array`] deprecated.
#[serde(skip_serializing_if = "Option::is_none")]
pub deprecated: Option<Deprecated>,
/// Example shown in UI of the value for richer documentation.
#[serde(skip_serializing_if = "Option::is_none")]
pub example: Option<Value>,
/// Default value which is provided when user has not provided the input in Swagger UI.
#[serde(skip_serializing_if = "Option::is_none")]
pub default: Option<Value>,
/// Max length of the array.
#[serde(skip_serializing_if = "Option::is_none")]
pub max_items: Option<usize>,
/// Min length of the array.
#[serde(skip_serializing_if = "Option::is_none")]
pub min_items: Option<usize>,
/// Setting this to `true` will validate successfully if all elements of this [`Array`] are
/// unique.
#[serde(default, skip_serializing_if = "super::is_false")]
pub unique_items: bool,
/// Xml format of the array.
#[serde(skip_serializing_if = "Option::is_none")]
pub xml: Option<Xml>,
/// Set `true` to allow `"null"` to be used as value for given type.
#[serde(default, skip_serializing_if = "super::is_false")]
pub nullable: bool,
}
impl Default for Array {
fn default() -> Self {
Self {
title: Default::default(),
schema_type: SchemaType::Array,
unique_items: bool::default(),
items: Default::default(),
description: Default::default(),
deprecated: Default::default(),
example: Default::default(),
default: Default::default(),
max_items: Default::default(),
min_items: Default::default(),
xml: Default::default(),
nullable: Default::default(),
}
}
}
impl Array {
/// Construct a new [`Array`] component from given [`Schema`].
///
/// # Examples
///
/// Create a `String` array component.
/// ```
/// # use salvo_oapi::schema::{Schema, Array, SchemaType, Object};
/// let string_array = Array::new(Object::with_type(SchemaType::String));
/// ```
pub fn new<I: Into<RefOr<Schema>>>(component: I) -> Self {
Self {
items: Box::new(component.into()),
..Default::default()
}
}
/// Set [`Schema`] type for the [`Array`].
pub fn items<I: Into<RefOr<Schema>>>(mut self, component: I) -> Self {
self.items = Box::new(component.into());
self
}
/// Add or change the title of the [`Array`].
pub fn title(mut self, title: impl Into<String>) -> Self {
self.title = Some(title.into());
self
}
/// Add or change description of the property. Markdown syntax is supported.
pub fn description(mut self, description: impl Into<String>) -> Self {
self.description = Some(description.into());
self
}
/// Add or change deprecated status for [`Array`].
pub fn deprecated(mut self, deprecated: Deprecated) -> Self {
self.deprecated = Some(deprecated);
self
}
/// Add or change example shown in UI of the value for richer documentation.
pub fn example(mut self, example: Value) -> Self {
self.example = Some(example);
self
}
/// Add or change default value for the object which is provided when user has not provided the input in Swagger UI.
pub fn default(mut self, default: Value) -> Self {
self.default = Some(default);
self
}
/// Set maximum allowed length for [`Array`].
pub fn max_items(mut self, max_items: usize) -> Self {
self.max_items = Some(max_items);
self
}
/// Set minimum allowed length for [`Array`].
pub fn min_items(mut self, min_items: usize) -> Self {
self.min_items = Some(min_items);
self
}
/// Set or change whether [`Array`] should enforce all items to be unique.
pub fn unique_items(mut self, unique_items: bool) -> Self {
self.unique_items = unique_items;
self
}
/// Set [`Xml`] formatting for [`Array`].
pub fn xml(mut self, xml: Xml) -> Self {
self.xml = Some(xml);
self
}
/// Add or change nullable flag for [`Object`].
pub fn nullable(mut self, nullable: bool) -> Self {
self.nullable = nullable;
self
}
}
impl From<Array> for Schema {
fn from(array: Array) -> Self {
Self::Array(array)
}
}
impl From<Array> for RefOr<Schema> {
fn from(array: Array) -> Self {
Self::T(Schema::Array(array))
}
}
impl ToArray for Array {}
/// Trait for converting a type to [`Array`].
pub trait ToArray
where
RefOr<Schema>: From<Self>,
Self: Sized,
{
/// Convert a type to [`Array`].
fn to_array(self) -> Array {
Array::new(self)
}
}