Skip to content
Merged
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
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