Skip to content

Commit 9902312

Browse files
committed
alt paths and auth info in extensions
1 parent 94b2f1a commit 9902312

File tree

3 files changed

+65
-55
lines changed

3 files changed

+65
-55
lines changed

compiler-rs/clients_schema_to_openapi/src/lib.rs

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ mod utils;
2222
pub mod cli;
2323

2424
use indexmap::IndexMap;
25-
26-
use clients_schema::{Availabilities, Availability, Flavor, IndexedModel, Stability, Visibility};
25+
use itertools::Itertools;
26+
use clients_schema::{Availabilities, Availability, Flavor, IndexedModel, Privileges, Stability, UrlTemplate, Visibility};
2727
use openapiv3::{Components, OpenAPI};
2828
use serde_json::{Map,Value};
2929
use clients_schema::transform::ExpandConfig;
@@ -201,6 +201,52 @@ pub fn product_meta_as_extensions(namespace: &str, product_meta: &IndexMap<Strin
201201
result
202202
}
203203

204+
pub fn auths_as_extentions(privileges: &Option<Privileges>) -> IndexMap<String, Value> {
205+
let mut result = IndexMap::new();
206+
let mut auths_list: Vec<Value> = Vec::new();
207+
208+
if let Some(privs) = privileges {
209+
if !privs.index.is_empty() {
210+
let mut index_priv = "Index privileges: ".to_string();
211+
index_priv += &privs.index.iter()
212+
.map(|a| format!("`{a}`"))
213+
.join(",");
214+
index_priv += "\n";
215+
auths_list.push(Value::String(index_priv));
216+
}
217+
if !privs.cluster.is_empty() {
218+
let mut cluster_priv = "Cluster privileges: ".to_string();
219+
cluster_priv += &privs.cluster.iter()
220+
.map(|a| format!("`{a}`"))
221+
.join(",");
222+
cluster_priv += "\n";
223+
auths_list.push(Value::String(cluster_priv));
224+
}
225+
result.insert("x-req-auth".to_string(),Value::Array(auths_list));
226+
}
227+
result
228+
}
229+
230+
pub fn paths_as_extentions(urls: Vec<UrlTemplate>) -> IndexMap<String, Value> {
231+
let mut result = IndexMap::new();
232+
if !urls.is_empty() {
233+
let mut paths_list: Vec<Value> = Vec::new();
234+
for url in urls {
235+
for method in url.methods {
236+
let lower_method = method.to_lowercase();
237+
let path = &url.path;
238+
paths_list.push(Value::String(format!(r#"<div>
239+
<span class="operation-verb {lower_method}">{method}</span>
240+
<span class="operation-path">{path}</span>
241+
</div>
242+
"#)));
243+
}
244+
}
245+
result.insert("x-variations".to_string(), Value::Array(paths_list));
246+
}
247+
result
248+
}
249+
204250
pub fn availability_as_extensions(availabilities: &Option<Availabilities>, flavor: &Option<Flavor>) -> IndexMap<String, Value> {
205251
let mut result = IndexMap::new();
206252
convert_availabilities(availabilities, flavor, &mut result);

compiler-rs/clients_schema_to_openapi/src/paths.rs

Lines changed: 17 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,18 @@ use std::collections::HashMap;
1919
use std::fmt::Write;
2020

2121
use anyhow::{anyhow, bail};
22-
use clients_schema::{Privileges, Property};
22+
use clients_schema::{Property};
2323
use indexmap::IndexMap;
2424
use indexmap::indexmap;
2525
use icu_segmenter::SentenceSegmenter;
26-
use itertools::Itertools;
2726
use openapiv3::{
2827
MediaType, Parameter, ParameterData, ParameterSchemaOrContent, PathItem, PathStyle, Paths, QueryStyle, ReferenceOr,
2928
RequestBody, Response, Responses, StatusCode, Example
3029
};
3130
use serde_json::Value;
3231
use clients_schema::SchemaExample;
3332
use crate::components::TypesAndComponents;
34-
use crate::convert_availabilities;
33+
use crate::{auths_as_extentions, convert_availabilities, paths_as_extentions};
3534

3635
/// Add an endpoint to the OpenAPI schema. This will result in the addition of a number of elements to the
3736
/// openapi schema's `paths` and `components` sections.
@@ -248,6 +247,8 @@ pub fn add_endpoint(
248247

249248
let mut new_endpoint: clients_schema::Endpoint;
250249

250+
let mut longest_urls = Vec::new();
251+
251252
let endpoint = if is_multipath && tac.config.merge_multipath_endpoints {
252253

253254
// Add redirects for operations that would have been generated otherwise
@@ -274,6 +275,8 @@ pub fn add_endpoint(
274275
let mut urls = vec![longest_path];
275276
std::mem::swap(&mut endpoint.urls, &mut urls);
276277

278+
longest_urls = urls;
279+
277280
let split_desc = split_summary_desc(&endpoint.description);
278281

279282
// Make sure the description is stays at the top
@@ -282,24 +285,6 @@ pub fn add_endpoint(
282285
None => String::new(),
283286
};
284287

285-
// Convert removed paths to descriptions
286-
write!(description, "**All methods and paths for this operation:**\n\n")?;
287-
288-
for url in urls {
289-
for method in url.methods {
290-
let lower_method = method.to_lowercase();
291-
let path = &url.path;
292-
write!(
293-
description,
294-
r#"<div>
295-
<span class="operation-verb {lower_method}">{method}</span>
296-
<span class="operation-path">{path}</span>
297-
</div>
298-
"#
299-
)?;
300-
}
301-
}
302-
303288
if let Some(desc) = &split_desc.description {
304289
write!(description, "\n\n{}", desc)?;
305290
}
@@ -335,18 +320,15 @@ pub fn add_endpoint(
335320

336321
parameters.append(&mut query_params.clone());
337322

338-
let sum_desc = split_summary_desc(&endpoint.description);
339-
340-
let privilege_desc = add_privileges(&endpoint.privileges);
341-
342-
let full_desc = match (sum_desc.description, privilege_desc) {
343-
(Some(a), Some(b)) => Some(a+ &b),
344-
(opt_a, opt_b) => opt_a.or(opt_b)
345-
};
346-
347323
// add the x-state extension for availability
348324
let mut extensions = crate::availability_as_extensions(&endpoint.availability, &tac.config.flavor);
349325

326+
// add the x-variations extension for paths
327+
extensions.append(&mut paths_as_extentions(longest_urls.clone()));
328+
329+
// add the x-req-auth extension for auth privileges
330+
extensions.append(&mut auths_as_extentions(&endpoint.privileges));
331+
350332
if tac.config.include_language_examples {
351333
// add the x-codeSamples extension
352334
let mut code_samples = vec![];
@@ -375,8 +357,12 @@ pub fn add_endpoint(
375357
}
376358
}
377359

360+
// add the x-metaTags extension for product name
378361
extensions.append(&mut crate::product_meta_as_extensions(namespace, product_meta));
379362

363+
// split summary from description
364+
let sum_desc = split_summary_desc(&endpoint.description);
365+
380366
// Create the operation, it will be repeated if we have several methods
381367
let operation = openapiv3::Operation {
382368
tags: if let Some(doc_tag) = &endpoint.doc_tag {
@@ -385,7 +371,7 @@ pub fn add_endpoint(
385371
vec![namespace.to_string()]
386372
},
387373
summary: sum_desc.summary,
388-
description: full_desc,
374+
description: sum_desc.description,
389375
external_docs: tac.convert_external_docs(endpoint),
390376
// external_docs: None, // Need values that differ from client purposes
391377
operation_id: None, // set in clone_operation below with operation_counter
@@ -522,28 +508,6 @@ fn split_summary_desc(desc: &str) -> SplitDesc{
522508
}
523509
}
524510

525-
fn add_privileges(privileges: &Option<Privileges>) -> Option<String>{
526-
if let Some(privs) = privileges {
527-
let mut result = "\n\n## Required authorization\n\n".to_string();
528-
if !privs.index.is_empty() {
529-
result += "* Index privileges: ";
530-
result += &privs.index.iter()
531-
.map(|a| format!("`{a}`"))
532-
.join(",");
533-
result += "\n";
534-
}
535-
if !privs.cluster.is_empty() {
536-
result += "* Cluster privileges: ";
537-
result += &privs.cluster.iter()
538-
.map(|a| format!("`{a}`"))
539-
.join(",");
540-
result += "\n";
541-
}
542-
return Some(result)
543-
}
544-
None
545-
}
546-
547511
#[derive(PartialEq,Debug)]
548512
struct SplitDesc {
549513
summary: Option<String>,
18.3 KB
Binary file not shown.

0 commit comments

Comments
 (0)