Skip to content

Commit

Permalink
ToSchema and IntoParams derive macros now support multiline comments (#…
Browse files Browse the repository at this point in the history
…359)

Make `description` field accept whole doc comment instead of the first line only.

Resolves #358
  • Loading branch information
michael-mark authored Nov 21, 2022
1 parent 9dd9023 commit bb3f60c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 18 deletions.
8 changes: 5 additions & 3 deletions utoipa-gen/src/component/into_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,8 @@ impl ToTokens for Param<'_> {
.rename_all
.map(|rename_all| rename_all.as_rename_rule())
});
let name =
super::rename::<FieldRename>(name, rename_to, rename_all).unwrap_or(Cow::Borrowed(name));
let name = super::rename::<FieldRename>(name, rename_to, rename_all)
.unwrap_or(Cow::Borrowed(name));
let type_tree = TypeTree::from_type(&field.ty);

tokens.extend(quote! { utoipa::openapi::path::ParameterBuilder::new()
Expand All @@ -297,7 +297,9 @@ impl ToTokens for Param<'_> {
if let Some(deprecated) = super::get_deprecated(&field.attrs) {
tokens.extend(quote! { .deprecated(Some(#deprecated)) });
}
if let Some(comment) = CommentAttributes::from_attributes(&field.attrs).first() {
let attributes = CommentAttributes::from_attributes(&field.attrs);
if !attributes.is_empty() {
let comment = attributes.join("\n");
tokens.extend(quote! {
.description(Some(#comment))
})
Expand Down
31 changes: 23 additions & 8 deletions utoipa-gen/src/component/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,9 @@ impl ToTokens for NamedStructSchema<'_> {
tokens.extend(struct_features.to_token_stream())
}

if let Some(comment) = CommentAttributes::from_attributes(self.attributes).first() {
let attributes = CommentAttributes::from_attributes(self.attributes);
if !attributes.is_empty() {
let comment = attributes.join("\n");
tokens.extend(quote! {
.description(Some(#comment))
})
Expand Down Expand Up @@ -432,7 +434,9 @@ impl ToTokens for UnnamedStructSchema<'_> {
}
};

if let Some(comment) = CommentAttributes::from_attributes(self.attributes).first() {
let attributes = CommentAttributes::from_attributes(self.attributes);
if !attributes.is_empty() {
let comment = attributes.join("\n");
if !is_object {
tokens.extend(quote! {
.description(Some(#comment))
Expand Down Expand Up @@ -542,7 +546,9 @@ impl ToTokens for EnumSchemaType<'_> {
tokens.extend(quote! { .deprecated(Some(#deprecated)) });
}

if let Some(comment) = CommentAttributes::from_attributes(attributes).first() {
let attributes = CommentAttributes::from_attributes(attributes);
if !attributes.is_empty() {
let comment = attributes.join("\n");
tokens.extend(quote! {
.description(Some(#comment))
})
Expand Down Expand Up @@ -1064,8 +1070,13 @@ impl ToTokens for SchemaProperty<'_> {
utoipa::openapi::ObjectBuilder::new().additional_properties(Some(#schema_property))
});

if let Some(description) = self.comments.and_then(|attributes| attributes.0.first())
{
if let Some(description) = self.comments.and_then(|attributes| {
if attributes.is_empty() {
None
} else {
Some(attributes.join("\n"))
}
}) {
tokens.extend(quote! {
.description(Some(#description))
})
Expand Down Expand Up @@ -1143,9 +1154,13 @@ impl ToTokens for SchemaProperty<'_> {
})
}

if let Some(description) =
self.comments.and_then(|attributes| attributes.0.first())
{
if let Some(description) = self.comments.and_then(|attributes| {
if attributes.is_empty() {
None
} else {
Some(attributes.join("\n"))
}
}) {
tokens.extend(quote! {
.description(Some(#description))
})
Expand Down
14 changes: 7 additions & 7 deletions utoipa-gen/tests/schema_derive_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ fn derive_struct_with_comments_success() {
let account = api_doc! {
/// This is user account dto object
///
/// Detailed documentation here
/// Only first line is added to the description so far
/// Detailed documentation here.
/// More than the first line is added to the description as well.
struct Account {
/// Database autogenerated id
id: i64,
Expand All @@ -229,7 +229,7 @@ fn derive_struct_with_comments_success() {
};

assert_value! {account=>
"description" = r#""This is user account dto object""#, "Account description"
"description" = r#""This is user account dto object\n\nDetailed documentation here.\nMore than the first line is added to the description as well.""#, "Account description"
"properties.id.description" = r#""Database autogenerated id""#, "Account id description"
"properties.username.description" = r#""Users username""#, "Account username description"
"properties.role_ids.type" = r#""array""#, "Account role_ids type"
Expand All @@ -244,8 +244,8 @@ fn derive_enum_with_comments_success() {
let account = api_doc! {
/// This is user account status enum
///
/// Detailed documentation here
/// Only first line is added to the description so far
/// Detailed documentation here.
/// More than the first line is added to the description as well.
enum AccountStatus {
/// When user is valid to login, these enum variant level docs are omitted!!!!!
/// Since the OpenAPI spec does not have a place to put such infomation.
Expand All @@ -257,7 +257,7 @@ fn derive_enum_with_comments_success() {
};

assert_value! {account=>
"description" = r#""This is user account status enum""#, "AccountStatus description"
"description" = r#""This is user account status enum\n\nDetailed documentation here.\nMore than the first line is added to the description as well.""#, "AccountStatus description"
}
}

Expand Down Expand Up @@ -286,7 +286,7 @@ fn derive_struct_unnamed_fields_tuple_with_same_type_success() {
"type" = r#""array""#, "Point type"
"items.type" = r#""number""#, "Point items type"
"items.format" = r#""float""#, "Point items format"
"items.description" = r#""Contains x and y coordinates""#, "Point items description"
"items.description" = r#""Contains x and y coordinates\n\nCoordinates are used to pinpoint location on a map""#, "Point items description"
"maxItems" = r#"2"#, "Wrapper max items"
"minItems" = r#"2"#, "Wrapper min items"
}
Expand Down

0 comments on commit bb3f60c

Please sign in to comment.