anicca/openapi/schema.rs
1use super::reference::ReferenceOr;
2use std::collections::BTreeMap;
3use serde::{Deserialize, Serialize};
4
5/// The Schema Object allows the definition of input and output data types.
6/// These types can be objects, but also primitives and arrays.
7/// This object is an extended subset of the
8/// [JSON Schema Specification Wright Draft 00](http://json-schema.org/).
9/// For more information about the properties, see
10/// [JSON Schema Core](https://tools.ietf.org/html/draft-wright-json-schema-00) and
11/// [JSON Schema Validation](https://tools.ietf.org/html/draft-wright-json-schema-validation-00).
12/// Unless stated otherwise, the property definitions follow the JSON Schema.
13///
14/// See <https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.1.md#schemaObject>.
15#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Default)]
16pub struct Schema {
17 #[serde(skip_serializing_if = "Option::is_none")]
18 pub description: Option<String>,
19
20 #[serde(skip_serializing_if = "Option::is_none")]
21 #[serde(rename = "type")]
22 pub schema_type: Option<String>,
23
24 #[serde(skip_serializing_if = "Option::is_none")]
25 pub format: Option<String>,
26
27 #[serde(skip_serializing_if = "Option::is_none")]
28 #[serde(rename = "enum")]
29 pub enum_values: Option<Vec<Option<String>>>,
30
31 #[serde(skip_serializing_if = "Option::is_none")]
32 pub required: Option<Vec<String>>,
33
34 #[serde(skip_serializing_if = "Option::is_none")]
35 pub items: Option<Box<Schema>>,
36
37 #[serde(skip_serializing_if = "Option::is_none")]
38 pub properties: Option<BTreeMap<String, Schema>>,
39
40 #[serde(skip_serializing_if = "Option::is_none", rename = "readOnly")]
41 pub read_only: Option<bool>,
42
43 #[serde(skip_serializing_if = "Option::is_none")]
44 pub nullable: Option<bool>,
45
46 /// Value can be boolean or object. Inline or referenced schema MUST be of a
47 /// [Schema Object](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.1.md#schemaObject)
48 /// and not a standard JSON Schema.
49 ///
50 /// See <https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.1.md#properties>.
51 #[serde(
52 skip_serializing_if = "Option::is_none",
53 rename = "additionalProperties"
54 )]
55 pub additional_properties: Option<AdditionalProperties>,
56
57 /// A free-form property to include an example of an instance for this schema.
58 /// To represent examples that cannot be naturally represented in JSON or YAML,
59 /// a string value can be used to contain the example with escaping where necessary.
60 /// NOTE: According to [spec], _Primitive data types in the OAS are based on the
61 /// types supported by the JSON Schema Specification Wright Draft 00._
62 /// This suggest using
63 /// [`serde_json::Value`](https://docs.serde.rs/serde_json/value/enum.Value.html). [spec][https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.1.md#data-types]
64 #[serde(skip_serializing_if = "Option::is_none")]
65 pub example: Option<serde_json::value::Value>,
66
67 #[serde(skip_serializing_if = "Option::is_none")]
68 pub title: Option<String>,
69
70 /// The default value represents what would be assumed by the consumer of the input as the value
71 /// of the schema if one is not provided. Unlike JSON Schema, the value MUST conform to the
72 /// defined type for the Schema Object defined at the same level. For example, if type is
73 /// `string`, then `default` can be `"foo"` but cannot be `1`.
74 #[serde(skip_serializing_if = "Option::is_none")]
75 pub default: Option<serde_json::Value>,
76
77 #[serde(skip_serializing_if = "Option::is_none")]
78 pub minimum: Option<serde_json::Value>,
79
80 /// Inline or referenced schema MUST be of a [Schema Object](#schemaObject) and not a standard
81 /// JSON Schema.
82 /// [allOf](https://swagger.io/docs/specification/data-models/oneof-anyof-allof-not/#allof)
83 #[serde(rename = "allOf", skip_serializing_if = "Option::is_none")]
84 pub all_of: Option<Vec<ReferenceOr<Schema>>>,
85
86 /// Inline or referenced schema MUST be of a [Schema Object](#schemaObject) and not a standard
87 /// JSON Schema.
88 /// [oneOf](https://swagger.io/docs/specification/data-models/oneof-anyof-allof-not/#oneof)
89 #[serde(rename = "oneOf", skip_serializing_if = "Option::is_none")]
90 pub one_of: Option<Vec<ReferenceOr<Schema>>>,
91
92 /// Inline or referenced schema MUST be of a [Schema Object](#schemaObject) and not a standard
93 /// JSON Schema.
94 /// [anyOf](https://swagger.io/docs/specification/data-models/oneof-anyof-allof-not/#anyof)
95 #[serde(rename = "anyOf", skip_serializing_if = "Option::is_none")]
96 pub any_of: Option<Vec<ReferenceOr<Schema>>>,
97
98 /// Inline or referenced schema MUST be of a [Schema Object](#schemaObject) and not a standard
99 /// JSON Schema.
100 /// [not](https://swagger.io/docs/specification/data-models/oneof-anyof-allof-not/#not)
101 #[serde(rename = "not", skip_serializing_if = "Option::is_none")]
102 pub not: Option<Vec<ReferenceOr<Schema>>>,
103
104 #[serde(rename = "maxLength", skip_serializing_if = "Option::is_none")]
105 pub max_length: Option<u32>,
106
107 #[serde(rename = "minLength", skip_serializing_if = "Option::is_none")]
108 pub min_length: Option<u32>,
109
110 /// [Specification extensions](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#specificationExtensions)
111 #[serde(flatten)]
112 pub extensions:BTreeMap<String, serde_json::Value>,
113}
114
115#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
116#[serde(untagged)]
117pub enum AdditionalProperties {
118 Any(bool),
119 Schema(Box<ReferenceOr<Schema>>),
120}