Skip to content

Commit 6046fff

Browse files
authored
[Openapi] Product metadata in extension (#4511) (#4590)
1 parent 3306c94 commit 6046fff

File tree

9 files changed

+2505
-1279
lines changed

9 files changed

+2505
-1279
lines changed

compiler-rs/clients_schema_to_openapi/src/lib.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub struct OpenApiConversion {
5252
}
5353

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

@@ -73,7 +73,7 @@ pub fn convert_schema(mut schema: IndexedModel, config: Configuration) -> anyhow
7373
schema = clients_schema::transform::filter_availability(schema, filter)?;
7474
}
7575

76-
convert_expanded_schema(&schema, &config)
76+
convert_expanded_schema(&schema, &config, &product_meta)
7777
}
7878

7979
/// Convert an API model into an OpenAPI v3 schema. The input model must have all generics expanded, conversion
@@ -82,7 +82,7 @@ pub fn convert_schema(mut schema: IndexedModel, config: Configuration) -> anyhow
8282
/// Note: there are ways to represent [generics in JSON Schema], but its unlikely that tooling will understand it.
8383
///
8484
/// [generics in JSON Schema]: https://json-schema.org/blog/posts/dynamicref-and-generics
85-
pub fn convert_expanded_schema(model: &IndexedModel, config: &Configuration) -> anyhow::Result<OpenApiConversion> {
85+
pub fn convert_expanded_schema(model: &IndexedModel, config: &Configuration, product_meta: &IndexMap<String,String>) -> anyhow::Result<OpenApiConversion> {
8686
let mut openapi = OpenAPI {
8787
openapi: "3.0.3".into(),
8888
info: info(model),
@@ -120,7 +120,7 @@ pub fn convert_expanded_schema(model: &IndexedModel, config: &Configuration) ->
120120
continue;
121121
}
122122
}
123-
paths::add_endpoint(endpoint, &mut tac, &mut openapi.paths)?;
123+
paths::add_endpoint(endpoint, &mut tac, &mut openapi.paths, product_meta)?;
124124
}
125125

126126
// // Sort maps to ensure output stability
@@ -180,7 +180,19 @@ fn info(model: &IndexedModel) -> openapiv3::Info {
180180
}
181181
}
182182

183-
pub fn availability_as_extensions(availabilities: &Option<Availabilities>, flavor: &Option<Flavor>) -> IndexMap<String, serde_json::Value> {
183+
pub fn product_meta_as_extensions(namespace: &str, product_meta: &IndexMap<String,String>) -> IndexMap<String, Value> {
184+
let mut result = IndexMap::new();
185+
let mut additional_namespace= "".to_string();
186+
if let Some(meta) = product_meta.get(namespace) {
187+
additional_namespace = format!(", {meta}");
188+
}
189+
190+
let product_str = format!("elasticsearch{additional_namespace}");
191+
result.insert("x-product-feature".to_string(), Value::String(product_str));
192+
result
193+
}
194+
195+
pub fn availability_as_extensions(availabilities: &Option<Availabilities>, flavor: &Option<Flavor>) -> IndexMap<String, Value> {
184196
let mut result = IndexMap::new();
185197
convert_availabilities(availabilities, flavor, &mut result);
186198
result

compiler-rs/clients_schema_to_openapi/src/main.rs

Lines changed: 5 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,10 +33,12 @@ 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();
3739
let redirect_path = cli.redirect_path(&cli.output);
38-
let openapi = clients_schema_to_openapi::convert_schema(schema, cli.into())?;
40+
let openapi = clients_schema_to_openapi::convert_schema(schema, cli.into(), product_meta)?;
41+
serde_json::to_writer_pretty(File::create(&output)?, &openapi.openapi)?;
3942
serde_json::to_writer_pretty(File::create(&output)?, &openapi.openapi)?;
4043

4144
if let Some(redirects) = openapi.redirects {

compiler-rs/clients_schema_to_openapi/src/paths.rs

Lines changed: 3 additions & 3 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);
@@ -371,8 +372,7 @@ pub fn add_endpoint(
371372
if !code_samples.is_empty() {
372373
extensions.insert("x-codeSamples".to_string(), serde_json::json!(code_samples));
373374
}
374-
let mut ext_availability = crate::availability_as_extensions(&endpoint.availability, &tac.config.flavor);
375-
extensions.append(&mut ext_availability);
375+
extensions.append(&mut crate::product_meta_as_extensions(namespace, product_meta));
376376

377377
// Create the operation, it will be repeated if we have several methods
378378
let operation = openapiv3::Operation {

compiler-rs/compiler-wasm-lib/pkg/compiler_wasm_lib.js

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
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};
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.
@@ -63,8 +64,15 @@ pub fn convert0(cli: Cli, cwd: Option<String>) -> anyhow::Result<()> {
6364

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

67-
let openapi = clients_schema_to_openapi::convert_schema(schema, cli.into())?;
75+
let openapi = clients_schema_to_openapi::convert_schema(schema, cli.into(), product_meta)?;
6876

6977
let result = serde_json::to_string_pretty(&openapi.openapi)?;
7078
node_fs::write_file_sync(&output.to_string_lossy(), &result);

0 commit comments

Comments
 (0)