Skip to content

Commit dbbdd21

Browse files
authored
[OpenAPI] Add a CLI to configure export parameters (#4426)
1 parent d0fded3 commit dbbdd21

File tree

18 files changed

+260
-225
lines changed

18 files changed

+260
-225
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,5 +234,6 @@ dist/* binary
234234

235235
output/** linguist-generated=true
236236
output/schema/validation-errors.json linguist-generated=false
237+
compiler-rs/compiler-wasm-lib/pkg/* linguist-generated=true
237238

238239
####################################################################################################

Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,11 @@ transform-expand-generics: ## Create a new schema with all generics expanded
4949
@npm run transform-expand-generics --prefix compiler
5050

5151
transform-to-openapi: ## Generate the OpenAPI definition from the compiled schema
52-
@npm run transform-to-openapi --prefix compiler
52+
@npm run transform-to-openapi -- --schema output/schema/schema.json --flavor stack --output output/openapi/elasticsearch-openapi.json
53+
@npm run transform-to-openapi -- --schema output/schema/schema.json --flavor serverless --output output/openapi/elasticsearch-serverless-openapi.json
5354

5455
filter-for-serverless: ## Generate the serverless version from the compiled schema
55-
@npm run --prefix compiler filter-by-availability -- --serverless --visibility=public --input ../output/schema/schema.json --output ../output/schema/schema-serverless.json
56+
@npm run --prefix compiler filter-by-availability -- --serverless --visibility=public --input ../output/schema/schema.json --output ../output/output/openapi/elasticsearch-serverless-openapi.json
5657

5758
dump-routes: ## Create a new schema with all generics expanded
5859
@npm run dump-routes --prefix compiler

compiler-rs/Cargo.lock

Lines changed: 63 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler-rs/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ members = [
1010
[workspace.dependencies]
1111
anyhow = "1"
1212
arcstr = "1"
13+
argh = "0.1"
1314
clap = "4"
1415
console_error_panic_hook = "0.1"
1516
convert_case = "0.6"

compiler-rs/clients_schema_to_openapi/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ publish = false
77
[dependencies]
88
clients_schema = {path="../clients_schema"}
99

10+
argh = { workspace = true }
11+
derive_more = { version = "2", features = ["from_str"] }
1012
serde_json = { workspace = true }
1113
serde_ignored = { workspace = true }
1214
icu_segmenter = { workspace = true }
@@ -16,5 +18,3 @@ indexmap = { workspace = true }
1618

1719
tracing = { workspace = true }
1820
tracing-subscriber = { workspace = true }
19-
clap = { workspace = true, features = ["derive"] }
20-
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
use std::path::PathBuf;
2+
use argh::FromArgs;
3+
use clients_schema::Flavor;
4+
use crate::Configuration;
5+
6+
/// Convert schema.json to an OpenAPI schema
7+
#[derive(Debug, FromArgs)]
8+
pub struct Cli {
9+
/// schema file to transform.
10+
#[argh(option)]
11+
pub schema: PathBuf,
12+
13+
/// output file.
14+
#[argh(option)]
15+
pub output: PathBuf,
16+
17+
//---- Fields from lib::Configuration
18+
19+
/// the flavor to generate [all, stack, serverless - default = all]
20+
#[argh(option, default = "SchemaFlavor::All")]
21+
pub flavor: SchemaFlavor,
22+
23+
/// add enum descriptions to property descriptions [default = true]
24+
#[argh(option, default = "true")]
25+
pub lift_enum_descriptions: bool,
26+
27+
/// generate only this namespace (can be repeated)
28+
#[argh(option)]
29+
pub namespace: Vec<String>,
30+
}
31+
32+
use derive_more::FromStr;
33+
34+
#[derive(Debug, Clone, PartialEq, FromStr)]
35+
pub enum SchemaFlavor {
36+
/// No schema filtering
37+
All,
38+
/// Stack (stateful) flavor
39+
Stack,
40+
/// Serverless flavor
41+
Serverless,
42+
}
43+
44+
impl From<Cli> for Configuration {
45+
fn from(val: Cli) -> Configuration {
46+
let flavor = match val.flavor {
47+
SchemaFlavor::All => None,
48+
SchemaFlavor::Serverless => Some(Flavor::Serverless),
49+
SchemaFlavor::Stack => Some(Flavor::Stack),
50+
};
51+
52+
Configuration {
53+
flavor,
54+
lift_enum_descriptions: val.lift_enum_descriptions,
55+
namespaces: if val.namespace.is_empty() {
56+
None
57+
} else {
58+
Some(val.namespace)
59+
},
60+
}
61+
}
62+
}

compiler-rs/clients_schema_to_openapi/src/lib.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ mod components;
1919
mod paths;
2020
mod schemas;
2121
mod utils;
22+
pub mod cli;
2223

2324
use indexmap::IndexMap;
2425

@@ -29,18 +30,10 @@ use crate::components::TypesAndComponents;
2930

3031
pub struct Configuration {
3132
pub flavor: Option<Flavor>,
33+
pub namespaces: Option<Vec<String>>,
3234
pub lift_enum_descriptions: bool,
3335
}
3436

35-
impl Default for Configuration {
36-
fn default() -> Self {
37-
Self {
38-
flavor: None,
39-
lift_enum_descriptions: true,
40-
}
41-
}
42-
}
43-
4437
/// Convert an API model into an OpenAPI v3 schema, optionally filtered for a given flavor
4538
pub fn convert_schema(mut schema: IndexedModel, config: Configuration) -> anyhow::Result<OpenAPI> {
4639
// Expand generics
@@ -105,6 +98,11 @@ pub fn convert_expanded_schema(model: &IndexedModel, config: &Configuration) ->
10598

10699
// Endpoints
107100
for endpoint in &model.endpoints {
101+
if let Some(namespaces) = &config.namespaces {
102+
if !namespaces.contains(&endpoint.name) {
103+
continue;
104+
}
105+
}
108106
paths::add_endpoint(endpoint, &mut tac, &mut openapi.paths)?;
109107
}
110108

0 commit comments

Comments
 (0)