Skip to content

Commit

Permalink
Populate OpenAPI summary field with first line of endpoint doc commen…
Browse files Browse the repository at this point in the history
…ts (#252)
  • Loading branch information
ahl authored Jan 21, 2022
1 parent 2559830 commit f98d611
Show file tree
Hide file tree
Showing 8 changed files with 244 additions and 54 deletions.
25 changes: 25 additions & 0 deletions Cargo.lock

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

10 changes: 9 additions & 1 deletion dropshot/src/api_description.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub struct ApiEndpoint<Context: ServerContext> {
pub path: String,
pub parameters: Vec<ApiEndpointParameter>,
pub response: ApiEndpointResponse,
pub summary: Option<String>,
pub description: Option<String>,
pub tags: Vec<String>,
pub paginated: bool,
Expand Down Expand Up @@ -63,13 +64,19 @@ impl<'a, Context: ServerContext> ApiEndpoint<Context> {
path: path.to_string(),
parameters: func_parameters.parameters,
response,
summary: None,
description: None,
tags: vec![],
paginated: func_parameters.paginated,
visible: true,
}
}

pub fn summary<T: ToString>(mut self, description: T) -> Self {
self.summary.replace(description.to_string());
self
}

pub fn description<T: ToString>(mut self, description: T) -> Self {
self.description.replace(description.to_string());
self
Expand Down Expand Up @@ -465,6 +472,7 @@ impl<Context: ServerContext> ApiDescription<Context> {
};
let mut operation = openapiv3::Operation::default();
operation.operation_id = Some(endpoint.operation_id.clone());
operation.summary = endpoint.summary.clone();
operation.description = endpoint.description.clone();
operation.tags = endpoint.tags.clone();

Expand Down Expand Up @@ -812,7 +820,7 @@ fn j2oas_schema_object(
(None, None) => {
openapiv3::SchemaKind::Any(openapiv3::AnySchema::default())
}
_ => panic!("invalid"),
_ => panic!("invalid {:#?}", obj),
};

let mut data = openapiv3::SchemaData::default();
Expand Down
1 change: 1 addition & 0 deletions dropshot/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,7 @@ mod test {
success: None,
description: None,
},
summary: None,
description: None,
tags: vec![],
paginated: false,
Expand Down
6 changes: 4 additions & 2 deletions dropshot/tests/test_openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,8 @@
},
"/test/person": {
"get": {
"description": "This is a multi-line comment. It uses Rust-style.",
"summary": "Rust style comment",
"description": "This is a multi-line comment.",
"operationId": "handler1",
"responses": {
"200": {
Expand Down Expand Up @@ -328,7 +329,8 @@
},
"/test/woman": {
"put": {
"description": "This is a multi-line comment. It uses C-style.",
"summary": "C-style comment",
"description": "This is a multi-line comment.",
"operationId": "handler2",
"parameters": [
{
Expand Down
6 changes: 4 additions & 2 deletions dropshot/tests/test_openapi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ use std::{io::Cursor, str::from_utf8, sync::Arc};
method = GET,
path = "/test/person",
}]
/// Rust style comment
///
/// This is a multi-
/// line comment.
/// It uses Rust-style.
async fn handler1(
_rqctx: Arc<RequestContext<()>>,
) -> Result<HttpResponseOk<()>, HttpError> {
Expand All @@ -36,9 +37,10 @@ struct QueryArgs {
path = "/test/woman",
}]
/**
* C-style comment
*
* This is a multi-
* line comment.
* It uses C-style.
*/
async fn handler2(
_rqctx: Arc<RequestContext<()>>,
Expand Down
6 changes: 4 additions & 2 deletions dropshot/tests/test_openapi_fuller.json
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,8 @@
},
"/test/person": {
"get": {
"description": "This is a multi-line comment. It uses Rust-style.",
"summary": "Rust style comment",
"description": "This is a multi-line comment.",
"operationId": "handler1",
"responses": {
"200": {
Expand Down Expand Up @@ -336,7 +337,8 @@
},
"/test/woman": {
"put": {
"description": "This is a multi-line comment. It uses C-style.",
"summary": "C-style comment",
"description": "This is a multi-line comment.",
"operationId": "handler2",
"parameters": [
{
Expand Down
7 changes: 5 additions & 2 deletions dropshot_endpoint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ proc-macro = true
[dependencies]
proc-macro2 = "1"
quote = "1"
serde_tokenstream = "0.1.3"
serde_tokenstream = "0.1"

[dependencies.serde]
version = "1.0"
features = [ "derive" ]

[dependencies.syn]
version = "1.0.85"
version = "1.0"
features = [ "parsing", "printing" ]

[dev-dependencies]
schema = "0.0.1"
Loading

0 comments on commit f98d611

Please sign in to comment.