Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instead of an ever growing list of included operations #101

Merged
merged 1 commit into from
Mar 17, 2025
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: 4 additions & 5 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl Api {
paths: openapi::Paths,
component_schemas: &IndexMap<String, openapi::SchemaObject>,
include_mode: IncludeMode,
specified_operations: BTreeSet<String>,
excluded_operations: BTreeSet<String>,
) -> anyhow::Result<Self> {
let mut resources = BTreeMap::new();

Expand All @@ -47,7 +47,7 @@ impl Api {
op,
component_schemas,
include_mode,
&specified_operations,
&excluded_operations,
) {
let resource = get_or_insert_resource(&mut resources, res_path);
resource.operations.push(op);
Expand Down Expand Up @@ -192,7 +192,7 @@ impl Operation {
op: openapi::Operation,
component_schemas: &IndexMap<String, aide::openapi::SchemaObject>,
include_mode: IncludeMode,
specified_operations: &BTreeSet<String>,
excluded_operations: &BTreeSet<String>,
) -> Option<(Vec<String>, Self)> {
let Some(op_id) = op.operation_id else {
// ignore operations without an operationId
Expand All @@ -206,9 +206,8 @@ impl Operation {
IncludeMode::OnlyPublic => !x_hidden,
IncludeMode::PublicAndHidden => true,
IncludeMode::OnlyHidden => x_hidden,
IncludeMode::Specified => specified_operations.contains(&op_id),
};
if !include_operation {
if !include_operation || excluded_operations.contains(&op_id) {
return None;
}

Expand Down
24 changes: 10 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,15 @@ enum Command {
input_file: String,

/// Path to the output directory.
#[clap(long)]
#[clap(short, long)]
output_dir: Option<Utf8PathBuf>,

#[clap(flatten)]
flags: GenerateFlags,

/// The specified operations for --include-mode=specified
///
/// This expects the operation ID, for example v1.message.create
#[clap(long)]
specified_operations: Vec<String>,
/// Ignore a specified operation id
#[clap(short, long = "exclude-op-id")]
excluded_operations: Vec<String>,
},
}

Expand Down Expand Up @@ -78,8 +76,6 @@ enum IncludeMode {
PublicAndHidden,
/// Only operations marked with `x-hidden`
OnlyHidden,
/// Only include operations specified in `--specified-operations`
Specified,
}

fn main() -> anyhow::Result<()> {
Expand All @@ -91,17 +87,17 @@ fn main() -> anyhow::Result<()> {
input_file,
output_dir,
flags,
specified_operations,
excluded_operations,
} = args.command;
let specified_operations = BTreeSet::from_iter(specified_operations);
let excluded_operations = BTreeSet::from_iter(excluded_operations);

let spec = fs::read_to_string(&input_file)?;

let spec: OpenApi = serde_json::from_str(&spec).context("failed to parse OpenAPI spec")?;

match &output_dir {
Some(path) => {
analyze_and_generate(spec, template.into(), path, flags, specified_operations)?;
analyze_and_generate(spec, template.into(), path, flags, excluded_operations)?;
}
None => {
let output_dir_root = PathBuf::from("out");
Expand All @@ -123,7 +119,7 @@ fn main() -> anyhow::Result<()> {
.path()
.try_into()
.context("non-UTF8 tempdir path")?;
analyze_and_generate(spec, template.into(), path, flags, specified_operations)?;
analyze_and_generate(spec, template.into(), path, flags, excluded_operations)?;
// Persist the TempDir if everything was successful
_ = output_dir.into_path();
}
Expand All @@ -137,7 +133,7 @@ fn analyze_and_generate(
template: String,
path: &Utf8Path,
flags: GenerateFlags,
specified_operations: BTreeSet<String>,
excluded_operations: BTreeSet<String>,
) -> anyhow::Result<()> {
let webhooks = get_webhooks(&spec);
let mut components = spec.components.unwrap_or_default();
Expand All @@ -146,7 +142,7 @@ fn analyze_and_generate(
paths,
&components.schemas,
flags.include_mode,
specified_operations,
excluded_operations,
)
.unwrap();
let types = api.types(&mut components.schemas, webhooks);
Expand Down