Skip to main content

hyperlane_plugin_openapi/
struct.rs

1use super::*;
2
3/// OpenAPI 3.1.0 `info` metadata object.
4#[derive(Clone, Data, Debug, Deserialize, Eq, PartialEq, Serialize)]
5pub struct OpenApiInfo {
6    /// Human-readable API title.
7    pub(crate) title: String,
8    /// API semantic version string.
9    pub(crate) version: String,
10    /// Optional Markdown description of the service.
11    #[serde(skip_serializing_if = "Option::is_none")]
12    pub(crate) description: Option<String>,
13}
14
15/// OpenAPI 3.1.0 `server` entry.
16#[derive(Clone, Debug, Deserialize, Eq, Getter, PartialEq, Serialize)]
17pub struct OpenApiServer {
18    /// Base URL for the target host.
19    pub(crate) url: String,
20    /// Optional human-readable server environment label.
21    #[serde(skip_serializing_if = "Option::is_none")]
22    pub(crate) description: Option<String>,
23}
24
25/// OpenAPI 3.1.0 parameter descriptor (`query`, `header`, `path`, `cookie`).
26#[derive(Clone, Debug, Deserialize, Eq, Getter, PartialEq, Serialize)]
27pub struct OpenApiParameter {
28    /// Parameter name.
29    pub(crate) name: String,
30    /// Parameter location (`"path"`, `"query"`, `"header"`, `"cookie"`).
31    #[serde(rename = "in")]
32    pub(crate) location: String,
33    /// Whether the parameter is required.
34    #[get(type(copy))]
35    pub(crate) required: bool,
36    /// Optional parameter description.
37    #[serde(skip_serializing_if = "Option::is_none")]
38    pub(crate) description: Option<String>,
39}
40
41/// OpenAPI 3.1.0 response entry keyed by HTTP status code string.
42#[derive(Clone, Debug, Deserialize, Eq, Getter, PartialEq, Serialize)]
43pub struct OpenApiResponse {
44    /// Human-readable description of the response.
45    pub(crate) description: String,
46    /// Optional content schema definitions keyed by MIME type.
47    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
48    pub(crate) content: BTreeMap<String, Value>,
49}
50
51/// OpenAPI 3.1.0 operation descriptor under a path and HTTP method.
52#[derive(Clone, Data, Debug, Deserialize, Eq, PartialEq, Serialize)]
53pub struct OpenApiOperation {
54    /// Short summary line.
55    pub(crate) summary: String,
56    /// Optional detailed operation description.
57    #[serde(skip_serializing_if = "Option::is_none")]
58    pub(crate) description: Option<String>,
59    /// Optional unique operation identifier.
60    #[serde(rename = "operationId", skip_serializing_if = "Option::is_none")]
61    pub(crate) operation_id: Option<String>,
62    /// Grouping tags.
63    #[serde(default, skip_serializing_if = "Vec::is_empty")]
64    pub(crate) tags: Vec<String>,
65    /// Operation parameters.
66    #[serde(default, skip_serializing_if = "Vec::is_empty")]
67    pub(crate) parameters: Vec<OpenApiParameter>,
68    /// Responses keyed by status code string (for example `"200"`).
69    pub(crate) responses: BTreeMap<String, OpenApiResponse>,
70}
71
72/// Complete OpenAPI 3.1.0 document structure.
73#[derive(Clone, Data, Debug, Deserialize, Eq, PartialEq, Serialize)]
74pub struct OpenApiSpec {
75    /// OpenAPI specification version (`"3.1.0"`).
76    pub(crate) openapi: String,
77    /// API metadata.
78    pub(crate) info: OpenApiInfo,
79    /// Configured server base URLs.
80    #[serde(default, skip_serializing_if = "Vec::is_empty")]
81    pub(crate) servers: Vec<OpenApiServer>,
82    /// Operations grouped by `path -> lowercase_method -> operation`.
83    pub(crate) paths: BTreeMap<String, BTreeMap<String, OpenApiOperation>>,
84}
85
86/// Configuration for [`OpenApiPlugin`].
87#[derive(Clone, Data, Debug, Eq, PartialEq)]
88pub struct OpenApiConfig {
89    /// OpenAPI 3.1.0 document to serve.
90    pub(crate) spec: OpenApiSpec,
91    /// Endpoint path serving JSON spec (default `"/openapi.json"`).
92    pub(crate) spec_path: String,
93    /// Optional endpoint path serving the self-contained HTML explorer (default `Some("/docs")`).
94    pub(crate) docs_path: Option<String>,
95    /// `Cache-Control` header emitted on spec and docs responses.
96    pub(crate) cache_control: String,
97}
98
99/// Pre-serialized zero-allocation OpenAPI server backing [`OpenapiHook`].
100#[derive(Clone, Debug)]
101pub struct OpenApiPlugin {
102    /// Retained configuration.
103    pub(crate) config: OpenApiConfig,
104    /// Strong ETag computed over the pre-serialized JSON document.
105    pub(crate) spec_etag: String,
106    /// Prebuilt `200 OK` HTTP wire response for `/openapi.json`.
107    pub(crate) spec_wire: Vec<u8>,
108    /// Prebuilt `304 Not Modified` HTTP wire response for `/openapi.json`.
109    pub(crate) spec_not_modified_wire: Vec<u8>,
110    /// Prebuilt `200 OK` HTTP wire response for `/docs`.
111    pub(crate) docs_wire: Vec<u8>,
112}
113
114/// Opt-in request middleware serving `/openapi.json` and `/docs` with zero per-request allocations.
115#[derive(Clone, Copy, Debug, Default)]
116#[hyperlane::request_middleware(-52)]
117pub struct OpenapiHook;