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