diff --git a/src/api/mod.rs b/src/api/mod.rs index 979cf97..8790e85 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -27,7 +27,7 @@ pub struct Api { impl Api { pub fn new( paths: openapi::Paths, - components: &mut openapi::Components, + components: openapi::Components, webhooks: &[String], include_mode: IncludeMode, excluded_operations: &BTreeSet, @@ -39,12 +39,7 @@ impl Api { excluded_operations, specified_operations, )?; - let types = types::from_referenced_components( - &resources, - &mut components.schemas, - webhooks, - include_mode, - ); + let types = types::from_referenced_components(&resources, components.schemas, webhooks); Ok(Self { resources, types }) } diff --git a/src/api/types.rs b/src/api/types.rs index 4daebec..f708943 100644 --- a/src/api/types.rs +++ b/src/api/types.rs @@ -9,7 +9,7 @@ use anyhow::{Context as _, bail, ensure}; use indexmap::IndexMap; use serde::{Deserialize, Serialize}; -use crate::{JsonValue, cli_v1::IncludeMode, utils::get_properties}; +use crate::{JsonValue, utils::get_properties}; use super::{ get_schema_name, @@ -23,17 +23,12 @@ pub type Types = BTreeMap; pub(crate) fn from_referenced_components( res: &Resources, - schemas: &mut IndexMap, + mut schemas: IndexMap, webhooks: &[String], - include_mode: IncludeMode, ) -> Types { - let mut referenced_components: Vec<&str> = match include_mode { - IncludeMode::Public | IncludeMode::PublicAndInternal | IncludeMode::Internal => { - webhooks.iter().map(|s| &**s).collect() - } - IncludeMode::OnlySpecified => vec![], - }; - referenced_components.extend(resources::referenced_components(res)); + let components: Vec<&str> = resources::referenced_components(res) + .chain(webhooks.iter().map(String::as_str)) + .collect(); let mut types = BTreeMap::new(); let mut add_type = |schema_name: &str, extra_components: &mut BTreeSet<_>| { @@ -58,10 +53,7 @@ pub(crate) fn from_referenced_components( } }; - let mut extra_components: BTreeSet<_> = referenced_components - .into_iter() - .map(ToOwned::to_owned) - .collect(); + let mut extra_components: BTreeSet<_> = components.into_iter().map(ToOwned::to_owned).collect(); while let Some(c) = extra_components.pop_first() { add_type(&c, &mut extra_components); } diff --git a/src/cli_v1.rs b/src/cli_v1.rs index b594232..5f11fa9 100644 --- a/src/cli_v1.rs +++ b/src/cli_v1.rs @@ -28,14 +28,17 @@ struct CliArgs { #[arg(global = true, long, value_enum, default_value_t = IncludeMode::Public)] include_mode: IncludeMode, - /// Ignore a specified operation id + /// Include webhooks in component models. + #[arg(global = true, long)] + include_webhooks: bool, + + /// Ignore a specified operation id. #[arg(global = true, short, long = "exclude-op-id")] excluded_operations: Vec, - /// Include specified operations + /// Include specified operations. /// /// Use this option, to run the codegen with a limited set of operations. - /// Op webhook models will be excluded from the generation #[arg(global = true, long = "include-op-id")] specified_operations: Vec, @@ -117,10 +120,14 @@ pub fn run_cli_v1_main() -> anyhow::Result<()> { let spec: OpenApi = serde_json::from_str(&input_file_contents) .context("failed to parse OpenAPI spec")?; - let webhooks = get_webhooks(&spec); + let webhooks = if args.include_webhooks { + get_webhooks(&spec) + } else { + vec![] + }; Api::new( spec.paths.context("found no endpoints in input spec")?, - &mut spec.components.unwrap_or_default(), + spec.components.unwrap_or_default(), &webhooks, args.include_mode, &excluded_operations, diff --git a/src/codesamples.rs b/src/codesamples.rs index f2ca0ca..b5786be 100644 --- a/src/codesamples.rs +++ b/src/codesamples.rs @@ -213,7 +213,7 @@ pub async fn generate_codesamples( .paths .clone() .context("found no endpoints in input spec")?, - &mut openapi_spec.components.clone().unwrap_or_default(), + openapi_spec.components.clone().unwrap_or_default(), &[], IncludeMode::Public, &excluded_operation_ids,