Repository: GREsau/schemars
Pull request #411 (2025-05-30 21:04:16)

Trim single space from doc comments
Resolves https://github.com/GREsau/schemars/issues/407


Patch:
diff --git a/schemars_derive/src/attr/doc.rs b/schemars_derive/src/attr/doc.rs
index 778d493..1d126d5 100644
--- a/schemars_derive/src/attr/doc.rs
+++ b/schemars_derive/src/attr/doc.rs
@@ -1,16 +1,33 @@
 use proc_macro2::TokenStream;
-use quote::TokenStreamExt;
-use syn::Attribute;
+use quote::ToTokens;
+use syn::{Attribute, Expr, ExprLit, Lit};
 
-pub fn get_doc(attrs: &[Attribute]) -> Option<syn::Expr> {
+pub fn get_doc(attrs: &[Attribute]) -> Option<Expr> {
     let mut macro_args: TokenStream = TokenStream::new();
 
-    let lines = attrs
+    for (i, line) in attrs
         .iter()
         .filter(|a| a.path().is_ident("doc"))
         .flat_map(|a| a.meta.require_name_value())
-        .map(|m| &m.value);
-    macro_args.append_separated(lines, quote!(, "\n",));
+        .enumerate()
+    {
+        if i > 0 {
+            macro_args.extend([quote!(, "\n",)]);
+        }
+
+        if let Expr::Lit(ExprLit {
+            lit: Lit::Str(lit_str),
+            ..
+        }) = &line.value
+        {
+            if let Some(trimmed) = lit_str.value().strip_prefix(' ') {
+                trimmed.to_tokens(&mut macro_args);
+                continue;
+            }
+        }
+
+        line.value.to_tokens(&mut macro_args);
+    }
 
     if macro_args.is_empty() {
         None


Test patch:
diff --git a/schemars/tests/integration/snapshots/schemars/tests/integration/docs.rs~doc_comments_enum.de.json b/schemars/tests/integration/snapshots/schemars/tests/integration/docs.rs~doc_comments_enum.de.json
index 0da3bed..aa6d047 100644
--- a/schemars/tests/integration/snapshots/schemars/tests/integration/docs.rs~doc_comments_enum.de.json
+++ b/schemars/tests/integration/snapshots/schemars/tests/integration/docs.rs~doc_comments_enum.de.json
@@ -1,7 +1,7 @@
 {
   "$schema": "https://json-schema.org/draft/2020-12/schema",
   "title": "This is the enum's title",
-  "description": "This is \n the enum's description.",
+  "description": "This is \nthe 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\n This\nis\n   the second\n  line!\n\n\n\n\n And this is the third!",
+              "description": "This field is a nullable string.\n\nThis\nis\n  the second\n line!\n\n\n\n\nAnd this is the third!",
               "type": [
                 "string",
                 "null"
diff --git a/schemars/tests/integration/snapshots/schemars/tests/integration/docs.rs~doc_comments_enum.ser.json b/schemars/tests/integration/snapshots/schemars/tests/integration/docs.rs~doc_comments_enum.ser.json
index 3c6e680..6341d24 100644
--- a/schemars/tests/integration/snapshots/schemars/tests/integration/docs.rs~doc_comments_enum.ser.json
+++ b/schemars/tests/integration/snapshots/schemars/tests/integration/docs.rs~doc_comments_enum.ser.json
@@ -1,7 +1,7 @@
 {
   "$schema": "https://json-schema.org/draft/2020-12/schema",
   "title": "This is the enum's title",
-  "description": "This is \n the enum's description.",
+  "description": "This is \nthe 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\n This\nis\n   the second\n  line!\n\n\n\n\n And this is the third!",
+              "description": "This field is a nullable string.\n\nThis\nis\n  the second\n line!\n\n\n\n\nAnd this is the third!",
               "type": [
                 "string",
                 "null"