mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-01-11 22:30:41 +01:00
Code generator improvements (#934)
* Handle new line escaping in C# mapping code generator * Prevent date conversion when value persisted as string * Handle object properties named as csharp keywords * Refactor: Extract logic responsible for generating anonymous object definition to a separate class
This commit is contained in:
@@ -5,7 +5,6 @@ using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Stef.Validation;
|
||||
using WireMock.Admin.Mappings;
|
||||
using WireMock.Constants;
|
||||
@@ -19,6 +18,7 @@ using WireMock.Settings;
|
||||
using WireMock.Types;
|
||||
using WireMock.Util;
|
||||
|
||||
using static WireMock.Util.CSharpFormatter;
|
||||
namespace WireMock.Serialization;
|
||||
|
||||
internal class MappingConverter
|
||||
@@ -150,18 +150,17 @@ internal class MappingConverter
|
||||
{
|
||||
case BodyType.String:
|
||||
case BodyType.FormUrlEncoded:
|
||||
sb.AppendLine($" .WithBody(\"{EscapeCSharpString(bodyData.BodyAsString)}\")");
|
||||
sb.AppendLine($" .WithBody({ToCSharpStringLiteral(bodyData.BodyAsString)})");
|
||||
break;
|
||||
case BodyType.Json:
|
||||
if (bodyData.BodyAsJson is string bodyStringValue)
|
||||
{
|
||||
sb.AppendLine($" .WithBody(\"{EscapeCSharpString(bodyStringValue)}\")");
|
||||
sb.AppendLine($" .WithBody({ToCSharpStringLiteral(bodyStringValue)})");
|
||||
}
|
||||
else
|
||||
else if(bodyData.BodyAsJson is {} jsonBody)
|
||||
{
|
||||
var serializedBody = JsonConvert.SerializeObject(bodyData.BodyAsJson);
|
||||
var deserializedBody = JToken.Parse(serializedBody);
|
||||
sb.AppendLine($" .WithBodyAsJson({ConvertJsonToAnonymousObjectDefinition(deserializedBody, 2)})");
|
||||
var anonymousObjectDefinition = ConvertToAnonymousObjectDefinition(jsonBody);
|
||||
sb.AppendLine($" .WithBodyAsJson({anonymousObjectDefinition})");
|
||||
}
|
||||
|
||||
break;
|
||||
@@ -406,7 +405,7 @@ internal class MappingConverter
|
||||
|
||||
private static string GetString(IStringMatcher stringMatcher)
|
||||
{
|
||||
return stringMatcher.GetPatterns().Select(p => $"\"{p.GetPattern()}\"").First();
|
||||
return stringMatcher.GetPatterns().Select(p => ToCSharpStringLiteral(p.GetPattern())).First();
|
||||
}
|
||||
|
||||
private static string[] GetStringArray(IReadOnlyList<IStringMatcher> stringMatchers)
|
||||
@@ -459,11 +458,9 @@ internal class MappingConverter
|
||||
|
||||
private static string ToValueArguments(string[]? values, string defaultValue = "")
|
||||
{
|
||||
return values is { } ? string.Join(", ", values.Select(v => $"\"{EscapeCSharpString(v)}\"")) : $"\"{EscapeCSharpString(defaultValue)}\"";
|
||||
return values is { } ? string.Join(", ", values.Select(ToCSharpStringLiteral)) : ToCSharpStringLiteral(defaultValue);
|
||||
}
|
||||
|
||||
private static string? EscapeCSharpString(string? value) => value?.Replace("\"", "\\\"");
|
||||
|
||||
private static WebProxyModel? MapWebProxy(WebProxySettings? settings)
|
||||
{
|
||||
return settings != null ? new WebProxyModel
|
||||
@@ -493,48 +490,5 @@ internal class MappingConverter
|
||||
return newDictionary;
|
||||
}
|
||||
|
||||
private static string ConvertJsonToAnonymousObjectDefinition(JToken token, int ind = 0)
|
||||
{
|
||||
return token switch
|
||||
{
|
||||
JArray jArray => FormatArray(jArray, ind),
|
||||
JObject jObject => FormatObject(jObject, ind),
|
||||
JProperty jProperty => $"{jProperty.Name} = {ConvertJsonToAnonymousObjectDefinition(jProperty.Value, ind)}",
|
||||
JValue jValue => jValue.Type switch
|
||||
{
|
||||
JTokenType.None => "null",
|
||||
JTokenType.Integer => jValue.Value?.ToString() ?? "null",
|
||||
JTokenType.Float => jValue.Value?.ToString() ?? "null",
|
||||
JTokenType.String => $"\"{EscapeCSharpString(jValue.Value?.ToString())}\"",
|
||||
JTokenType.Boolean => jValue.Value?.ToString()?.ToLower() ?? "null",
|
||||
JTokenType.Null => "null",
|
||||
JTokenType.Undefined => "null",
|
||||
_ => $"UNHANDLED_CASE: {jValue.Type}"
|
||||
},
|
||||
_ => $"UNHANDLED_CASE: {token}"
|
||||
};
|
||||
}
|
||||
|
||||
private static string FormatObject(JObject jObject, int ind)
|
||||
{
|
||||
var indStr = new string(' ', 4 * ind);
|
||||
var indStrSub = new string(' ', 4 * (ind + 1));
|
||||
var items = jObject.Properties().Select(x => ConvertJsonToAnonymousObjectDefinition(x, ind + 1));
|
||||
|
||||
return $"new\r\n{indStr}{{\r\n{indStrSub}{string.Join($",\r\n{indStrSub}", items)}\r\n{indStr}}}";
|
||||
}
|
||||
|
||||
private static string FormatArray(JArray jArray, int ind)
|
||||
{
|
||||
var hasComplexItems = jArray.FirstOrDefault() is JObject or JArray;
|
||||
var items = jArray.Select(x => ConvertJsonToAnonymousObjectDefinition(x, hasComplexItems ? ind + 1 : ind));
|
||||
if (hasComplexItems)
|
||||
{
|
||||
var indStr = new string(' ', 4 * ind);
|
||||
var indStrSub = new string(' ', 4 * (ind + 1));
|
||||
return $"new []\r\n{indStr}{{\r\n{indStrSub}{string.Join($",\r\n{indStrSub}", items)}\r\n{indStr}}}";
|
||||
}
|
||||
|
||||
return $"new [] {{ {string.Join(", ", items)} }}";
|
||||
}
|
||||
}
|
||||
167
src/WireMock.Net/Util/CSharpFormatter.cs
Normal file
167
src/WireMock.Net/Util/CSharpFormatter.cs
Normal file
@@ -0,0 +1,167 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace WireMock.Util;
|
||||
|
||||
internal static class CSharpFormatter
|
||||
{
|
||||
public static object ConvertToAnonymousObjectDefinition(object jsonBody)
|
||||
{
|
||||
var serializedBody = JsonConvert.SerializeObject(jsonBody);
|
||||
using var jsonReader = new JsonTextReader(new StringReader(serializedBody));
|
||||
jsonReader.DateParseHandling = DateParseHandling.None;
|
||||
var deserializedBody = JObject.Load(jsonReader);
|
||||
|
||||
var anonymousObjectDefinition = ConvertJsonToAnonymousObjectDefinition(deserializedBody, 2);
|
||||
return anonymousObjectDefinition;
|
||||
}
|
||||
|
||||
private static string ConvertJsonToAnonymousObjectDefinition(JToken token, int ind = 0)
|
||||
{
|
||||
return token switch
|
||||
{
|
||||
JArray jArray => FormatArray(jArray, ind),
|
||||
JObject jObject => FormatObject(jObject, ind),
|
||||
JProperty jProperty =>
|
||||
$"{FormatPropertyName(jProperty.Name)} = {ConvertJsonToAnonymousObjectDefinition(jProperty.Value, ind)}",
|
||||
JValue jValue => jValue.Type switch
|
||||
{
|
||||
JTokenType.None => "null",
|
||||
JTokenType.Integer => jValue.Value?.ToString() ?? "null",
|
||||
JTokenType.Float => jValue.Value?.ToString() ?? "null",
|
||||
JTokenType.String => ToCSharpStringLiteral(jValue.Value?.ToString()),
|
||||
JTokenType.Boolean => jValue.Value?.ToString()?.ToLower() ?? "null",
|
||||
JTokenType.Null => "null",
|
||||
JTokenType.Undefined => "null",
|
||||
JTokenType.Date when jValue.Value is DateTime dateValue =>
|
||||
$"DateTime.Parse({ToCSharpStringLiteral(dateValue.ToString("s"))})",
|
||||
_ => $"UNHANDLED_CASE: {jValue.Type}"
|
||||
},
|
||||
_ => $"UNHANDLED_CASE: {token}"
|
||||
};
|
||||
}
|
||||
|
||||
public static string ToCSharpStringLiteral(string? value)
|
||||
{
|
||||
var escapedValue = value?.Replace("\"", "\\\"") ?? string.Empty;
|
||||
if (escapedValue.Contains("\n"))
|
||||
{
|
||||
return $"@\"{escapedValue}\"";
|
||||
}
|
||||
|
||||
return $"\"{escapedValue}\"";
|
||||
}
|
||||
|
||||
private static string FormatPropertyName(string propertyName)
|
||||
{
|
||||
return CsharpKeywords.Contains(propertyName) ? "@" + propertyName : propertyName;
|
||||
}
|
||||
|
||||
private static string FormatObject(JObject jObject, int ind)
|
||||
{
|
||||
var indStr = new string(' ', 4 * ind);
|
||||
var indStrSub = new string(' ', 4 * (ind + 1));
|
||||
var items = jObject.Properties().Select(x => ConvertJsonToAnonymousObjectDefinition(x, ind + 1));
|
||||
|
||||
return $"new\r\n{indStr}{{\r\n{indStrSub}{string.Join($",\r\n{indStrSub}", items)}\r\n{indStr}}}";
|
||||
}
|
||||
|
||||
private static string FormatArray(JArray jArray, int ind)
|
||||
{
|
||||
var hasComplexItems = jArray.FirstOrDefault() is JObject or JArray;
|
||||
var items = jArray.Select(x => ConvertJsonToAnonymousObjectDefinition(x, hasComplexItems ? ind + 1 : ind));
|
||||
if (hasComplexItems)
|
||||
{
|
||||
var indStr = new string(' ', 4 * ind);
|
||||
var indStrSub = new string(' ', 4 * (ind + 1));
|
||||
return $"new []\r\n{indStr}{{\r\n{indStrSub}{string.Join($",\r\n{indStrSub}", items)}\r\n{indStr}}}";
|
||||
}
|
||||
|
||||
return $"new [] {{ {string.Join(", ", items)} }}";
|
||||
}
|
||||
|
||||
private static readonly HashSet<string> CsharpKeywords = new HashSet<string>(new[]
|
||||
{
|
||||
"abstract",
|
||||
"as",
|
||||
"base",
|
||||
"bool",
|
||||
"break",
|
||||
"byte",
|
||||
"case",
|
||||
"catch",
|
||||
"char",
|
||||
"checked",
|
||||
"class",
|
||||
"const",
|
||||
"continue",
|
||||
"decimal",
|
||||
"default",
|
||||
"delegate",
|
||||
"do",
|
||||
"double",
|
||||
"else",
|
||||
"enum",
|
||||
"event",
|
||||
"explicit",
|
||||
"extern",
|
||||
"false",
|
||||
"finally",
|
||||
"fixed",
|
||||
"float",
|
||||
"for",
|
||||
"foreach",
|
||||
"goto",
|
||||
"if",
|
||||
"implicit",
|
||||
"in",
|
||||
"int",
|
||||
"interface",
|
||||
"internal",
|
||||
"is",
|
||||
"lock",
|
||||
"long",
|
||||
"namespace",
|
||||
"new",
|
||||
"null",
|
||||
"object",
|
||||
"operator",
|
||||
"out",
|
||||
"override",
|
||||
"params",
|
||||
"private",
|
||||
"protected",
|
||||
"public",
|
||||
"readonly",
|
||||
"ref",
|
||||
"return",
|
||||
"sbyte",
|
||||
"sealed",
|
||||
"short",
|
||||
"sizeof",
|
||||
"stackalloc",
|
||||
"static",
|
||||
"string",
|
||||
"struct",
|
||||
"switch",
|
||||
"this",
|
||||
"throw",
|
||||
"true",
|
||||
"try",
|
||||
"typeof",
|
||||
"uint",
|
||||
"ulong",
|
||||
"unchecked",
|
||||
"unsafe",
|
||||
"ushort",
|
||||
"using",
|
||||
"virtual",
|
||||
"void",
|
||||
"volatile",
|
||||
"while"
|
||||
});
|
||||
}
|
||||
@@ -36,7 +36,7 @@ server
|
||||
.WithStatusCode(208)
|
||||
.WithBodyAsJson(new
|
||||
{
|
||||
a = 1,
|
||||
@as = 1,
|
||||
b = 1.2,
|
||||
d = true,
|
||||
e = false,
|
||||
@@ -59,7 +59,14 @@ server
|
||||
b = 3
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
date_field = "2023-05-08T11:20:19",
|
||||
string_field_with_date = "2021-03-13T21:04:00Z",
|
||||
multiline_text = @"This
|
||||
is
|
||||
multiline
|
||||
text
|
||||
"
|
||||
})
|
||||
);
|
||||
|
||||
|
||||
@@ -762,7 +762,11 @@ public class WireMockAdminApiTests
|
||||
.RespondWith(
|
||||
Response.Create()
|
||||
.WithStatusCode(HttpStatusCode.AlreadyReported)
|
||||
.WithBodyAsJson(new { a = 1, b=1.2, d=true, e=false, f=new[]{1,2,3,4}, g= new{z1=1, z2=2, z3=new []{"a","b","c"}, z4=new[]{new {a=1, b=2},new {a=2, b=3}}} })
|
||||
.WithBodyAsJson(new { @as = 1, b=1.2, d=true, e=false, f=new[]{1,2,3,4}, g= new{z1=1, z2=2, z3=new []{"a","b","c"}, z4=new[]{new {a=1, b=2},new {a=2, b=3}}}, date_field = new DateTime(2023,05,08,11,20,19), string_field_with_date="2021-03-13T21:04:00Z", multiline_text= @"This
|
||||
is
|
||||
multiline
|
||||
text
|
||||
" })
|
||||
);
|
||||
|
||||
// Act
|
||||
|
||||
Reference in New Issue
Block a user