Skip to content
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

Splitting summary and description in OpenAPI output #2608

Merged
merged 9 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
212 changes: 212 additions & 0 deletions compiler-rs/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions compiler-rs/clients_schema_to_openapi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ serde = {version = "1.0", features=["derive"]}
serde_json = "1.0"
serde_path_to_error = "0.1"
serde_ignored = "0.1"
icu_segmenter = "1.5.0"
openapiv3 = "1.0"
anyhow = "1.0"
indexmap = "1.9"
Expand Down
64 changes: 62 additions & 2 deletions compiler-rs/clients_schema_to_openapi/src/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use std::fmt::Write;
use anyhow::{anyhow, bail};
use clients_schema::Property;
use indexmap::indexmap;
use icu_segmenter::SentenceSegmenter;
use openapiv3::{
MediaType, Parameter, ParameterData, ParameterSchemaOrContent, PathItem, PathStyle, Paths, QueryStyle, ReferenceOr,
RequestBody, Response, Responses, StatusCode,
Expand Down Expand Up @@ -191,11 +192,13 @@ pub fn add_endpoint(

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

let sum_desc = split_summary_desc(&endpoint.description);

// Create the operation, it will be repeated if we have several methods
let operation = openapiv3::Operation {
tags: vec![endpoint.name.clone()],
summary: Some(endpoint.description.clone()),
description: Some(endpoint.description.clone()),
summary: sum_desc.summary,
description: sum_desc.description,
external_docs: tac.convert_external_docs(endpoint),
operation_id: None, // set in clone_operation below with operation_counter
parameters,
Expand Down Expand Up @@ -311,6 +314,39 @@ fn get_path_parameters(template: &str) -> Vec<&str> {
result
}

// Splits the original endpoint description into OpenAPI summary and description, where summary
// is the first sentence of the original description with no trailing `.`, and description contains
// the remaining sentences, if there are any left.
fn split_summary_desc(desc: &str) -> SplitDesc{
Anaethelion marked this conversation as resolved.
Show resolved Hide resolved
let segmenter = SentenceSegmenter::new();

let desc_no_newlines = desc.replace('\n'," ");

let breakpoints: Vec<usize> = segmenter
.segment_str(&desc_no_newlines)
.collect();

if breakpoints.len()<2{
return SplitDesc {
summary: None,
description: None
}
}
let first_line = &desc_no_newlines[breakpoints[0]..breakpoints[1]];
let rest = &desc_no_newlines[breakpoints[1]..breakpoints[breakpoints.len()-1]];

SplitDesc {
summary: Some(String::from(first_line.trim().strip_suffix('.').unwrap_or(first_line))),
description: if !rest.is_empty() {Some(String::from(rest.trim()))} else {None}
}
}

#[derive(PartialEq,Debug)]
struct SplitDesc {
summary: Option<String>,
description: Option<String>
}

#[cfg(test)]
Anaethelion marked this conversation as resolved.
Show resolved Hide resolved
mod tests {
use super::*;
Expand All @@ -325,4 +361,28 @@ mod tests {
assert_eq!(get_path_parameters("{index}{id/"), vec! {"index"});
assert_eq!(get_path_parameters("{index{id}/"), vec! {"index{id"});
}

#[test]
fn test_split_summary_desc() {
assert_eq!(split_summary_desc("One sentence."),
SplitDesc{
summary: Some(String::from("One sentence")),
description: None
});
assert_eq!(split_summary_desc("This is\nstill one. sentence: all; together"),
SplitDesc{
summary: Some(String::from("This is still one. sentence: all; together")),
description: None
});
assert_eq!(split_summary_desc("These are two totally. Separate sentences!"),
SplitDesc{
summary: Some(String::from("These are two totally")),
description: Some(String::from("Separate sentences!"))
});
assert_eq!(split_summary_desc(""),
SplitDesc{
summary: None,
description: None
});
l-trotta marked this conversation as resolved.
Show resolved Hide resolved
}
}
Binary file modified compiler-rs/compiler-wasm-lib/pkg/compiler_wasm_lib_bg.wasm
Binary file not shown.
Loading
Loading