progenitor_impl/lib.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 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
// Copyright 2024 Oxide Computer Company
//! Core implementation for the progenitor OpenAPI client generator.
#![deny(missing_docs)]
use std::collections::{BTreeMap, HashMap, HashSet};
use openapiv3::OpenAPI;
use proc_macro2::TokenStream;
use quote::quote;
use serde::Deserialize;
use thiserror::Error;
use typify::{TypeSpace, TypeSpaceSettings};
use crate::to_schema::ToSchema;
pub use typify::CrateVers;
pub use typify::TypeSpaceImpl as TypeImpl;
pub use typify::TypeSpacePatch as TypePatch;
pub use typify::UnknownPolicy;
mod cli;
mod httpmock;
mod method;
mod template;
mod to_schema;
mod util;
#[allow(missing_docs)]
#[derive(Error, Debug)]
pub enum Error {
#[error("unexpected value type {0}: {1}")]
BadValue(String, serde_json::Value),
#[error("type error {0}")]
TypeError(#[from] typify::Error),
#[error("unexpected or unhandled format in the OpenAPI document {0}")]
UnexpectedFormat(String),
#[error("invalid operation path {0}")]
InvalidPath(String),
#[error("invalid dropshot extension use: {0}")]
InvalidExtension(String),
#[error("internal error {0}")]
InternalError(String),
}
#[allow(missing_docs)]
pub type Result<T> = std::result::Result<T, Error>;
/// OpenAPI generator.
pub struct Generator {
type_space: TypeSpace,
settings: GenerationSettings,
uses_futures: bool,
uses_websockets: bool,
}
/// Settings for [Generator].
#[derive(Default, Clone)]
pub struct GenerationSettings {
interface: InterfaceStyle,
tag: TagStyle,
inner_type: Option<TokenStream>,
pre_hook: Option<TokenStream>,
pre_hook_async: Option<TokenStream>,
post_hook: Option<TokenStream>,
post_hook_async: Option<TokenStream>,
extra_derives: Vec<String>,
map_type: Option<String>,
unknown_crates: UnknownPolicy,
crates: BTreeMap<String, CrateSpec>,
patch: HashMap<String, TypePatch>,
replace: HashMap<String, (String, Vec<TypeImpl>)>,
convert: Vec<(schemars::schema::SchemaObject, String, Vec<TypeImpl>)>,
}
#[derive(Debug, Clone)]
struct CrateSpec {
version: CrateVers,
rename: Option<String>,
}
/// Style of generated client.
#[derive(Clone, Deserialize, PartialEq, Eq)]
pub enum InterfaceStyle {
/// Use positional style.
Positional,
/// Use builder style.
Builder,
}
impl Default for InterfaceStyle {
fn default() -> Self {
Self::Positional
}
}
/// Style for using the OpenAPI tags when generating names in the client.
#[derive(Clone, Deserialize)]
pub enum TagStyle {
/// Merge tags to create names in the generated client.
Merged,
/// Use each tag name to create separate names in the generated client.
Separate,
}
impl Default for TagStyle {
fn default() -> Self {
Self::Merged
}
}
impl GenerationSettings {
/// Create new generator settings with default values.
pub fn new() -> Self {
Self::default()
}
/// Set the [InterfaceStyle].
pub fn with_interface(&mut self, interface: InterfaceStyle) -> &mut Self {
self.interface = interface;
self
}
/// Set the [TagStyle].
pub fn with_tag(&mut self, tag: TagStyle) -> &mut Self {
self.tag = tag;
self
}
/// Client inner type available to pre and post hooks.
pub fn with_inner_type(&mut self, inner_type: TokenStream) -> &mut Self {
self.inner_type = Some(inner_type);
self
}
/// Hook invoked before issuing the HTTP request.
pub fn with_pre_hook(&mut self, pre_hook: TokenStream) -> &mut Self {
self.pre_hook = Some(pre_hook);
self
}
/// Hook invoked before issuing the HTTP request.
pub fn with_pre_hook_async(&mut self, pre_hook: TokenStream) -> &mut Self {
self.pre_hook_async = Some(pre_hook);
self
}
/// Hook invoked prior to receiving the HTTP response.
pub fn with_post_hook(&mut self, post_hook: TokenStream) -> &mut Self {
self.post_hook = Some(post_hook);
self
}
/// Hook invoked prior to receiving the HTTP response.
pub fn with_post_hook_async(&mut self, post_hook: TokenStream) -> &mut Self {
self.post_hook_async = Some(post_hook);
self
}
/// Additional derive macros applied to generated types.
pub fn with_derive(&mut self, derive: impl ToString) -> &mut Self {
self.extra_derives.push(derive.to_string());
self
}
/// Modify a type with the given name.
/// See [typify::TypeSpaceSettings::with_patch].
pub fn with_patch<S: AsRef<str>>(&mut self, type_name: S, patch: &TypePatch) -> &mut Self {
self.patch
.insert(type_name.as_ref().to_string(), patch.clone());
self
}
/// Replace a referenced type with a named type.
/// See [typify::TypeSpaceSettings::with_replacement].
pub fn with_replacement<TS: ToString, RS: ToString, I: Iterator<Item = TypeImpl>>(
&mut self,
type_name: TS,
replace_name: RS,
impls: I,
) -> &mut Self {
self.replace.insert(
type_name.to_string(),
(replace_name.to_string(), impls.collect()),
);
self
}
/// Replace a given schema with a named type.
/// See [typify::TypeSpaceSettings::with_conversion].
pub fn with_conversion<S: ToString, I: Iterator<Item = TypeImpl>>(
&mut self,
schema: schemars::schema::SchemaObject,
type_name: S,
impls: I,
) -> &mut Self {
self.convert
.push((schema, type_name.to_string(), impls.collect()));
self
}
/// Policy regarding crates referenced by the schema extension
/// `x-rust-type` not explicitly specified via [Self::with_crate].
/// See [typify::TypeSpaceSettings::with_unknown_crates].
pub fn with_unknown_crates(&mut self, policy: UnknownPolicy) -> &mut Self {
self.unknown_crates = policy;
self
}
/// Explicitly named crates whose types may be used during generation
/// rather than generating new types based on their schemas (base on the
/// presence of the x-rust-type extension).
/// See [typify::TypeSpaceSettings::with_crate].
pub fn with_crate<S1: ToString>(
&mut self,
crate_name: S1,
version: CrateVers,
rename: Option<&String>,
) -> &mut Self {
self.crates.insert(
crate_name.to_string(),
CrateSpec {
version,
rename: rename.cloned(),
},
);
self
}
/// Set the type used for key-value maps. Common examples:
/// - [`std::collections::HashMap`] - **Default**
/// - [`std::collections::BTreeMap`]
/// - [`indexmap::IndexMap`]
///
/// The requiremnets for a map type can be found in the
/// [typify::TypeSpaceSettings::with_map_type] documentation.
pub fn with_map_type<MT: ToString>(&mut self, map_type: MT) -> &mut Self {
self.map_type = Some(map_type.to_string());
self
}
}
impl Default for Generator {
fn default() -> Self {
Self {
type_space: TypeSpace::new(TypeSpaceSettings::default().with_type_mod("types")),
settings: Default::default(),
uses_futures: Default::default(),
uses_websockets: Default::default(),
}
}
}
impl Generator {
/// Create a new generator with default values.
pub fn new(settings: &GenerationSettings) -> Self {
let mut type_settings = TypeSpaceSettings::default();
type_settings
.with_type_mod("types")
.with_struct_builder(settings.interface == InterfaceStyle::Builder);
settings.extra_derives.iter().for_each(|derive| {
let _ = type_settings.with_derive(derive.clone());
});
// Control use of crates found in x-rust-type extension
type_settings.with_unknown_crates(settings.unknown_crates);
settings
.crates
.iter()
.for_each(|(crate_name, CrateSpec { version, rename })| {
type_settings.with_crate(crate_name, version.clone(), rename.as_ref());
});
// Adjust generation by type, name, or schema.
settings.patch.iter().for_each(|(type_name, patch)| {
type_settings.with_patch(type_name, patch);
});
settings
.replace
.iter()
.for_each(|(type_name, (replace_name, impls))| {
type_settings.with_replacement(type_name, replace_name, impls.iter().cloned());
});
settings
.convert
.iter()
.for_each(|(schema, type_name, impls)| {
type_settings.with_conversion(schema.clone(), type_name, impls.iter().cloned());
});
// Set the map type if specified.
if let Some(map_type) = &settings.map_type {
type_settings.with_map_type(map_type.clone());
}
Self {
type_space: TypeSpace::new(&type_settings),
settings: settings.clone(),
uses_futures: false,
uses_websockets: false,
}
}
/// Emit a [TokenStream] containing the generated client code.
pub fn generate_tokens(&mut self, spec: &OpenAPI) -> Result<TokenStream> {
validate_openapi(spec)?;
// Convert our components dictionary to schemars
let schemas = spec.components.iter().flat_map(|components| {
components
.schemas
.iter()
.map(|(name, ref_or_schema)| (name.clone(), ref_or_schema.to_schema()))
});
self.type_space.add_ref_types(schemas)?;
let raw_methods = spec
.paths
.iter()
.flat_map(|(path, ref_or_item)| {
// Exclude externally defined path items.
let item = ref_or_item.as_item().unwrap();
item.iter().map(move |(method, operation)| {
(path.as_str(), method, operation, &item.parameters)
})
})
.map(|(path, method, operation, path_parameters)| {
self.process_operation(operation, &spec.components, path, method, path_parameters)
})
.collect::<Result<Vec<_>>>()?;
let operation_code = match (&self.settings.interface, &self.settings.tag) {
(InterfaceStyle::Positional, TagStyle::Merged) => self
.generate_tokens_positional_merged(
&raw_methods,
self.settings.inner_type.is_some(),
),
(InterfaceStyle::Positional, TagStyle::Separate) => {
unimplemented!("positional arguments with separate tags are currently unsupported")
}
(InterfaceStyle::Builder, TagStyle::Merged) => self
.generate_tokens_builder_merged(&raw_methods, self.settings.inner_type.is_some()),
(InterfaceStyle::Builder, TagStyle::Separate) => {
let tag_info = spec
.tags
.iter()
.map(|tag| (&tag.name, tag))
.collect::<BTreeMap<_, _>>();
self.generate_tokens_builder_separate(
&raw_methods,
tag_info,
self.settings.inner_type.is_some(),
)
}
}?;
let types = self.type_space.to_stream();
// Generate an implementation of a `Self::as_inner` method, if an inner
// type is defined.
let maybe_inner = self.settings.inner_type.as_ref().map(|inner| {
quote! {
/// Return a reference to the inner type stored in `self`.
pub fn inner(&self) -> &#inner {
&self.inner
}
}
});
let inner_property = self.settings.inner_type.as_ref().map(|inner| {
quote! {
pub (crate) inner: #inner,
}
});
let inner_parameter = self.settings.inner_type.as_ref().map(|inner| {
quote! {
inner: #inner,
}
});
let inner_value = self.settings.inner_type.as_ref().map(|_| {
quote! {
inner
}
});
let client_docstring = {
let mut s = format!("Client for {}", spec.info.title);
if let Some(ss) = &spec.info.description {
s.push_str("\n\n");
s.push_str(ss);
}
if let Some(ss) = &spec.info.terms_of_service {
s.push_str("\n\n");
s.push_str(ss);
}
s.push_str(&format!("\n\nVersion: {}", &spec.info.version));
s
};
let version_str = &spec.info.version;
// The allow(unused_imports) on the `pub use` is necessary with Rust
// 1.76+, in case the generated file is not at the top level of the
// crate.
let file = quote! {
// Re-export ResponseValue and Error since those are used by the
// public interface of Client.
#[allow(unused_imports)]
pub use progenitor_client::{ByteStream, Error, ResponseValue};
#[allow(unused_imports)]
use progenitor_client::{encode_path, RequestBuilderExt};
#[allow(unused_imports)]
use reqwest::header::{HeaderMap, HeaderValue};
/// Types used as operation parameters and responses.
#[allow(clippy::all)]
pub mod types {
#types
}
#[derive(Clone, Debug)]
#[doc = #client_docstring]
pub struct Client {
pub(crate) baseurl: String,
pub(crate) client: reqwest::Client,
#inner_property
}
impl Client {
/// Create a new client.
///
/// `baseurl` is the base URL provided to the internal
/// `reqwest::Client`, and should include a scheme and hostname,
/// as well as port and a path stem if applicable.
pub fn new(
baseurl: &str,
#inner_parameter
) -> Self {
#[cfg(not(target_arch = "wasm32"))]
let client = {
let dur = std::time::Duration::from_secs(15);
reqwest::ClientBuilder::new()
.connect_timeout(dur)
.timeout(dur)
};
#[cfg(target_arch = "wasm32")]
let client = reqwest::ClientBuilder::new();
Self::new_with_client(baseurl, client.build().unwrap(), #inner_value)
}
/// Construct a new client with an existing `reqwest::Client`,
/// allowing more control over its configuration.
///
/// `baseurl` is the base URL provided to the internal
/// `reqwest::Client`, and should include a scheme and hostname,
/// as well as port and a path stem if applicable.
pub fn new_with_client(
baseurl: &str,
client: reqwest::Client,
#inner_parameter
) -> Self {
Self {
baseurl: baseurl.to_string(),
client,
#inner_value
}
}
/// Get the base URL to which requests are made.
pub fn baseurl(&self) -> &String {
&self.baseurl
}
/// Get the internal `reqwest::Client` used to make requests.
pub fn client(&self) -> &reqwest::Client {
&self.client
}
/// Get the version of this API.
///
/// This string is pulled directly from the source OpenAPI
/// document and may be in any format the API selects.
pub fn api_version(&self) -> &'static str {
#version_str
}
#maybe_inner
}
#operation_code
};
Ok(file)
}
fn generate_tokens_positional_merged(
&mut self,
input_methods: &[method::OperationMethod],
has_inner: bool,
) -> Result<TokenStream> {
let methods = input_methods
.iter()
.map(|method| self.positional_method(method, has_inner))
.collect::<Result<Vec<_>>>()?;
// The allow(unused_imports) on the `pub use` is necessary with Rust
// 1.76+, in case the generated file is not at the top level of the
// crate.
let out = quote! {
#[allow(clippy::all)]
#[allow(elided_named_lifetimes)]
impl Client {
#(#methods)*
}
/// Items consumers will typically use such as the Client.
pub mod prelude {
#[allow(unused_imports)]
pub use super::Client;
}
};
Ok(out)
}
fn generate_tokens_builder_merged(
&mut self,
input_methods: &[method::OperationMethod],
has_inner: bool,
) -> Result<TokenStream> {
let builder_struct = input_methods
.iter()
.map(|method| self.builder_struct(method, TagStyle::Merged, has_inner))
.collect::<Result<Vec<_>>>()?;
let builder_methods = input_methods
.iter()
.map(|method| self.builder_impl(method))
.collect::<Vec<_>>();
let out = quote! {
impl Client {
#(#builder_methods)*
}
/// Types for composing operation parameters.
#[allow(clippy::all)]
pub mod builder {
use super::types;
#[allow(unused_imports)]
use super::{
encode_path,
ByteStream,
Error,
HeaderMap,
HeaderValue,
RequestBuilderExt,
ResponseValue,
};
#(#builder_struct)*
}
/// Items consumers will typically use such as the Client.
pub mod prelude {
pub use self::super::Client;
}
};
Ok(out)
}
fn generate_tokens_builder_separate(
&mut self,
input_methods: &[method::OperationMethod],
tag_info: BTreeMap<&String, &openapiv3::Tag>,
has_inner: bool,
) -> Result<TokenStream> {
let builder_struct = input_methods
.iter()
.map(|method| self.builder_struct(method, TagStyle::Separate, has_inner))
.collect::<Result<Vec<_>>>()?;
let (traits_and_impls, trait_preludes) = self.builder_tags(input_methods, &tag_info);
// The allow(unused_imports) on the `pub use` is necessary with Rust
// 1.76+, in case the generated file is not at the top level of the
// crate.
let out = quote! {
#traits_and_impls
/// Types for composing operation parameters.
#[allow(clippy::all)]
pub mod builder {
use super::types;
#[allow(unused_imports)]
use super::{
encode_path,
ByteStream,
Error,
HeaderMap,
HeaderValue,
RequestBuilderExt,
ResponseValue,
};
#(#builder_struct)*
}
/// Items consumers will typically use such as the Client and
/// extension traits.
pub mod prelude {
#[allow(unused_imports)]
pub use super::Client;
#trait_preludes
}
};
Ok(out)
}
/// Get the [TypeSpace] for schemas present in the OpenAPI specification.
pub fn get_type_space(&self) -> &TypeSpace {
&self.type_space
}
/// Whether the generated client needs to use additional crates to support
/// futures.
pub fn uses_futures(&self) -> bool {
self.uses_futures
}
/// Whether the generated client needs to use additional crates to support
/// websockets.
pub fn uses_websockets(&self) -> bool {
self.uses_websockets
}
}
/// Add newlines after end-braces at <= two levels of indentation.
pub fn space_out_items(content: String) -> Result<String> {
Ok(if cfg!(not(windows)) {
let regex = regex::Regex::new(r#"(\n\s*})(\n\s{0,8}[^} ])"#).unwrap();
regex.replace_all(&content, "$1\n$2").to_string()
} else {
let regex = regex::Regex::new(r#"(\n\s*})(\r\n\s{0,8}[^} ])"#).unwrap();
regex.replace_all(&content, "$1\r\n$2").to_string()
})
}
/// Do some very basic checks of the OpenAPI documents.
pub fn validate_openapi(spec: &OpenAPI) -> Result<()> {
match spec.openapi.as_str() {
"3.0.0" | "3.0.1" | "3.0.2" | "3.0.3" => (),
v => return Err(Error::UnexpectedFormat(format!("invalid version: {}", v))),
}
let mut opids = HashSet::new();
spec.paths.paths.iter().try_for_each(|p| {
match p.1 {
openapiv3::ReferenceOr::Reference { reference: _ } => Err(Error::UnexpectedFormat(
format!("path {} uses reference, unsupported", p.0,),
)),
openapiv3::ReferenceOr::Item(item) => {
// Make sure every operation has an operation ID, and that each
// operation ID is only used once in the document.
item.iter().try_for_each(|(_, o)| {
if let Some(oid) = o.operation_id.as_ref() {
if !opids.insert(oid.to_string()) {
return Err(Error::UnexpectedFormat(format!(
"duplicate operation ID: {}",
oid,
)));
}
} else {
return Err(Error::UnexpectedFormat(format!(
"path {} is missing operation ID",
p.0,
)));
}
Ok(())
})
}
}
})?;
Ok(())
}
#[cfg(test)]
mod tests {
use serde_json::json;
use crate::Error;
#[test]
fn test_bad_value() {
assert_eq!(
Error::BadValue("nope".to_string(), json! { "nope"},).to_string(),
"unexpected value type nope: \"nope\"",
);
}
#[test]
fn test_type_error() {
assert_eq!(
Error::UnexpectedFormat("nope".to_string()).to_string(),
"unexpected or unhandled format in the OpenAPI document nope",
);
}
#[test]
fn test_invalid_path() {
assert_eq!(
Error::InvalidPath("nope".to_string()).to_string(),
"invalid operation path nope",
);
}
#[test]
fn test_internal_error() {
assert_eq!(
Error::InternalError("nope".to_string()).to_string(),
"internal error nope",
);
}
}