Skip to content

Commit 0cae3a8

Browse files
committed
Add include-mode to select which operations to include
Keep the same default behavior to only include public operations
1 parent f22ff3a commit 0cae3a8

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

src/api.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use serde::Serialize;
99
use crate::{
1010
types::{FieldType, Types},
1111
util::{get_schema_name, serialize_btree_map_values},
12+
IncludeMode,
1213
};
1314

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

@@ -40,7 +41,7 @@ impl Api {
4041

4142
for (method, op) in path_item {
4243
if let Some((res_path, op)) =
43-
Operation::from_openapi(&path, method, op, component_schemas, include_hidden)
44+
Operation::from_openapi(&path, method, op, component_schemas, include_mode)
4445
{
4546
let resource = get_or_insert_resource(&mut resources, res_path);
4647
resource.operations.push(op);
@@ -184,15 +185,22 @@ impl Operation {
184185
method: &str,
185186
op: openapi::Operation,
186187
component_schemas: &IndexMap<String, aide::openapi::SchemaObject>,
187-
include_hidden: bool,
188+
include_mode: IncludeMode,
188189
) -> Option<(Vec<String>, Self)> {
189190
let Some(op_id) = op.operation_id else {
190191
// ignore operations without an operationId
191192
return None;
192193
};
193194
tracing::Span::current().record("op_id", &op_id);
194195

195-
if !include_hidden && op.extensions.get("x-hidden").is_some_and(|val| val == true) {
196+
// verbose, but very easy to understand
197+
let x_hidden = op.extensions.get("x-hidden").is_some_and(|val| val == true);
198+
let include_operation = match include_mode {
199+
IncludeMode::OnlyPublic => !x_hidden,
200+
IncludeMode::PublicAndHidden => true,
201+
IncludeMode::OnlyHidden => x_hidden,
202+
};
203+
if !include_operation {
196204
return None;
197205
}
198206

src/main.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,26 @@ struct GenerateFlags {
5353
#[clap(long)]
5454
no_postprocess: bool,
5555

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

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

65+
#[derive(Copy, Clone, clap::ValueEnum)]
66+
#[clap(rename_all = "kebab-case")]
67+
enum IncludeMode {
68+
/// Only public options
69+
OnlyPublic,
70+
/// Both public operations and operations marked with `x-hidden`
71+
PublicAndHidden,
72+
/// Only operations marked with `x-hidden`
73+
OnlyHidden,
74+
}
75+
6576
fn main() -> anyhow::Result<()> {
6677
tracing_subscriber::fmt().with_writer(io::stderr).init();
6778

@@ -119,7 +130,7 @@ fn analyze_and_generate(
119130
let webhooks = get_webhooks(&spec);
120131
let mut components = spec.components.unwrap_or_default();
121132
if let Some(paths) = spec.paths {
122-
let api = Api::new(paths, &components.schemas, flags.include_hidden).unwrap();
133+
let api = Api::new(paths, &components.schemas, flags.include_mode).unwrap();
123134
let types = api.types(&mut components.schemas, webhooks);
124135

125136
if flags.debug {

0 commit comments

Comments
 (0)