Skip to main content

navree_openapi/
document.rs

1//! The OpenAPI 3.1 document: [`Document`] and the objects inside it.
2
3use std::collections::BTreeMap;
4
5use indexmap::IndexMap;
6use serde::Serialize;
7use serde_json::Value;
8
9use crate::Schema;
10
11/// The operations of one path, by lower-case method (`get`, `post`), in the
12/// order they were added.
13pub type PathItem = IndexMap<String, Operation>;
14
15/// An OpenAPI document (the root object).
16#[derive(Clone, Debug, PartialEq, Serialize)]
17pub struct Document {
18    /// The OpenAPI version.
19    pub openapi: String,
20    /// The API's title and version.
21    pub info: Info,
22    /// The server URLs; empty leaves clients with the URL of the document.
23    #[serde(skip_serializing_if = "Vec::is_empty")]
24    pub servers: Vec<Server>,
25    /// The tags, in the order operations first use them.
26    #[serde(skip_serializing_if = "Vec::is_empty")]
27    pub tags: Vec<Tag>,
28    /// The paths, in the order they were added.
29    pub paths: IndexMap<String, PathItem>,
30    /// The component schemas and security schemes.
31    pub components: Components,
32}
33
34/// `info`.
35#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
36pub struct Info {
37    /// The API's title.
38    pub title: String,
39    /// The API's version.
40    pub version: String,
41}
42
43/// One entry of `servers`.
44#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
45pub struct Server {
46    /// The server's URL.
47    pub url: String,
48}
49
50/// One entry of `tags`.
51#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
52pub struct Tag {
53    /// The tag's name.
54    pub name: String,
55}
56
57/// An operation: what one method on one path does.
58#[derive(Clone, Debug, Default, PartialEq, Serialize)]
59pub struct Operation {
60    /// The groups the operation belongs to.
61    #[serde(skip_serializing_if = "Vec::is_empty")]
62    pub tags: Vec<String>,
63    /// A one-line summary.
64    #[serde(skip_serializing_if = "Option::is_none")]
65    pub summary: Option<String>,
66    /// A longer description (Markdown).
67    #[serde(skip_serializing_if = "Option::is_none")]
68    pub description: Option<String>,
69    /// The unique operation id.
70    #[serde(rename = "operationId", skip_serializing_if = "Option::is_none")]
71    pub operation_id: Option<String>,
72    /// The parameters.
73    #[serde(skip_serializing_if = "Vec::is_empty")]
74    pub parameters: Vec<Parameter>,
75    /// The request body.
76    #[serde(rename = "requestBody", skip_serializing_if = "Option::is_none")]
77    pub request_body: Option<RequestBody>,
78    /// The responses, by status, in the order they were added.
79    pub responses: IndexMap<String, Response>,
80    /// The security requirements; any one of them is enough. Empty means none.
81    #[serde(skip_serializing_if = "Vec::is_empty")]
82    pub security: Vec<BTreeMap<String, Vec<String>>>,
83    /// Specification extensions (`x-navree-flag`).
84    #[serde(flatten)]
85    pub extensions: BTreeMap<String, Value>,
86}
87
88/// A parameter of an operation.
89#[derive(Clone, Debug, PartialEq, Serialize)]
90pub struct Parameter {
91    /// The parameter's name.
92    pub name: String,
93    /// Where it goes: `path`, `query`, `header` or `cookie`.
94    #[serde(rename = "in")]
95    pub location: String,
96    /// Whether the operation needs it.
97    pub required: bool,
98    /// Its schema.
99    pub schema: Schema,
100    /// What it means (Markdown).
101    #[serde(skip_serializing_if = "Option::is_none")]
102    pub description: Option<String>,
103    /// How an array or an object is written: `form`, `deepObject` and so on.
104    #[serde(skip_serializing_if = "Option::is_none")]
105    pub style: Option<String>,
106    /// Whether each member of an array or object is a parameter of its own.
107    #[serde(skip_serializing_if = "Option::is_none")]
108    pub explode: Option<bool>,
109}
110
111impl Parameter {
112    /// A required path parameter.
113    pub fn path(name: impl Into<String>, schema: Schema) -> Self {
114        Self::new(name, "path", true, schema)
115    }
116
117    /// An optional query parameter.
118    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    /// Sets `description`.
135    #[must_use]
136    pub fn described(mut self, text: impl Into<String>) -> Self {
137        self.description = Some(text.into());
138        self
139    }
140
141    /// Marks the parameter as required.
142    #[must_use]
143    pub fn required(mut self) -> Self {
144        self.required = true;
145        self
146    }
147
148    /// An object written as `name[key]=value` (`style: deepObject`, `explode: true`).
149    #[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    /// A list written as one comma-separated value (`style: form`, `explode: false`).
157    #[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/// A request body.
166#[derive(Clone, Debug, PartialEq, Serialize)]
167pub struct RequestBody {
168    /// Whether the operation needs it.
169    pub required: bool,
170    /// The body by content type.
171    pub content: IndexMap<String, MediaType>,
172}
173
174/// A response.
175#[derive(Clone, Debug, PartialEq, Serialize)]
176pub struct Response {
177    /// What the response means.
178    pub description: String,
179    /// The body by content type.
180    #[serde(skip_serializing_if = "IndexMap::is_empty")]
181    pub content: IndexMap<String, MediaType>,
182}
183
184/// A body of one content type.
185#[derive(Clone, Debug, PartialEq, Serialize)]
186pub struct MediaType {
187    /// The body's schema.
188    pub schema: Schema,
189}
190
191/// `components`.
192#[derive(Clone, Debug, Default, PartialEq, Serialize)]
193pub struct Components {
194    /// The schemas `$ref`s point at, by name.
195    pub schemas: BTreeMap<String, Schema>,
196    /// The security schemes, by name.
197    #[serde(rename = "securitySchemes")]
198    pub security_schemes: IndexMap<String, SecurityScheme>,
199}
200
201/// A security scheme.
202#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
203pub struct SecurityScheme {
204    /// `apiKey` or `http`.
205    #[serde(rename = "type")]
206    pub kind: String,
207    /// For `apiKey`: where the key goes.
208    #[serde(rename = "in", skip_serializing_if = "Option::is_none")]
209    pub location: Option<String>,
210    /// For `apiKey`: the name of the cookie or header.
211    #[serde(skip_serializing_if = "Option::is_none")]
212    pub name: Option<String>,
213    /// For `http`: the authorization scheme.
214    #[serde(skip_serializing_if = "Option::is_none")]
215    pub scheme: Option<String>,
216    /// For `http` with bearer tokens: their format.
217    #[serde(rename = "bearerFormat", skip_serializing_if = "Option::is_none")]
218    pub bearer_format: Option<String>,
219    /// What the scheme is.
220    #[serde(skip_serializing_if = "Option::is_none")]
221    pub description: Option<String>,
222}