Skip to content

Commit

Permalink
Fix cshardocs multi-line regression
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma committed Dec 14, 2021
1 parent 76afab6 commit 9b8505a
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,11 @@ public async Task When_enum_has_description_then_csharp_has_xml_comment()
var output = generator.GenerateFile("MyClass");

//// Assert
Assert.Contains(@"/// <summary>EnumDesc.</summary>", output);
var summary = @"
/// <summary>
/// EnumDesc.
/// </summary>".Replace("\r", "").Trim();
Assert.Contains(summary, output);

AssertCompile(output);
}
Expand All @@ -310,7 +314,11 @@ public async Task When_class_has_description_then_csharp_has_xml_comment()
var output = generator.GenerateFile("MyClass");

//// Assert
Assert.Contains(@"/// <summary>ClassDesc.</summary>", output);
var summary = @"
/// <summary>
/// ClassDesc.
/// </summary>".Replace("\r", "");
Assert.Contains(summary, output);

AssertCompile(output);
}
Expand All @@ -327,7 +335,11 @@ public async Task When_property_has_description_then_csharp_has_xml_comment()
var output = generator.GenerateFile("MyClass");

//// Assert
Assert.Contains(@"/// <summary>PropertyDesc.</summary>", output);
var summary = @"
/// <summary>
/// PropertyDesc.
/// </summary>".Replace("\r", "").Trim();
Assert.Contains(summary, output);

AssertCompile(output);
}
Expand Down Expand Up @@ -1980,5 +1992,48 @@ public static Person FromJson(string data)

AssertCompile(output);
}

public class DocumentationTest
{
/// <summary>
/// Summary is here
///
/// spanning multiple lines
///
/// like this.
///
/// </summary>
public string HelloMessage { get; set; }
}

[Fact]
public async Task When_documentation_present_produces_valid_xml_documentation_syntax()
{
// Arrange
var schema = JsonSchema.FromType<DocumentationTest>();

// Act
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings
{
ClassStyle = CSharpClassStyle.Poco
});

var code = generator.GenerateFile("MyClass");

// Assert
var expected = @"
/// <summary>
/// Summary is here
/// <br/>
/// <br/>spanning multiple lines
/// <br/>
/// <br/>like this.
/// <br/>
/// </summary>".Replace("\r","");

Assert.Contains(expected, code);

AssertCompile(code);
}
}
}
8 changes: 6 additions & 2 deletions src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.liquid
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{%- if HasDescription -%}
/// <summary>{{ Description | csharpdocs }}</summary>
/// <summary>
/// {{ Description | csharpdocs }}
/// </summary>
{%- endif -%}
{%- if HasDiscriminator -%}
{%- if UseSystemTextJson -%}
Expand Down Expand Up @@ -45,7 +47,9 @@
{%- endif -%}
{%- for property in Properties -%}
{%- if property.HasDescription -%}
/// <summary>{{ property.Description | csharpdocs }}</summary>
/// <summary>
/// {{ property.Description | csharpdocs }}
/// </summary>
{%- endif -%}
{%- if UseSystemTextJson %}
[System.Text.Json.Serialization.JsonPropertyName("{{ property.Name }}")]
Expand Down
4 changes: 3 additions & 1 deletion src/NJsonSchema.CodeGeneration.CSharp/Templates/Enum.liquid
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{%- if HasDescription -%}
/// <summary>{{ Description | csharpdocs }}</summary>
/// <summary>
/// {{ Description | csharpdocs }}
/// </summary>
{%- endif -%}
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "{{ ToolchainVersion }}")]
{%- if IsEnumAsBitFlags -%}
Expand Down
16 changes: 5 additions & 11 deletions src/NJsonSchema/ConversionUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -270,19 +270,13 @@ private static void AddPrefixToBeginningOfNonEmptyLines(string input, string tab
/// <returns>The output.</returns>
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] + "/// <br/>", RegexOptions.Multiline);
}

Expand Down

0 comments on commit 9b8505a

Please sign in to comment.