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 include-mode to select which operations to include #99

Merged
merged 1 commit into from
Mar 13, 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
16 changes: 12 additions & 4 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use serde::Serialize;
use crate::{
types::{FieldType, Types},
util::{get_schema_name, serialize_btree_map_values},
IncludeMode,
};

/// The API we generate a client for.
Expand All @@ -24,7 +25,7 @@ impl Api {
pub(crate) fn new(
paths: openapi::Paths,
component_schemas: &IndexMap<String, openapi::SchemaObject>,
include_hidden: bool,
include_mode: IncludeMode,
) -> anyhow::Result<Self> {
let mut resources = BTreeMap::new();

Expand All @@ -40,7 +41,7 @@ impl Api {

for (method, op) in path_item {
if let Some((res_path, op)) =
Operation::from_openapi(&path, method, op, component_schemas, include_hidden)
Operation::from_openapi(&path, method, op, component_schemas, include_mode)
{
let resource = get_or_insert_resource(&mut resources, res_path);
resource.operations.push(op);
Expand Down Expand Up @@ -184,15 +185,22 @@ impl Operation {
method: &str,
op: openapi::Operation,
component_schemas: &IndexMap<String, aide::openapi::SchemaObject>,
include_hidden: bool,
include_mode: IncludeMode,
) -> Option<(Vec<String>, Self)> {
let Some(op_id) = op.operation_id else {
// ignore operations without an operationId
return None;
};
tracing::Span::current().record("op_id", &op_id);

if !include_hidden && op.extensions.get("x-hidden").is_some_and(|val| val == true) {
// verbose, but very easy to understand
let x_hidden = op.extensions.get("x-hidden").is_some_and(|val| val == true);
let include_operation = match include_mode {
IncludeMode::OnlyPublic => !x_hidden,
IncludeMode::PublicAndHidden => true,
IncludeMode::OnlyHidden => x_hidden,
};
if !include_operation {
return None;
}

Expand Down
19 changes: 15 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,26 @@ struct GenerateFlags {
#[clap(long)]
no_postprocess: bool,

/// Include operations in the output that are marked `"x-hidden": true`.
#[clap(long)]
include_hidden: bool,
/// Which operations to include
#[clap(long, value_enum, default_value_t=IncludeMode::OnlyPublic)]
include_mode: IncludeMode,

/// Write api.ron and types.ron files, as a debugging aid.
#[clap(long)]
debug: bool,
}

#[derive(Copy, Clone, clap::ValueEnum)]
#[clap(rename_all = "kebab-case")]
enum IncludeMode {
/// Only public options
OnlyPublic,
/// Both public operations and operations marked with `x-hidden`
PublicAndHidden,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get it, but PublicAndHidden just kinda makes my brain bleed blood tears when I read it.

/// Only operations marked with `x-hidden`
OnlyHidden,
}

fn main() -> anyhow::Result<()> {
tracing_subscriber::fmt().with_writer(io::stderr).init();

Expand Down Expand Up @@ -119,7 +130,7 @@ fn analyze_and_generate(
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_hidden).unwrap();
let api = Api::new(paths, &components.schemas, flags.include_mode).unwrap();
let types = api.types(&mut components.schemas, webhooks);

if flags.debug {
Expand Down