Skip to content

Commit 1ed3490

Browse files
Only add x-codeSamples extensions to docs builds of openapi (#4582)
1 parent 5b7a1fc commit 1ed3490

File tree

8 files changed

+1587
-31657
lines changed

8 files changed

+1587
-31657
lines changed

Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ validate-no-cache: ## Validate a given endpoint request or response without loca
88

99
generate: ## Generate the output spec
1010
@echo ">> generating the spec .."
11-
@make generate-language-examples
1211
@npm run generate-schema --prefix compiler -- --spec ../specification/ --output ../output/
1312
@npm run start --prefix typescript-generator
1413

@@ -55,7 +54,9 @@ transform-to-openapi: ## Generate the OpenAPI definition from the compiled schem
5554
@npm run transform-to-openapi -- --schema output/schema/schema.json --flavor serverless --output output/openapi/elasticsearch-serverless-openapi.json
5655

5756
transform-to-openapi-for-docs: ## Generate the OpenAPI definition tailored for API docs generation
58-
@npm run transform-to-openapi -- --schema output/schema/schema.json --flavor stack --lift-enum-descriptions --merge-multipath-endpoints --multipath-redirects --output output/openapi/elasticsearch-openapi-docs.json
57+
@make generate-language-examples
58+
@make generate
59+
@npm run transform-to-openapi -- --schema output/schema/schema.json --flavor stack --lift-enum-descriptions --merge-multipath-endpoints --multipath-redirects --include-language-examples --output output/openapi/elasticsearch-openapi-docs.json
5960

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

compiler-rs/clients_schema_to_openapi/src/cli.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ pub struct Cli {
3535
/// output a redirection map when merging multipath endpoints
3636
#[argh(switch)]
3737
pub multipath_redirects: bool,
38+
39+
/// include the x-codeSamples extension with language examples for all endpoints
40+
#[argh(switch)]
41+
pub include_language_examples: bool,
3842
}
3943

4044
impl Cli {
@@ -74,6 +78,7 @@ impl From<Cli> for Configuration {
7478
lift_enum_descriptions: cli.lift_enum_descriptions,
7579
merge_multipath_endpoints: cli.merge_multipath_endpoints,
7680
multipath_redirects: cli.multipath_redirects,
81+
include_language_examples: cli.include_language_examples,
7782
namespaces: if cli.namespace.is_empty() {
7883
None
7984
} else {

compiler-rs/clients_schema_to_openapi/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ pub struct Configuration {
4444

4545
/// Should we output a redirect map when merging multipath endpoints?
4646
pub multipath_redirects: bool,
47+
48+
/// include the x-codeSamples extension with language examples for all endpoints
49+
pub include_language_examples: bool,
4750
}
4851

4952
pub struct OpenApiConversion {

compiler-rs/clients_schema_to_openapi/src/paths.rs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -346,30 +346,32 @@ pub fn add_endpoint(
346346
// add the x-state extension for availability
347347
let mut extensions = crate::availability_as_extensions(&endpoint.availability, &tac.config.flavor);
348348

349-
// add the x-codeSamples extension
350-
let mut code_samples = vec![];
351-
if let Some(examples) = request.examples.clone() {
352-
if let Some((_, example)) = examples.first() {
353-
let request_line = example.method_request.clone().unwrap_or(String::from(""));
354-
let request_body = example.value.clone().unwrap_or(String::from(""));
355-
if !request_line.is_empty() {
356-
code_samples.push(serde_json::json!({
357-
"lang": "Console",
358-
"source": request_line + "\n" + request_body.as_str(),
359-
}));
360-
}
361-
if let Some(alternatives) = example.alternatives.clone() {
362-
for alternative in alternatives.iter() {
349+
if tac.config.include_language_examples {
350+
// add the x-codeSamples extension
351+
let mut code_samples = vec![];
352+
if let Some(examples) = request.examples.clone() {
353+
if let Some((_, example)) = examples.first() {
354+
let request_line = example.method_request.clone().unwrap_or(String::from(""));
355+
let request_body = example.value.clone().unwrap_or(String::from(""));
356+
if !request_line.is_empty() {
363357
code_samples.push(serde_json::json!({
364-
"lang": alternative.language,
365-
"source": alternative.code.as_str(),
358+
"lang": "Console",
359+
"source": request_line + "\n" + request_body.as_str(),
366360
}));
367361
}
362+
if let Some(alternatives) = example.alternatives.clone() {
363+
for alternative in alternatives.iter() {
364+
code_samples.push(serde_json::json!({
365+
"lang": alternative.language,
366+
"source": alternative.code.as_str(),
367+
}));
368+
}
369+
}
368370
}
369371
}
370-
}
371-
if !code_samples.is_empty() {
372-
extensions.insert("x-codeSamples".to_string(), serde_json::json!(code_samples));
372+
if !code_samples.is_empty() {
373+
extensions.insert("x-codeSamples".to_string(), serde_json::json!(code_samples));
374+
}
373375
}
374376

375377
// Create the operation, it will be repeated if we have several methods
Binary file not shown.

docs/examples/generate-language-examples.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,14 @@ async function generateLanguages(example) {
3636
request += '\n' + JSON.stringify(data.value);
3737
}
3838
}
39-
data.alternatives = [];
39+
const alternatives = [];
4040
for (const lang of LANGUAGES) {
41-
data.alternatives.push({
41+
alternatives.push({
4242
language: lang,
4343
code: (await convertRequests(request, lang, {})).trim(),
4444
});
4545
}
46+
data.alternatives = alternatives.concat(data.alternatives.filter(pair => !LANGUAGES.includes(pair.language)));
4647
doc.delete('alternatives');
4748
doc.add(doc.createPair('alternatives', data.alternatives));
4849
await fs.promises.writeFile(example, doc.toString({lineWidth: 132}));

0 commit comments

Comments
 (0)