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

Do not collapse newlines in doc comments #310

Merged
merged 1 commit into from
Aug 4, 2024
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/_includes/examples/doc_comments.schema.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "My Amazing Struct",
"description": "This struct shows off generating a schema with a custom title and description.",
"description": "This struct shows off generating a schema with\na custom title and description.",
"type": "object",
"properties": {
"my_bool": {
Expand Down Expand Up @@ -48,7 +48,7 @@
]
},
{
"description": "A struct-like enum variant which contains some floats",
"description": "A struct-like enum variant which contains\nsome floats",
"type": "object",
"properties": {
"StructVariant": {
Expand Down
4 changes: 2 additions & 2 deletions schemars/examples/doc_comments.schema.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "My Amazing Struct",
"description": "This struct shows off generating a schema with a custom title and description.",
"description": "This struct shows off generating a schema with\na custom title and description.",
"type": "object",
"properties": {
"my_bool": {
Expand Down Expand Up @@ -48,7 +48,7 @@
]
},
{
"description": "A struct-like enum variant which contains some floats",
"description": "A struct-like enum variant which contains\nsome floats",
"type": "object",
"properties": {
"StructVariant": {
Expand Down
10 changes: 4 additions & 6 deletions schemars/tests/docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ use util::*;
#[allow(dead_code)]
#[derive(JsonSchema)]
/**
*
* # This is the struct's title
*
* This is the struct's description.
*
*/
# This is the struct's title

This is the struct's description.
*/
struct MyStruct {
/// # An integer
my_int: i32,
Expand Down
4 changes: 2 additions & 2 deletions schemars/tests/expected/doc_comments_enum.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "This is the enum's title",
"description": "This is the enum's description.",
"description": "This is \n the enum's description.",
"oneOf": [
{
"type": "string",
Expand All @@ -25,7 +25,7 @@
"properties": {
"my_nullable_string": {
"title": "A nullable string",
"description": "This field is a nullable string.\n\nThis is the second line!\n\nAnd this is the third!",
"description": "This field is a nullable string.\n\n This\nis\n the second\n line!\n\n\n\n\n And this is the third!",
"type": [
"string",
"null"
Expand Down
40 changes: 4 additions & 36 deletions schemars_derive/src/attr/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,15 @@ pub fn get_title_and_desc_from_doc(attrs: &[Attribute]) -> (Option<String>, Opti
.trim_start_matches('#')
.trim()
.to_owned();
let maybe_desc = split.next().and_then(merge_description_lines);
let maybe_desc = split.next().map(|s| s.trim().to_owned());
(none_if_empty(title), maybe_desc)
} else {
(None, merge_description_lines(&doc))
(None, Some(doc))
}
}

fn merge_description_lines(doc: &str) -> Option<String> {
let desc = doc
.trim()
.split("\n\n")
.filter_map(|line| none_if_empty(line.trim().replace('\n', " ")))
.collect::<Vec<_>>()
.join("\n\n");
none_if_empty(desc)
}

fn get_doc(attrs: &[Attribute]) -> Option<String> {
let attrs = attrs
let lines = attrs
.iter()
.filter_map(|attr| {
if !attr.path().is_ident("doc") {
Expand All @@ -52,29 +42,7 @@ fn get_doc(attrs: &[Attribute]) -> Option<String> {
})
.collect::<Vec<_>>();

let mut lines = attrs
.iter()
.flat_map(|a| a.split('\n'))
.map(str::trim)
.skip_while(|s| s.is_empty())
.collect::<Vec<_>>();

if let Some(&"") = lines.last() {
lines.pop();
}

// Added for backward-compatibility, but perhaps we shouldn't do this
// https://github.com/rust-lang/rust/issues/32088
if lines.iter().all(|l| l.starts_with('*')) {
for line in lines.iter_mut() {
*line = line[1..].trim()
}
while let Some(&"") = lines.first() {
lines.remove(0);
}
};

none_if_empty(lines.join("\n"))
none_if_empty(lines.join("\n").trim().to_owned())
}

fn none_if_empty(s: String) -> Option<String> {
Expand Down
Loading