1use std::collections::BTreeMap;
4
5use indexmap::IndexMap;
6use serde::Serialize;
7use serde_json::Value;
8
9use crate::Schema;
10
11pub type PathItem = IndexMap<String, Operation>;
14
15#[derive(Clone, Debug, PartialEq, Serialize)]
17pub struct Document {
18 pub openapi: String,
20 pub info: Info,
22 #[serde(skip_serializing_if = "Vec::is_empty")]
24 pub servers: Vec<Server>,
25 #[serde(skip_serializing_if = "Vec::is_empty")]
27 pub tags: Vec<Tag>,
28 pub paths: IndexMap<String, PathItem>,
30 pub components: Components,
32}
33
34#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
36pub struct Info {
37 pub title: String,
39 pub version: String,
41}
42
43#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
45pub struct Server {
46 pub url: String,
48}
49
50#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
52pub struct Tag {
53 pub name: String,
55}
56
57#[derive(Clone, Debug, Default, PartialEq, Serialize)]
59pub struct Operation {
60 #[serde(skip_serializing_if = "Vec::is_empty")]
62 pub tags: Vec<String>,
63 #[serde(skip_serializing_if = "Option::is_none")]
65 pub summary: Option<String>,
66 #[serde(skip_serializing_if = "Option::is_none")]
68 pub description: Option<String>,
69 #[serde(rename = "operationId", skip_serializing_if = "Option::is_none")]
71 pub operation_id: Option<String>,
72 #[serde(skip_serializing_if = "Vec::is_empty")]
74 pub parameters: Vec<Parameter>,
75 #[serde(rename = "requestBody", skip_serializing_if = "Option::is_none")]
77 pub request_body: Option<RequestBody>,
78 pub responses: IndexMap<String, Response>,
80 #[serde(skip_serializing_if = "Vec::is_empty")]
82 pub security: Vec<BTreeMap<String, Vec<String>>>,
83 #[serde(flatten)]
85 pub extensions: BTreeMap<String, Value>,
86}
87
88#[derive(Clone, Debug, PartialEq, Serialize)]
90pub struct Parameter {
91 pub name: String,
93 #[serde(rename = "in")]
95 pub location: String,
96 pub required: bool,
98 pub schema: Schema,
100 #[serde(skip_serializing_if = "Option::is_none")]
102 pub description: Option<String>,
103 #[serde(skip_serializing_if = "Option::is_none")]
105 pub style: Option<String>,
106 #[serde(skip_serializing_if = "Option::is_none")]
108 pub explode: Option<bool>,
109}
110
111impl Parameter {
112 pub fn path(name: impl Into<String>, schema: Schema) -> Self {
114 Self::new(name, "path", true, schema)
115 }
116
117 pub fn query(name: impl Into<String>, schema: Schema) -> Self {
119 Self::new(name, "query", false, schema)
120 }
121
122 fn new(name: impl Into<String>, location: &str, required: bool, schema: Schema) -> Self {
123 Self {
124 name: name.into(),
125 location: location.to_owned(),
126 required,
127 schema,
128 description: None,
129 style: None,
130 explode: None,
131 }
132 }
133
134 #[must_use]
136 pub fn described(mut self, text: impl Into<String>) -> Self {
137 self.description = Some(text.into());
138 self
139 }
140
141 #[must_use]
143 pub fn required(mut self) -> Self {
144 self.required = true;
145 self
146 }
147
148 #[must_use]
150 pub fn deep_object(mut self) -> Self {
151 self.style = Some("deepObject".to_owned());
152 self.explode = Some(true);
153 self
154 }
155
156 #[must_use]
158 pub fn comma_separated(mut self) -> Self {
159 self.style = Some("form".to_owned());
160 self.explode = Some(false);
161 self
162 }
163}
164
165#[derive(Clone, Debug, PartialEq, Serialize)]
167pub struct RequestBody {
168 pub required: bool,
170 pub content: IndexMap<String, MediaType>,
172}
173
174#[derive(Clone, Debug, PartialEq, Serialize)]
176pub struct Response {
177 pub description: String,
179 #[serde(skip_serializing_if = "IndexMap::is_empty")]
181 pub content: IndexMap<String, MediaType>,
182}
183
184#[derive(Clone, Debug, PartialEq, Serialize)]
186pub struct MediaType {
187 pub schema: Schema,
189}
190
191#[derive(Clone, Debug, Default, PartialEq, Serialize)]
193pub struct Components {
194 pub schemas: BTreeMap<String, Schema>,
196 #[serde(rename = "securitySchemes")]
198 pub security_schemes: IndexMap<String, SecurityScheme>,
199}
200
201#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
203pub struct SecurityScheme {
204 #[serde(rename = "type")]
206 pub kind: String,
207 #[serde(rename = "in", skip_serializing_if = "Option::is_none")]
209 pub location: Option<String>,
210 #[serde(skip_serializing_if = "Option::is_none")]
212 pub name: Option<String>,
213 #[serde(skip_serializing_if = "Option::is_none")]
215 pub scheme: Option<String>,
216 #[serde(rename = "bearerFormat", skip_serializing_if = "Option::is_none")]
218 pub bearer_format: Option<String>,
219 #[serde(skip_serializing_if = "Option::is_none")]
221 pub description: Option<String>,
222}