Skip to content

Commit a7d1980

Browse files
committed
product meta from file
1 parent 27c144c commit a7d1980

File tree

6 files changed

+33
-19
lines changed

6 files changed

+33
-19
lines changed

compiler-rs/clients_schema_to_openapi/src/lib.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub struct Configuration {
4444
}
4545

4646
/// Convert an API model into an OpenAPI v3 schema, optionally filtered for a given flavor
47-
pub fn convert_schema(mut schema: IndexedModel, config: Configuration) -> anyhow::Result<OpenAPI> {
47+
pub fn convert_schema(mut schema: IndexedModel, config: Configuration, product_meta: IndexMap<String,String>) -> anyhow::Result<OpenAPI> {
4848
// Expand generics
4949
schema = clients_schema::transform::expand_generics(schema, ExpandConfig::default())?;
5050

@@ -65,7 +65,7 @@ pub fn convert_schema(mut schema: IndexedModel, config: Configuration) -> anyhow
6565
schema = clients_schema::transform::filter_availability(schema, filter)?;
6666
}
6767

68-
convert_expanded_schema(&schema, &config)
68+
convert_expanded_schema(&schema, &config, &product_meta)
6969
}
7070

7171
/// Convert an API model into an OpenAPI v3 schema. The input model must have all generics expanded, conversion
@@ -74,7 +74,7 @@ pub fn convert_schema(mut schema: IndexedModel, config: Configuration) -> anyhow
7474
/// Note: there are ways to represent [generics in JSON Schema], but its unlikely that tooling will understand it.
7575
///
7676
/// [generics in JSON Schema]: https://json-schema.org/blog/posts/dynamicref-and-generics
77-
pub fn convert_expanded_schema(model: &IndexedModel, config: &Configuration) -> anyhow::Result<OpenAPI> {
77+
pub fn convert_expanded_schema(model: &IndexedModel, config: &Configuration, product_meta: &IndexMap<String,String>) -> anyhow::Result<OpenAPI> {
7878
let mut openapi = OpenAPI {
7979
openapi: "3.0.3".into(),
8080
info: info(model),
@@ -112,7 +112,7 @@ pub fn convert_expanded_schema(model: &IndexedModel, config: &Configuration) ->
112112
continue;
113113
}
114114
}
115-
paths::add_endpoint(endpoint, &mut tac, &mut openapi.paths)?;
115+
paths::add_endpoint(endpoint, &mut tac, &mut openapi.paths, product_meta)?;
116116
}
117117

118118
// // Sort maps to ensure output stability
@@ -158,15 +158,12 @@ fn info(model: &IndexedModel) -> openapiv3::Info {
158158
}
159159
}
160160

161-
pub fn product_meta_as_extensions(namespace: &str) -> IndexMap<String, Value> {
161+
pub fn product_meta_as_extensions(namespace: &str, product_meta: &IndexMap<String,String>) -> IndexMap<String, Value> {
162162
let mut result = IndexMap::new();
163-
let additional_namespace = match namespace {
164-
"inference" => { ", machine-learning"}
165-
"ml" => { ", machine-learning"}
166-
"fleet" => { ", fleet"}
167-
"logstash" => { ", logstash"}
168-
&_ => { ""}
169-
};
163+
let mut additional_namespace= "".to_string();
164+
if let Some(meta) = product_meta.get(namespace) {
165+
additional_namespace = format!(", {meta}");
166+
}
170167

171168
let product_str = format!("elasticsearch{additional_namespace}");
172169
result.insert("x-product-feature".to_string(), Value::String(product_str));

compiler-rs/clients_schema_to_openapi/src/main.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
// under the License.
1717

1818
use std::fs::File;
19-
use clients_schema::IndexedModel;
19+
use indexmap::IndexMap;
20+
use clients_schema::{IndexedModel};
2021
use tracing::Level;
2122
use tracing_subscriber::fmt::format::FmtSpan;
2223
use tracing_subscriber::FmtSubscriber;
@@ -32,9 +33,10 @@ fn main() -> anyhow::Result<()> {
3233
.finish();
3334
tracing::subscriber::set_global_default(subscriber)?;
3435

36+
let product_meta: IndexMap<String, String> = serde_json::from_reader(File::open("../../specification/_doc_ids/product-meta.json")?)?;
3537
let schema = IndexedModel::from_reader(File::open(&cli.schema)?)?;
3638
let output = cli.output.clone();
37-
let openapi = clients_schema_to_openapi::convert_schema(schema, cli.into())?;
39+
let openapi = clients_schema_to_openapi::convert_schema(schema, cli.into(), product_meta)?;
3840
serde_json::to_writer_pretty(File::create(&output)?, &openapi)?;
3941
Ok(())
4042
}

compiler-rs/clients_schema_to_openapi/src/paths.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ use crate::convert_availabilities;
3838
pub fn add_endpoint(
3939
endpoint: &clients_schema::Endpoint,
4040
tac: &mut TypesAndComponents,
41-
out: &mut Paths
41+
out: &mut Paths,
42+
product_meta: &IndexMap<String,String>
4243
) -> anyhow::Result<()> {
4344
if endpoint.request.is_none() {
4445
// tracing::warn!("Endpoint {} is missing a request -- ignored", &endpoint.name);
@@ -345,7 +346,7 @@ pub fn add_endpoint(
345346
if !code_samples.is_empty() {
346347
extensions.insert("x-codeSamples".to_string(), serde_json::json!(code_samples));
347348
}
348-
extensions.append(&mut crate::product_meta_as_extensions(namespace));
349+
extensions.append(&mut crate::product_meta_as_extensions(namespace, product_meta));
349350

350351
// Create the operation, it will be repeated if we have several methods
351352
let operation = openapiv3::Operation {
14.7 KB
Binary file not shown.

compiler-rs/compiler-wasm-lib/src/lib.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
use std::path::PathBuf;
18+
use std::path::{PathBuf, MAIN_SEPARATOR_STR};
1919
use argh::FromArgs;
20-
use clients_schema::IndexedModel;
20+
use clients_schema::{IndexedModel};
2121
use wasm_bindgen::prelude::*;
22+
use clients_schema::indexmap::IndexMap;
2223
use clients_schema_to_openapi::cli::Cli;
2324

2425
/// Minimal bindings to Node's `fs` module.
@@ -62,8 +63,15 @@ pub fn convert0(cli: Cli, cwd: Option<String>) -> anyhow::Result<()> {
6263

6364
let json = node_fs::read_file_sync_to_string(&input.to_string_lossy(), "utf8");
6465
let schema = IndexedModel::from_reader(json.as_bytes())?;
66+
67+
let product_meta_path = match cwd {
68+
Some(ref cwd) => format!("{cwd}{MAIN_SEPARATOR_STR}specification/_doc_ids/product-meta.json"),
69+
None => "specification/_doc_ids/product-meta.json".to_string(),
70+
};
71+
let json_product_map = node_fs::read_file_sync_to_string(&product_meta_path, "utf8");
72+
let product_meta: IndexMap<String, String> = serde_json::from_str(&json_product_map).expect("Cannot parse product metadata file");
6573

66-
let openapi = clients_schema_to_openapi::convert_schema(schema, cli.into())?;
74+
let openapi = clients_schema_to_openapi::convert_schema(schema, cli.into(), product_meta)?;
6775

6876
let result = serde_json::to_string_pretty(&openapi)?;
6977
node_fs::write_file_sync(&output.to_string_lossy(), &result);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"fleet" : "fleet",
3+
"inference" : "machine-learning",
4+
"logstash" : "logstash",
5+
"ml" : "machine-learning"
6+
}

0 commit comments

Comments
 (0)