From 9b8505a92a2c6a554a0d14f815bdae857308d3db Mon Sep 17 00:00:00 2001 From: Marko Lahma Date: Tue, 14 Dec 2021 20:41:01 +0200 Subject: [PATCH] Fix cshardocs multi-line regression --- .../GeneralGeneratorTests.cs | 61 ++++++++++++++++++- .../Templates/Class.liquid | 8 ++- .../Templates/Enum.liquid | 4 +- src/NJsonSchema/ConversionUtilities.cs | 16 ++--- 4 files changed, 72 insertions(+), 17 deletions(-) diff --git a/src/NJsonSchema.CodeGeneration.CSharp.Tests/GeneralGeneratorTests.cs b/src/NJsonSchema.CodeGeneration.CSharp.Tests/GeneralGeneratorTests.cs index e85ba28c2..cff1db46e 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp.Tests/GeneralGeneratorTests.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp.Tests/GeneralGeneratorTests.cs @@ -293,7 +293,11 @@ public async Task When_enum_has_description_then_csharp_has_xml_comment() var output = generator.GenerateFile("MyClass"); //// Assert - Assert.Contains(@"/// EnumDesc.", output); + var summary = @" + /// + /// EnumDesc. + /// ".Replace("\r", "").Trim(); + Assert.Contains(summary, output); AssertCompile(output); } @@ -310,7 +314,11 @@ public async Task When_class_has_description_then_csharp_has_xml_comment() var output = generator.GenerateFile("MyClass"); //// Assert - Assert.Contains(@"/// ClassDesc.", output); + var summary = @" + /// + /// ClassDesc. + /// ".Replace("\r", ""); + Assert.Contains(summary, output); AssertCompile(output); } @@ -327,7 +335,11 @@ public async Task When_property_has_description_then_csharp_has_xml_comment() var output = generator.GenerateFile("MyClass"); //// Assert - Assert.Contains(@"/// PropertyDesc.", output); + var summary = @" + /// + /// PropertyDesc. + /// ".Replace("\r", "").Trim(); + Assert.Contains(summary, output); AssertCompile(output); } @@ -1980,5 +1992,48 @@ public static Person FromJson(string data) AssertCompile(output); } + + public class DocumentationTest + { + /// + /// Summary is here + /// + /// spanning multiple lines + /// + /// like this. + /// + /// + public string HelloMessage { get; set; } + } + + [Fact] + public async Task When_documentation_present_produces_valid_xml_documentation_syntax() + { + // Arrange + var schema = JsonSchema.FromType(); + + // Act + var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings + { + ClassStyle = CSharpClassStyle.Poco + }); + + var code = generator.GenerateFile("MyClass"); + + // Assert + var expected = @" + /// + /// Summary is here + ///
+ ///
spanning multiple lines + ///
+ ///
like this. + ///
+ ///
".Replace("\r",""); + + Assert.Contains(expected, code); + + AssertCompile(code); + } } } diff --git a/src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.liquid b/src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.liquid index 67f24c90b..e70dfdb1d 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.liquid +++ b/src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.liquid @@ -1,5 +1,7 @@ {%- if HasDescription -%} -/// {{ Description | csharpdocs }} +/// +/// {{ Description | csharpdocs }} +/// {%- endif -%} {%- if HasDiscriminator -%} {%- if UseSystemTextJson -%} @@ -45,7 +47,9 @@ {%- endif -%} {%- for property in Properties -%} {%- if property.HasDescription -%} - /// {{ property.Description | csharpdocs }} + /// + /// {{ property.Description | csharpdocs }} + /// {%- endif -%} {%- if UseSystemTextJson %} [System.Text.Json.Serialization.JsonPropertyName("{{ property.Name }}")] diff --git a/src/NJsonSchema.CodeGeneration.CSharp/Templates/Enum.liquid b/src/NJsonSchema.CodeGeneration.CSharp/Templates/Enum.liquid index 6f74e42e2..56401d8d3 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp/Templates/Enum.liquid +++ b/src/NJsonSchema.CodeGeneration.CSharp/Templates/Enum.liquid @@ -1,5 +1,7 @@ {%- if HasDescription -%} -/// {{ Description | csharpdocs }} +/// +/// {{ Description | csharpdocs }} +/// {%- endif -%} [System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "{{ ToolchainVersion }}")] {%- if IsEnumAsBitFlags -%} diff --git a/src/NJsonSchema/ConversionUtilities.cs b/src/NJsonSchema/ConversionUtilities.cs index d8cb68685..dd1ae7e2d 100644 --- a/src/NJsonSchema/ConversionUtilities.cs +++ b/src/NJsonSchema/ConversionUtilities.cs @@ -270,19 +270,13 @@ private static void AddPrefixToBeginningOfNonEmptyLines(string input, string tab /// The output. public static string ConvertCSharpDocs(string input, int tabCount) { - if (input is null) - { - return ""; - } - var tabString = CreateTabString(tabCount); - input = input - .Replace("\r", string.Empty); - - var stringWriter = new StringWriter(new StringBuilder(input.Length), CultureInfo.CurrentCulture); - AddPrefixToBeginningOfNonEmptyLines(input, tabString + "/// ", stringWriter); + input = input? + .Replace("\r", string.Empty) + .Replace("\n", "\n" + string.Join("", Enumerable.Repeat(" ", tabCount)) + "/// ") + ?? string.Empty; // TODO: Support more markdown features here - var xml = new XText(stringWriter.ToString()).ToString(); + var xml = new XText(input).ToString(); return Regex.Replace(xml, @"^( *)/// ", m => m.Groups[1] + "///
", RegexOptions.Multiline); }