Skip to content

Commit 5ea3c24

Browse files
committed
Add new option --specified-operations
Allows me to only include operations from a specified list of operations, This will be helpful when I generate the internal JS lib
1 parent 0cae3a8 commit 5ea3c24

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

src/api.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ impl Api {
2626
paths: openapi::Paths,
2727
component_schemas: &IndexMap<String, openapi::SchemaObject>,
2828
include_mode: IncludeMode,
29+
specified_operations: BTreeSet<String>,
2930
) -> anyhow::Result<Self> {
3031
let mut resources = BTreeMap::new();
3132

@@ -40,9 +41,14 @@ impl Api {
4041
}
4142

4243
for (method, op) in path_item {
43-
if let Some((res_path, op)) =
44-
Operation::from_openapi(&path, method, op, component_schemas, include_mode)
45-
{
44+
if let Some((res_path, op)) = Operation::from_openapi(
45+
&path,
46+
method,
47+
op,
48+
component_schemas,
49+
include_mode,
50+
&specified_operations,
51+
) {
4652
let resource = get_or_insert_resource(&mut resources, res_path);
4753
resource.operations.push(op);
4854
}
@@ -186,6 +192,7 @@ impl Operation {
186192
op: openapi::Operation,
187193
component_schemas: &IndexMap<String, aide::openapi::SchemaObject>,
188194
include_mode: IncludeMode,
195+
specified_operations: &BTreeSet<String>,
189196
) -> Option<(Vec<String>, Self)> {
190197
let Some(op_id) = op.operation_id else {
191198
// ignore operations without an operationId
@@ -199,6 +206,7 @@ impl Operation {
199206
IncludeMode::OnlyPublic => !x_hidden,
200207
IncludeMode::PublicAndHidden => true,
201208
IncludeMode::OnlyHidden => x_hidden,
209+
IncludeMode::Specified => specified_operations.contains(&op_id),
202210
};
203211
if !include_operation {
204212
return None;

src/main.rs

+21-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::{
2+
collections::BTreeSet,
23
io::{self, BufWriter, Write as _},
34
path::PathBuf,
45
};
@@ -43,6 +44,12 @@ enum Command {
4344

4445
#[clap(flatten)]
4546
flags: GenerateFlags,
47+
48+
/// The specified operations for --include-mode=specified
49+
///
50+
/// This expects the operation ID, for example v1.message.create
51+
#[clap(long)]
52+
specified_operations: Vec<String>,
4653
},
4754
}
4855

@@ -71,6 +78,8 @@ enum IncludeMode {
7178
PublicAndHidden,
7279
/// Only operations marked with `x-hidden`
7380
OnlyHidden,
81+
/// Only include operations specified in `--specified-operations`
82+
Specified,
7483
}
7584

7685
fn main() -> anyhow::Result<()> {
@@ -82,15 +91,17 @@ fn main() -> anyhow::Result<()> {
8291
input_file,
8392
output_dir,
8493
flags,
94+
specified_operations,
8595
} = args.command;
96+
let specified_operations = BTreeSet::from_iter(specified_operations);
8697

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

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

91102
match &output_dir {
92103
Some(path) => {
93-
analyze_and_generate(spec, template.into(), path, flags)?;
104+
analyze_and_generate(spec, template.into(), path, flags, specified_operations)?;
94105
}
95106
None => {
96107
let output_dir_root = PathBuf::from("out");
@@ -112,7 +123,7 @@ fn main() -> anyhow::Result<()> {
112123
.path()
113124
.try_into()
114125
.context("non-UTF8 tempdir path")?;
115-
analyze_and_generate(spec, template.into(), path, flags)?;
126+
analyze_and_generate(spec, template.into(), path, flags, specified_operations)?;
116127
// Persist the TempDir if everything was successful
117128
_ = output_dir.into_path();
118129
}
@@ -126,11 +137,18 @@ fn analyze_and_generate(
126137
template: String,
127138
path: &Utf8Path,
128139
flags: GenerateFlags,
140+
specified_operations: BTreeSet<String>,
129141
) -> anyhow::Result<()> {
130142
let webhooks = get_webhooks(&spec);
131143
let mut components = spec.components.unwrap_or_default();
132144
if let Some(paths) = spec.paths {
133-
let api = Api::new(paths, &components.schemas, flags.include_mode).unwrap();
145+
let api = Api::new(
146+
paths,
147+
&components.schemas,
148+
flags.include_mode,
149+
specified_operations,
150+
)
151+
.unwrap();
134152
let types = api.types(&mut components.schemas, webhooks);
135153

136154
if flags.debug {

src/postprocessing.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ fn execute_command(
154154
bail!("`{command}` failed with exit code {:?}", exit_status.code());
155155
}
156156
Err(e) if e.kind() == io::ErrorKind::NotFound => {
157-
bail!("`{command}` not found - run with --no-postprocessing to skip");
157+
bail!("`{command}` not found - run with --no-postprocess to skip");
158158
}
159159
Err(e) => Err(e.into()),
160160
}

0 commit comments

Comments
 (0)