Repository: GREsau/schemars
Pull request #310 (2024-08-04 15:43:11)

Do not collapse newlines in doc comments
Related issue: #120 

This is quite a significant change in behaviour so I don't think can reasonably be applied as a non-breaking change


Patch:
diff --git a/docs/_includes/examples/doc_comments.schema.json b/docs/_includes/examples/doc_comments.schema.json
index 417d3be..4997ac5 100644
--- a/docs/_includes/examples/doc_comments.schema.json
+++ b/docs/_includes/examples/doc_comments.schema.json
@@ -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": {
@@ -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": {
diff --git a/schemars/examples/doc_comments.schema.json b/schemars/examples/doc_comments.schema.json
index 417d3be..4997ac5 100644
--- a/schemars/examples/doc_comments.schema.json
+++ b/schemars/examples/doc_comments.schema.json
@@ -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": {
@@ -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": {
diff --git a/schemars_derive/src/attr/doc.rs b/schemars_derive/src/attr/doc.rs
index aab15d2..df9daca 100644
--- a/schemars_derive/src/attr/doc.rs
+++ b/schemars_derive/src/attr/doc.rs
@@ -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") {
@@ -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> {


Test patch:
diff --git a/schemars/tests/docs.rs b/schemars/tests/docs.rs
index 788140d..7d1d6d8 100644
--- a/schemars/tests/docs.rs
+++ b/schemars/tests/docs.rs
@@ -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,
diff --git a/schemars/tests/expected/doc_comments_enum.json b/schemars/tests/expected/doc_comments_enum.json
index b2e8f94..0da3bed 100644
--- a/schemars/tests/expected/doc_comments_enum.json
+++ b/schemars/tests/expected/doc_comments_enum.json
@@ -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",
@@ -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"