Skip to content

Commit

Permalink
Humanize multi line code tag (#3239)
Browse files Browse the repository at this point in the history
Trim text at the end and add newline if text containing `<code>\r\n` and/or `\r\n</code>` combinations.
  • Loading branch information
EvgeniyZ authored Jan 29, 2025
1 parent 5ecbb3b commit 2826dd9
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;

namespace Swashbuckle.AspNetCore.SwaggerGen
Expand All @@ -10,7 +11,7 @@ public static partial class XmlCommentsTextHelper
public static string Humanize(string text)
{
if (text == null)
throw new ArgumentNullException("text");
throw new ArgumentNullException(nameof(text));

//Call DecodeXml at last to avoid entities like &lt and &gt to break valid xml

Expand Down Expand Up @@ -101,7 +102,24 @@ private static string HumanizeCodeTags(this string text)

private static string HumanizeMultilineCodeTags(this string text)
{
return MultilineCodeTag().Replace(text, (match) => "```" + match.Groups["display"].Value + "```");
return MultilineCodeTag().Replace(text, match =>
{
var codeText = match.Groups["display"].Value;
if (LineBreaks().IsMatch(codeText))
{
var builder = new StringBuilder().Append("```");
if (!codeText.StartsWith("\r") && !codeText.StartsWith("\n"))
{
builder.AppendLine();
}

return builder.AppendLine(codeText.TrimEnd())
.Append("```")
.ToString();
}

return $"```{codeText}```";
});
}

private static string HumanizeParaTags(this string text)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,31 @@
"x-purpose": "test"
}
},
"/products/all": {
"get": {
"tags": [
"CrudActions"
],
"summary": "Get all products",
"description": "```\r\n {\r\n \"Id\":1,\r\n \"Description\":\"\",\r\n \"Status\": 0,\r\n \"Status2\": 1\r\n }\r\n```",
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Product"
}
}
}
}
}
},
"x-purpose": "test"
}
},
"/products/{id}": {
"get": {
"tags": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,31 @@
"x-purpose": "test"
}
},
"/products/all": {
"get": {
"tags": [
"CrudActions"
],
"summary": "Get all products",
"description": "```\r\n {\r\n \"Id\":1,\r\n \"Description\":\"\",\r\n \"Status\": 0,\r\n \"Status2\": 1\r\n }\r\n```",
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Product"
}
}
}
}
}
},
"x-purpose": "test"
}
},
"/products/{id}": {
"get": {
"tags": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,5 +158,54 @@ This is a paragraph.

Assert.Equal("\r\nThis is a paragraph. MultiLined.\r\n\r\nThis is a paragraph.", output, false, true);
}

[Fact]
public void Humanize_CodeMultiLineTag()
{
const string input = @"
<code>
{
""Prop1"":1,
""Prop2"":[]
}
</code>";

var output = XmlCommentsTextHelper.Humanize(input);

var expected = string.Join("\r\n",
[
"```",
" {",
" \"Prop1\":1,",
" \"Prop2\":[]",
" }",
"```"
]);
Assert.Equal(expected, output, false, true);
}

[Fact]
public void Humanize_CodeMultiLineTag_OnSameLine()
{
const string input = @"
<code>{
""Prop1"":1,
""Prop2"":[]
}
</code>";

var output = XmlCommentsTextHelper.Humanize(input);

var expected = string.Join("\r\n",
[
"```",
"{",
" \"Prop1\":1,",
" \"Prop2\":[]",
" }",
"```"
]);
Assert.Equal(expected, output, false, true);
}
}
}
17 changes: 17 additions & 0 deletions test/WebSites/Basic/Controllers/CrudActionsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,23 @@ public Product Create([FromBody, Required]Product product)
return product;
}

/// <summary>Get all products</summary>
/// <remarks>
/// <code>
/// {
/// "Id":1,
/// "Description":"",
/// "Status": 0,
/// "Status2": 1
/// }
/// </code>
/// </remarks>
[HttpGet("all")]
public List<Product> GetAll()
{
return [];
}

/// <summary>
/// Searches the collection of products by description key words
/// </summary>
Expand Down

0 comments on commit 2826dd9

Please sign in to comment.