-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.rs
502 lines (455 loc) · 17.7 KB
/
api.rs
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
use std::collections::{BTreeMap, BTreeSet};
use aide::openapi::{self, ReferenceOr};
use anyhow::{bail, Context as _};
use indexmap::IndexMap;
use schemars::schema::{InstanceType, Schema};
use serde::Serialize;
use crate::{
types::{FieldType, Types},
util::{get_schema_name, serialize_btree_map_values},
IncludeMode,
};
/// The API we generate a client for.
///
/// Intermediate representation of `paths` from the spec.
#[derive(Debug, Serialize)]
pub(crate) struct Api {
#[serde(serialize_with = "serialize_btree_map_values")]
pub resources: BTreeMap<String, Resource>,
}
impl Api {
pub(crate) fn new(
paths: openapi::Paths,
component_schemas: &IndexMap<String, openapi::SchemaObject>,
include_mode: IncludeMode,
specified_operations: BTreeSet<String>,
) -> anyhow::Result<Self> {
let mut resources = BTreeMap::new();
for (path, pi) in paths {
let path_item = pi
.into_item()
.context("$ref paths are currently not supported")?;
if !path_item.parameters.is_empty() {
tracing::info!("parameters at the path item level are not currently supported");
continue;
}
for (method, op) in path_item {
if let Some((res_path, op)) = Operation::from_openapi(
&path,
method,
op,
component_schemas,
include_mode,
&specified_operations,
) {
let resource = get_or_insert_resource(&mut resources, res_path);
resource.operations.push(op);
}
}
}
Ok(Self { resources })
}
fn referenced_components(&self) -> impl Iterator<Item = &str> {
self.resources
.values()
.flat_map(Resource::referenced_components)
}
pub(crate) fn types(
&self,
schemas: &mut IndexMap<String, openapi::SchemaObject>,
webhooks: Vec<String>,
) -> Types {
let mut referenced_components: Vec<&str> = webhooks.iter().map(|s| &**s).collect();
referenced_components.extend(self.referenced_components());
Types::from_referenced_components(schemas, referenced_components.into_iter())
}
}
fn get_or_insert_resource(
resources: &mut BTreeMap<String, Resource>,
path: Vec<String>,
) -> &mut Resource {
let mut path_iter = path.into_iter();
let mut name = path_iter.next().expect("path must be non-empty");
let mut r = resources
.entry(name.clone())
.or_insert_with(|| Resource::new(name.clone()));
for sub_name in path_iter {
name.push('-');
name.push_str(&sub_name);
r = r
.subresources
.entry(sub_name)
.or_insert_with(|| Resource::new(name.clone()));
}
r
}
/// A named group of [`Operation`]s.
#[derive(Debug, serde::Serialize)]
pub(crate) struct Resource {
pub name: String,
pub operations: Vec<Operation>,
pub subresources: BTreeMap<String, Resource>,
}
impl Resource {
fn new(name: String) -> Self {
Self {
name,
operations: Vec::new(),
subresources: BTreeMap::new(),
}
}
pub(crate) fn referenced_components(&self) -> BTreeSet<&str> {
let mut res = BTreeSet::new();
for resource in self.subresources.values() {
res.extend(resource.referenced_components());
}
for operation in &self.operations {
for param in &operation.query_params {
if let FieldType::SchemaRef(name) = ¶m.r#type {
res.insert(name);
}
}
if let Some(name) = &operation.request_body_schema_name {
res.insert(name);
}
if let Some(name) = &operation.response_body_schema_name {
res.insert(name);
}
}
res
}
}
/// A named HTTP endpoint.
#[derive(Debug, serde::Serialize)]
pub(crate) struct Operation {
/// The operation ID from the spec.
id: String,
/// The name to use for the operation in code.
pub(crate) name: String,
/// Description of the operation to use for documentation.
#[serde(skip_serializing_if = "Option::is_none")]
description: Option<String>,
/// Whether this operation is marked as deprecated.
deprecated: bool,
/// The HTTP method.
///
/// Encoded as "get", "post" or such because that's what aide's PathItem iterator gives us.
method: String,
/// The operation's endpoint path.
path: String,
/// Path parameters.
///
/// Only required string-typed parameters are currently supported.
path_params: Vec<String>,
/// Header parameters.
///
/// Only string-typed parameters are currently supported.
header_params: Vec<HeaderParam>,
/// Query parameters.
query_params: Vec<QueryParam>,
/// Name of the request body type, if any.
#[serde(skip_serializing_if = "Option::is_none")]
request_body_schema_name: Option<String>,
/// Some request bodies are required, but all the fields are optional (i.e. the CLI can omit
/// this from the argument list).
/// Only useful when `request_body_schema_name` is `Some`.
request_body_all_optional: bool,
/// Name of the response body type, if any.
#[serde(skip_serializing_if = "Option::is_none")]
response_body_schema_name: Option<String>,
}
impl Operation {
#[tracing::instrument(
name = "operation_from_openapi",
skip_all,
fields(path = path, method = method, op_id),
)]
fn from_openapi(
path: &str,
method: &str,
op: openapi::Operation,
component_schemas: &IndexMap<String, aide::openapi::SchemaObject>,
include_mode: IncludeMode,
specified_operations: &BTreeSet<String>,
) -> Option<(Vec<String>, Self)> {
let Some(op_id) = op.operation_id else {
// ignore operations without an operationId
return None;
};
tracing::Span::current().record("op_id", &op_id);
// verbose, but very easy to understand
let x_hidden = op.extensions.get("x-hidden").is_some_and(|val| val == true);
let include_operation = match include_mode {
IncludeMode::OnlyPublic => !x_hidden,
IncludeMode::PublicAndHidden => true,
IncludeMode::OnlyHidden => x_hidden,
IncludeMode::Specified => specified_operations.contains(&op_id),
};
if !include_operation {
return None;
}
let mut op_id_parts_iter = op_id.split('.');
let version = op_id_parts_iter
.next()
.expect("split iter always contains at least one item");
let Some(op_name) = op_id_parts_iter.next_back() else {
tracing::debug!("skipping operation whose ID doesn't contain a period");
return None;
};
let res_path: Vec<_> = op_id_parts_iter.map(ToOwned::to_owned).collect();
if res_path.is_empty() {
tracing::debug!("skipping operation whose ID only contains one period");
return None;
}
if version != "v1" {
tracing::warn!("found operation whose ID does not begin with v1");
return None;
}
let mut path_params = Vec::new();
let mut query_params = Vec::new();
let mut header_params = Vec::new();
for param in op.parameters {
match param {
ReferenceOr::Reference { .. } => {
tracing::warn!("$ref parameters are not currently supported");
return None;
}
ReferenceOr::Item(openapi::Parameter::Path {
parameter_data,
style: openapi::PathStyle::Simple,
}) => {
assert!(parameter_data.required, "no optional path params");
if let Err(e) = enforce_string_parameter(¶meter_data) {
tracing::warn!("unsupported path parameter: {e}");
return None;
}
path_params.push(parameter_data.name);
}
ReferenceOr::Item(openapi::Parameter::Header {
parameter_data,
style: openapi::HeaderStyle::Simple,
}) => {
if parameter_data.name != "idempotency-key" {
tracing::warn!(name = parameter_data.name, "unknown header parameter");
}
if let Err(e) = enforce_string_parameter(¶meter_data) {
tracing::warn!("unsupported header parameter: {e}");
return None;
}
header_params.push(HeaderParam {
name: parameter_data.name,
required: parameter_data.required,
});
}
ReferenceOr::Item(openapi::Parameter::Query {
parameter_data,
allow_reserved: false,
style: openapi::QueryStyle::Form,
allow_empty_value: None,
}) => {
let name = parameter_data.name;
if method == "post" && name == "get_if_exists" {
tracing::debug!("ignoring get_if_exists query parameter");
continue;
}
let _guard = tracing::info_span!("field_type_from_openapi", name).entered();
let r#type = match FieldType::from_openapi(parameter_data.format) {
Ok(t) => t,
Err(e) => {
tracing::warn!("unsupported query parameter type: {e}");
return None;
}
};
query_params.push(QueryParam {
name,
description: parameter_data.description,
required: parameter_data.required,
r#type,
});
}
ReferenceOr::Item(parameter) => {
tracing::warn!(
?parameter,
"this kind of parameter is not currently supported"
);
return None;
}
}
}
let request_body_all_optional = op
.request_body
.as_ref()
.map(|r| {
match r {
ReferenceOr::Reference { .. } => {
unimplemented!("reference")
}
ReferenceOr::Item(body) => {
if let Some(mt) = body.content.get("application/json") {
match mt.schema.as_ref().map(|so| &so.json_schema) {
Some(Schema::Object(schemars::schema::SchemaObject {
object: Some(ov),
..
})) => {
return ov.required.is_empty();
}
Some(Schema::Object(schemars::schema::SchemaObject {
reference: Some(s),
..
})) => {
match component_schemas
.get(
&get_schema_name(Some(s)).expect("schema should exist"),
)
.map(|so| &so.json_schema)
{
Some(Schema::Object(schemars::schema::SchemaObject {
object: Some(ov),
..
})) => {
return ov.required.is_empty();
}
_ => unimplemented!("double ref not supported"),
}
}
_ => {}
}
}
}
}
false
})
.unwrap_or_default();
let request_body_schema_name = op.request_body.and_then(|b| match b {
ReferenceOr::Item(mut req_body) => {
assert!(req_body.required);
assert!(req_body.extensions.is_empty());
assert_eq!(req_body.content.len(), 1);
let json_body = req_body
.content
.swap_remove("application/json")
.expect("should have JSON body");
assert!(json_body.extensions.is_empty());
match json_body.schema.expect("no json body schema?!").json_schema {
Schema::Bool(_) => {
tracing::error!("unexpected bool schema");
None
}
Schema::Object(obj) => {
if !obj.is_ref() {
tracing::error!(?obj, "unexpected non-$ref json body schema");
}
get_schema_name(obj.reference.as_deref())
}
}
}
ReferenceOr::Reference { .. } => {
tracing::error!("$ref request bodies are not currently supported");
None
}
});
let response_body_schema_name = op.responses.and_then(|r| {
assert_eq!(r.default, None);
assert!(r.extensions.is_empty());
let mut success_responses = r.responses.into_iter().filter(|(st, _)| {
match st {
openapi::StatusCode::Code(c) => match c {
0..100 => tracing::error!("invalid status code < 100"),
100..200 => tracing::error!("what is this? status code {c}..."),
200..300 => return true,
300..400 => tracing::error!("what is this? status code {c}..."),
400.. => {}
},
openapi::StatusCode::Range(_) => {
tracing::error!("unsupported status code range");
}
}
false
});
let (_, resp) = success_responses
.next()
.expect("every operation must have one success response");
let schema_name = response_body_schema_name(resp);
for (_, resp) in success_responses {
assert_eq!(schema_name, response_body_schema_name(resp));
}
schema_name
});
let op_name = op_name.to_owned();
let op = Operation {
id: op_id,
name: op_name,
description: op.description,
deprecated: op.deprecated,
method: method.to_owned(),
path: path.to_owned(),
path_params,
header_params,
query_params,
request_body_schema_name,
request_body_all_optional,
response_body_schema_name,
};
Some((res_path, op))
}
pub(crate) fn has_query_or_header_params(&self) -> bool {
!self.header_params.is_empty() || !self.query_params.is_empty()
}
}
fn enforce_string_parameter(parameter_data: &openapi::ParameterData) -> anyhow::Result<()> {
let openapi::ParameterSchemaOrContent::Schema(s) = ¶meter_data.format else {
bail!("found unexpected 'content' data format");
};
let Schema::Object(obj) = &s.json_schema else {
bail!("found unexpected `true` schema");
};
if obj.instance_type != Some(InstanceType::String.into()) {
bail!("unsupported path parameter type `{:?}`", obj.instance_type);
}
Ok(())
}
fn response_body_schema_name(resp: ReferenceOr<openapi::Response>) -> Option<String> {
match resp {
ReferenceOr::Item(mut resp_body) => {
assert!(resp_body.extensions.is_empty());
if resp_body.content.is_empty() {
return None;
}
assert_eq!(resp_body.content.len(), 1);
let json_body = resp_body
.content
.swap_remove("application/json")
.expect("should have JSON body");
assert!(json_body.extensions.is_empty());
match json_body.schema.expect("no json body schema?!").json_schema {
Schema::Bool(_) => {
tracing::error!("unexpected bool schema");
None
}
Schema::Object(obj) => {
if !obj.is_ref() {
tracing::error!(?obj, "unexpected non-$ref json body schema");
}
get_schema_name(obj.reference.as_deref())
}
}
}
ReferenceOr::Reference { .. } => {
tracing::error!("$ref response bodies are not currently supported");
None
}
}
}
#[derive(Debug, serde::Serialize)]
struct HeaderParam {
name: String,
required: bool,
}
#[derive(Debug, serde::Serialize)]
struct QueryParam {
name: String,
#[serde(skip_serializing_if = "Option::is_none")]
description: Option<String>,
required: bool,
r#type: FieldType,
}