Skip to content

Add request details to examples #4445

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions compiler-rs/clients_schema/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,7 @@ impl TypeDefinition {
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SchemaExample {
pub summary: Option<String>,
pub method_request: Option<String>,
pub description: Option<String>,
pub value: Option<String>,
pub external_value: Option<String>,
Expand Down
40 changes: 38 additions & 2 deletions compiler-rs/clients_schema_to_openapi/src/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ pub fn add_endpoint(
// If this endpoint response has examples in schema.json, convert them to the
// OpenAPI format and add them to the endpoint response in the OpenAPI document.
let response_examples = if let Some(examples) = &response_def.examples {
get_openapi_examples(examples)
get_openapi_examples(examples)
} else {
IndexMap::new()
};
Expand Down Expand Up @@ -251,6 +251,42 @@ pub fn add_endpoint(

let sum_desc = split_summary_desc(&endpoint.description);

// add the x-state extension for availability
let mut extensions = crate::availability_as_extensions(&endpoint.availability);

// add the x-codeSamples extension
let mut code_samples = vec![];
if let Some(examples) = request.examples.clone() {
if let Some((_, example)) = examples.first() {
let request_line = example.method_request.clone().unwrap_or(String::from(""));
let request_body = example.value.clone().unwrap_or(String::from(""));
if !request_line.is_empty() {
code_samples.push(serde_json::json!({
"lang": "Console",
"source": request_line + "\n" + request_body.as_str(),
}));
}
}
}
if code_samples.is_empty() {
// if there are no example requests we look for example responses
// this can only happen for examples that do not have a request body
if let Some(examples) = response_def.examples.clone() {
if let Some((_, example)) = examples.first() {
let request_line = example.method_request.clone().unwrap_or(String::from(""));
if !request_line.is_empty() {
code_samples.push(serde_json::json!({
"lang": "Console",
"source": request_line + "\n",
}));
}
}
}
}
if !code_samples.is_empty() {
extensions.insert("x-codeSamples".to_string(), serde_json::json!(code_samples));
}

// Create the operation, it will be repeated if we have several methods
let operation = openapiv3::Operation {
tags: if let Some(doc_tag) = &endpoint.doc_tag {
Expand All @@ -274,7 +310,7 @@ pub fn add_endpoint(
deprecated: endpoint.deprecation.is_some(),
security: None,
servers: vec![],
extensions: crate::availability_as_extensions(&endpoint.availability),
extensions,
};


Expand Down
Binary file modified compiler-rs/compiler-wasm-lib/pkg/compiler_wasm_lib_bg.wasm
Binary file not shown.
2 changes: 2 additions & 0 deletions compiler/src/model/metamodel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,8 @@ export class Example {
summary?: string
/** Long description. */
description?: string
/** request method and URL */
method_request?: string
/** Embedded literal example. Mutually exclusive with `external_value` */
value?: string
/** A URI that points to the literal example */
Expand Down
Loading
Loading