1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
use super::super::extractor::ExtractorKind;
use super::super::openapi::{schema_to_fields, param_type_to_openapi_schema};
use indexmap::IndexMap;
use openapiv3 as oa;
use openapiv3::{
Operation, Parameter, ParameterData, ReferenceOr, Response,
QueryStyle, StatusCode,
};
/// Converter for building OpenAPI operations from extractor metadata.
///
/// This struct handles the conversion of Axum extractor information
/// into OpenAPI parameter and request body definitions.
pub struct OpenApiConverter {
/// The OpenAPI operation being built.
operation: Operation,
}
impl OpenApiConverter {
/// Creates a new empty converter.
///
/// # Returns
///
/// A new `OpenApiConverter` with an empty operation.
pub fn new() -> Self {
Self {
operation: Operation::default(),
}
}
/// Processes an extractor kind and adds appropriate OpenAPI definitions.
///
/// This method analyzes the extractor type and adds corresponding
/// OpenAPI parameter or request body definitions to the operation.
///
/// # Arguments
///
/// * `extractor_kind` - The extractor metadata to process
///
/// # Extractor Handling
///
/// - `Path` → Adds path parameters
/// - `Query` → Adds query parameters
/// - `Json` → Adds request body schema
/// - `State` → Ignored (not part of OpenAPI spec)
pub fn process_extractor_kind(&mut self, extractor_kind: ExtractorKind) {
match extractor_kind {
ExtractorKind::Path(schema) => {
for field in schema_to_fields(&schema) {
let path_parameter = Parameter::Path {
parameter_data: ParameterData {
name: field.name,
description: None,
deprecated: None,
required: field.is_required,
format: param_type_to_openapi_schema(&field.kind, &field.format),
example: None,
examples: IndexMap::new(),
explode: None,
extensions: IndexMap::new(),
},
style: Default::default(),
};
self.operation.parameters.push(ReferenceOr::Item(path_parameter));
}
},
ExtractorKind::Json(json_schema) => {
let mut properties = IndexMap::new();
let mut required = Vec::new();
for field in schema_to_fields(&json_schema) {
let field_schema_or_content = param_type_to_openapi_schema(&field.kind, &field.format);
if let oa::ParameterSchemaOrContent::Schema(s) = field_schema_or_content {
let boxed_schema = match s {
oa::ReferenceOr::Reference { reference } => {
oa::ReferenceOr::Reference { reference }
}
oa::ReferenceOr::Item(item) => {
oa::ReferenceOr::Item(Box::new(item))
}
};
properties.insert(field.name.clone(), boxed_schema);
if field.is_required {
required.push(field.name);
}
}
}
let body_schema = oa::Schema {
schema_data: oa::SchemaData::default(),
schema_kind: oa::SchemaKind::Type(oa::Type::Object(oa::ObjectType {
properties,
required,
additional_properties: None,
min_properties: None,
max_properties: None,
})),
};
let media_type = oa::MediaType {
schema: Some(oa::ReferenceOr::Item(body_schema)),
example: None,
examples: IndexMap::new(),
encoding: IndexMap::new(),
extensions: IndexMap::new(),
};
let mut content = IndexMap::new();
content.insert("application/json".to_string(), media_type);
let request_body = oa::RequestBody {
content,
description: Some("JSON payload".to_string()),
required: true,
extensions: IndexMap::new(),
};
self.operation.request_body = Some(oa::ReferenceOr::Item(request_body));
},
ExtractorKind::Query(schema) => {
for field in schema_to_fields(&schema) {
let query_parameter = oa::Parameter::Query {
parameter_data: oa::ParameterData {
name: field.name,
description: None,
required: field.is_required,
deprecated: None,
format: param_type_to_openapi_schema(&field.kind, &field.format),
example: None,
examples: IndexMap::new(),
explode: None,
extensions: IndexMap::new(),
},
style: QueryStyle::Form,
allow_reserved: false,
allow_empty_value: None,
};
self.operation.parameters.push(oa::ReferenceOr::Item(query_parameter));
}
},
ExtractorKind::State(_) => ()
}
}
/// Builds an OpenAPI responses object from a map of status codes to responses.
///
/// # Arguments
///
/// * `responses` - Map of status codes to response definitions
///
/// # Returns
///
/// An OpenAPI `Responses` object ready to be attached to an operation.
pub fn build_responses(responses: IndexMap<StatusCode, ReferenceOr<Response>>) -> oa::Responses {
oa::Responses {
default: None,
responses,
extensions: IndexMap::new(),
}
}
/// Converts a schemars schema into an OpenAPI object schema.
///
/// This method is used to convert response type schemas into
/// OpenAPI format for documentation.
///
/// # Arguments
///
/// * `js_schema` - The schemars schema to convert
///
/// # Returns
///
/// An OpenAPI schema object representing the response structure.
pub fn convert_schemars_to_oa(js_schema: &schemars::Schema) -> oa::Schema {
let fields = schema_to_fields(js_schema);
let mut properties = indexmap::IndexMap::new();
let mut required = Vec::new();
for field in fields {
let field_oa = param_type_to_openapi_schema(&field.kind, &field.format);
if let oa::ParameterSchemaOrContent::Schema(s) = field_oa {
let boxed = match s {
oa::ReferenceOr::Reference { reference } => oa::ReferenceOr::Reference { reference },
oa::ReferenceOr::Item(i) => oa::ReferenceOr::Item(Box::new(i)),
};
properties.insert(field.name.clone(), boxed);
if field.is_required {
required.push(field.name);
}
}
}
oa::Schema {
schema_data: oa::SchemaData::default(),
schema_kind: oa::SchemaKind::Type(oa::Type::Object(oa::ObjectType {
properties,
required,
additional_properties: None,
min_properties: None,
max_properties: None,
})),
}
}
/// Consumes the converter and returns the built operation.
///
/// # Returns
///
/// The completed OpenAPI operation with all parameters and
/// request body definitions.
pub fn into_operation(self) -> Operation {
self.operation
}
}