From 828b7df2ac697d4b3b513d42a17cad152ad9f2e6 Mon Sep 17 00:00:00 2001 From: Maxwell Weru Date: Thu, 16 May 2024 10:28:38 +0300 Subject: [PATCH] Replace br tags in XML comments with new lines (#2899) Replace any `
` tags with a new line instead. --- .../XmlComments/XmlCommentsTextHelper.cs | 12 ++++++++++++ .../XmlComments/XmlCommentsTextHelperTests.cs | 3 ++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Swashbuckle.AspNetCore.SwaggerGen/XmlComments/XmlCommentsTextHelper.cs b/src/Swashbuckle.AspNetCore.SwaggerGen/XmlComments/XmlCommentsTextHelper.cs index bb70fab137..6383b3a36f 100644 --- a/src/Swashbuckle.AspNetCore.SwaggerGen/XmlComments/XmlCommentsTextHelper.cs +++ b/src/Swashbuckle.AspNetCore.SwaggerGen/XmlComments/XmlCommentsTextHelper.cs @@ -21,6 +21,7 @@ public static string Humanize(string text) .HumanizeCodeTags() .HumanizeMultilineCodeTags() .HumanizeParaTags() + .HumanizeBrTags() // must be called after HumanizeParaTags() so that it replaces any additional
tags .DecodeXml(); } @@ -108,6 +109,11 @@ private static string HumanizeParaTags(this string text) return ParaTag().Replace(text, (match) => "
" + match.Groups["display"].Value); } + private static string HumanizeBrTags(this string text) + { + return BrTag().Replace(text, m => Environment.NewLine); + } + private static string DecodeXml(this string text) { return WebUtility.HtmlDecode(text); @@ -118,6 +124,7 @@ private static string DecodeXml(this string text) private const string MultilineCodeTagPattern = @"(?.+?)"; private const string ParaTagPattern = @"(?.+?)"; private const string HrefPattern = @"(.*)<\/see>"; + private const string BrPattern = @"(
)"; // handles
,
,
#if NET7_0_OR_GREATER [GeneratedRegex(RefTagPattern)] @@ -134,18 +141,23 @@ private static string DecodeXml(this string text) [GeneratedRegex(HrefPattern)] private static partial Regex HrefTag(); + + [GeneratedRegex(BrPattern)] + private static partial Regex BrTag(); #else private static readonly Regex _refTag = new(RefTagPattern); private static readonly Regex _codeTag = new(CodeTagPattern); private static readonly Regex _multilineCodeTag = new(MultilineCodeTagPattern, RegexOptions.Singleline); private static readonly Regex _paraTag = new(ParaTagPattern, RegexOptions.Singleline); private static readonly Regex _hrefTag = new(HrefPattern); + private static readonly Regex _brTag = new(BrPattern); private static Regex RefTag() => _refTag; private static Regex CodeTag() => _codeTag; private static Regex MultilineCodeTag() => _multilineCodeTag; private static Regex ParaTag() => _paraTag; private static Regex HrefTag() => _hrefTag; + private static Regex BrTag() => _brTag; #endif } } diff --git a/test/Swashbuckle.AspNetCore.SwaggerGen.Test/XmlComments/XmlCommentsTextHelperTests.cs b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/XmlComments/XmlCommentsTextHelperTests.cs index 7690cd78fd..9464de28ff 100644 --- a/test/Swashbuckle.AspNetCore.SwaggerGen.Test/XmlComments/XmlCommentsTextHelperTests.cs +++ b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/XmlComments/XmlCommentsTextHelperTests.cs @@ -128,10 +128,11 @@ Misplaced Tab Indentation [InlineData(@" does something", "param1 does something")] [InlineData("DoWork is a method in TestClass.", "`DoWork` is a method in `TestClass`.")] [InlineData("DoWork is a method in \nTestClass\n.", "```DoWork``` is a method in ```\nTestClass\n```.")] - [InlineData("This is a paragraph.", "
This is a paragraph.")] + [InlineData("This is a paragraph.", "\r\nThis is a paragraph.")] [InlineData("GET /Todo?iscomplete=true&owner=mike", "GET /Todo?iscomplete=true&owner=mike")] [InlineData(@"Returns a item.", "Returns a null item.")] [InlineData(@"ISO currency code", "[ISO currency code](https://www.iso.org/iso-4217-currency-codes.html)")] + [InlineData("First line.
Second line.
Third line.
Fourth line.", "First line.\r\nSecond line.\r\nThird line.\r\nFourth line.")] public void Humanize_HumanizesInlineTags( string input, string expectedOutput)