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

Add new option --specified-operations #100

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

Expand All @@ -40,9 +41,14 @@ impl Api {
}

for (method, op) in path_item {
if let Some((res_path, op)) =
Operation::from_openapi(&path, method, op, component_schemas, include_mode)
{
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);
}
Expand Down Expand Up @@ -186,6 +192,7 @@ impl Operation {
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
Expand All @@ -199,6 +206,7 @@ impl Operation {
IncludeMode::OnlyPublic => !x_hidden,
IncludeMode::PublicAndHidden => true,
IncludeMode::OnlyHidden => x_hidden,
IncludeMode::Specified => specified_operations.contains(&op_id),
};
if !include_operation {
return None;
Expand Down
24 changes: 21 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{
collections::BTreeSet,
io::{self, BufWriter, Write as _},
path::PathBuf,
};
Expand Down Expand Up @@ -43,6 +44,12 @@ enum Command {

#[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>,
},
}

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

fn main() -> anyhow::Result<()> {
Expand All @@ -82,15 +91,17 @@ fn main() -> anyhow::Result<()> {
input_file,
output_dir,
flags,
specified_operations,
} = args.command;
let specified_operations = BTreeSet::from_iter(specified_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)?;
analyze_and_generate(spec, template.into(), path, flags, specified_operations)?;
}
None => {
let output_dir_root = PathBuf::from("out");
Expand All @@ -112,7 +123,7 @@ fn main() -> anyhow::Result<()> {
.path()
.try_into()
.context("non-UTF8 tempdir path")?;
analyze_and_generate(spec, template.into(), path, flags)?;
analyze_and_generate(spec, template.into(), path, flags, specified_operations)?;
// Persist the TempDir if everything was successful
_ = output_dir.into_path();
}
Expand All @@ -126,11 +137,18 @@ fn analyze_and_generate(
template: String,
path: &Utf8Path,
flags: GenerateFlags,
specified_operations: BTreeSet<String>,
) -> anyhow::Result<()> {
let webhooks = get_webhooks(&spec);
let mut components = spec.components.unwrap_or_default();
if let Some(paths) = spec.paths {
let api = Api::new(paths, &components.schemas, flags.include_mode).unwrap();
let api = Api::new(
paths,
&components.schemas,
flags.include_mode,
specified_operations,
)
.unwrap();
let types = api.types(&mut components.schemas, webhooks);

if flags.debug {
Expand Down
2 changes: 1 addition & 1 deletion src/postprocessing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ fn execute_command(
bail!("`{command}` failed with exit code {:?}", exit_status.code());
}
Err(e) if e.kind() == io::ErrorKind::NotFound => {
bail!("`{command}` not found - run with --no-postprocessing to skip");
bail!("`{command}` not found - run with --no-postprocess to skip");
}
Err(e) => Err(e.into()),
}
Expand Down