Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
Expand All @@ -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 })
}
Expand Down
20 changes: 6 additions & 14 deletions src/api/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -23,17 +23,12 @@ pub type Types = BTreeMap<String, Type>;

pub(crate) fn from_referenced_components(
res: &Resources,
schemas: &mut IndexMap<String, openapi::SchemaObject>,
mut schemas: IndexMap<String, openapi::SchemaObject>,
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<_>| {
Expand All @@ -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);
}
Expand Down
17 changes: 12 additions & 5 deletions src/cli_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,

/// 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<String>,

Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/codesamples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading