openapiv3/openapi.rs
1use crate::*;
2use indexmap::IndexMap;
3use serde::{Deserialize, Serialize};
4
5/// This is the root document object of the OpenAPI document.
6#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
7pub struct OpenAPI {
8 /// REQUIRED. This string MUST be the semantic version number of the
9 /// OpenAPI Specification version that the OpenAPI document uses.
10 /// The openapi field SHOULD be used by tooling specifications and
11 /// clients to interpret the OpenAPI document. This is not related to
12 /// the API info.version string.
13 pub openapi: String,
14 /// REQUIRED. Provides metadata about the API.
15 /// The metadata MAY be used by tooling as required.
16 pub info: Info,
17 /// An array of Server Objects, which provide connectivity information to a
18 /// target server. If the servers property is not provided, or is an empty
19 /// array, the default value would be a Server Object with a url value of /.
20 #[serde(default, skip_serializing_if = "Vec::is_empty")]
21 pub servers: Vec<Server>,
22 /// REQUIRED. The available paths and operations for the API.
23 pub paths: Paths,
24 /// An element to hold various schemas for the specification.
25 #[serde(skip_serializing_if = "Option::is_none")]
26 pub components: Option<Components>,
27 /// A declaration of which security mechanisms can be used across the API.
28 /// The list of values includes alternative security requirement objects
29 /// that can be used. Only one of the security requirement objects need to
30 /// be satisfied to authorize a request. Individual operations can override
31 /// this definition. Global security settings may be overridden on a per-path
32 /// basis.
33 #[serde(default, skip_serializing_if = "Option::is_none")]
34 pub security: Option<Vec<SecurityRequirement>>,
35 /// A list of tags used by the specification with additional metadata.
36 /// The order of the tags can be used to reflect on their order by the
37 /// parsing tools. Not all tags that are used by the Operation Object
38 /// must be declared. The tags that are not declared MAY be organized
39 /// randomly or based on the tool's logic. Each tag name in the list
40 /// MUST be unique.
41 #[serde(default, skip_serializing_if = "Vec::is_empty")]
42 pub tags: Vec<Tag>,
43 /// Additional external documentation.
44 #[serde(rename = "externalDocs", skip_serializing_if = "Option::is_none")]
45 pub external_docs: Option<ExternalDocumentation>,
46 /// Inline extensions to this object.
47 #[serde(flatten, deserialize_with = "crate::util::deserialize_extensions")]
48 pub extensions: IndexMap<String, serde_json::Value>,
49}
50
51impl OpenAPI {
52 /// Iterates through all [Operation]s in this API.
53 ///
54 /// The iterated items are tuples of `(&str, &str, &Operation)` containing
55 /// the path, method, and the operation.
56 ///
57 /// Path items containing `$ref`s are skipped.
58 pub fn operations(&self) -> impl Iterator<Item = (&str, &str, &Operation)> {
59 self.paths
60 .iter()
61 .filter_map(|(path, item)| item.as_item().map(|i| (path, i)))
62 .flat_map(|(path, item)| {
63 item.iter()
64 .map(move |(method, op)| (path.as_str(), method, op))
65 })
66 }
67}