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

Option to preserve formatting #45

Closed
wants to merge 3 commits into from
Closed
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
15 changes: 15 additions & 0 deletions schemars/tests/docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ pub enum MyEnum {
},
}

/// This
/// is
/// not
/// a
/// single
/// line.
#[derive(JsonSchema)]
#[schemars_preserve_doc_formatting]
pub struct MyPreserveFormattingStruct;

#[test]
fn doc_comments_struct() -> TestResult {
test_default_generated_schema::<MyStruct>("doc_comments_struct")
Expand All @@ -67,6 +77,11 @@ fn doc_comments_enum() -> TestResult {
test_default_generated_schema::<MyEnum>("doc_comments_enum")
}

#[test]
fn doc_comments_preserve_formatting() -> TestResult {
test_default_generated_schema::<MyPreserveFormattingStruct>("doc_comments_preserve_formatting")
}

/// # OverrideDocs struct
/// This description should be overridden
#[derive(Debug, JsonSchema)]
Expand Down
6 changes: 6 additions & 0 deletions schemars/tests/expected/doc_comments_preserve_formatting.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "MyPreserveFormattingStruct",
"description": "This\nis\nnot\n a\nsingle\nline.",
"type": "null"
}
48 changes: 35 additions & 13 deletions schemars_derive/src/attr/doc.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
use syn::{Attribute, Lit::Str, Meta::NameValue, MetaNameValue};

pub fn get_title_and_desc_from_doc(attrs: &[Attribute]) -> (Option<String>, Option<String>) {
let doc = match get_doc(attrs) {
let preserve_formatting = should_preserve_formatting(attrs);

let doc = match get_doc(attrs, preserve_formatting) {
None => return (None, None),
Some(doc) => doc,
};

if doc.starts_with('#') {
let (title, mut maybe_desc) = if doc.starts_with('#') {
let mut split = doc.splitn(2, '\n');
let title = split
.next()
.unwrap()
.trim_start_matches('#')
.trim()
.to_owned();
let maybe_desc = split.next().and_then(merge_description_lines);
(none_if_empty(title), maybe_desc)
(none_if_empty(title), split.next().map(ToOwned::to_owned))
} else {
(None, merge_description_lines(&doc))
(None, Some(doc))
};

if !preserve_formatting {
maybe_desc = maybe_desc
.as_ref()
.map(String::as_str)
.and_then(merge_description_lines);
}

(title, maybe_desc)
}

fn merge_description_lines(doc: &str) -> Option<String> {
Expand All @@ -31,7 +41,13 @@ fn merge_description_lines(doc: &str) -> Option<String> {
none_if_empty(desc)
}

fn get_doc(attrs: &[Attribute]) -> Option<String> {
fn should_preserve_formatting(attrs: &[Attribute]) -> bool {
attrs
.iter()
.any(|attr| attr.path.is_ident("schemars_preserve_doc_formatting"))
}

fn get_doc(attrs: &[Attribute], preserve_formatting: bool) -> Option<String> {
let doc = attrs
.iter()
.filter_map(|attr| {
Expand All @@ -46,13 +62,19 @@ fn get_doc(attrs: &[Attribute]) -> Option<String> {

None
})
.collect::<Vec<_>>()
.iter()
.flat_map(|a| a.split('\n'))
.map(str::trim)
.skip_while(|s| *s == "")
.collect::<Vec<_>>()
.join("\n");
.collect::<Vec<_>>();

let doc = if !preserve_formatting {
doc.iter()
.flat_map(|a| a.split('\n'))
.map(str::trim)
.skip_while(|s| *s == "")
.collect::<Vec<_>>()
.join("\n")
} else {
doc.join("\n")
};

none_if_empty(doc)
}

Expand Down
5 changes: 4 additions & 1 deletion schemars_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ mod schema_exprs;
use ast::*;
use proc_macro2::TokenStream;

#[proc_macro_derive(JsonSchema, attributes(schemars, serde))]
#[proc_macro_derive(
JsonSchema,
attributes(schemars, serde, schemars_preserve_doc_formatting)
)]
pub fn derive_json_schema_wrapper(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input = parse_macro_input!(input as syn::DeriveInput);
derive_json_schema(input).into()
Expand Down