mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-22 17:19:00 +01:00
Code generator improvements (#940)
* Fix quotation marks escaping in multiline string * Add support for JsonPartialMatcher and JsonPartialWildcardMatcher in mapping code generator
This commit is contained in:
@@ -107,11 +107,28 @@ internal class MappingConverter
|
||||
|
||||
if (bodyMatcher is { Matchers: { } })
|
||||
{
|
||||
var wildcardMatcher = bodyMatcher.Matchers.OfType<WildcardMatcher>().FirstOrDefault();
|
||||
if (wildcardMatcher is { } && wildcardMatcher.GetPatterns().Any())
|
||||
if (bodyMatcher.Matchers.OfType<WildcardMatcher>().FirstOrDefault() is { } wildcardMatcher && wildcardMatcher.GetPatterns().Any())
|
||||
{
|
||||
sb.AppendLine($" .WithBody({GetString(wildcardMatcher)})");
|
||||
}
|
||||
else if (bodyMatcher.Matchers.OfType<JsonPartialMatcher>().FirstOrDefault() is { Value: { } } jsonPartialMatcher)
|
||||
{
|
||||
sb.AppendLine(@$" .WithBody(new JsonPartialMatcher(
|
||||
value: {ToCSharpStringLiteral(jsonPartialMatcher.Value.ToString())},
|
||||
ignoreCase: {ToCSharpBooleanLiteral(jsonPartialMatcher.IgnoreCase)},
|
||||
throwException: {ToCSharpBooleanLiteral(jsonPartialMatcher.ThrowException)},
|
||||
regex: {ToCSharpBooleanLiteral(jsonPartialMatcher.Regex)}
|
||||
))");
|
||||
}
|
||||
else if (bodyMatcher.Matchers.OfType<JsonPartialWildcardMatcher>().FirstOrDefault() is { Value: { } } jsonPartialWildcardMatcher)
|
||||
{
|
||||
sb.AppendLine(@$" .WithBody(new JsonPartialWildcardMatcher(
|
||||
value: {ToCSharpStringLiteral(jsonPartialWildcardMatcher.Value.ToString())},
|
||||
ignoreCase: {ToCSharpBooleanLiteral(jsonPartialWildcardMatcher.IgnoreCase)},
|
||||
throwException: {ToCSharpBooleanLiteral(jsonPartialWildcardMatcher.ThrowException)},
|
||||
regex: {ToCSharpBooleanLiteral(jsonPartialWildcardMatcher.Regex)}
|
||||
))");
|
||||
}
|
||||
}
|
||||
|
||||
sb.AppendLine(@" )");
|
||||
|
||||
@@ -129,15 +129,25 @@ internal static class CSharpFormatter
|
||||
};
|
||||
}
|
||||
|
||||
public static string ToCSharpBooleanLiteral(bool value) => value ? "true" : "false";
|
||||
|
||||
public static string ToCSharpStringLiteral(string? value)
|
||||
{
|
||||
var escapedValue = value?.Replace("\"", "\\\"") ?? string.Empty;
|
||||
if (escapedValue.Contains("\n"))
|
||||
if (string.IsNullOrEmpty(value))
|
||||
{
|
||||
return $"@\"{escapedValue}\"";
|
||||
return "\"\"";
|
||||
}
|
||||
|
||||
return $"\"{escapedValue}\"";
|
||||
if (value.Contains("\n"))
|
||||
{
|
||||
var escapedValue = value?.Replace("\"", "\"\"") ?? string.Empty;
|
||||
return $"@\"{escapedValue}\"";
|
||||
}
|
||||
else
|
||||
{
|
||||
var escapedValue = value?.Replace("\"", "\\\"") ?? string.Empty;
|
||||
return $"\"{escapedValue}\"";
|
||||
}
|
||||
}
|
||||
|
||||
public static string FormatPropertyName(string propertyName)
|
||||
|
||||
Reference in New Issue
Block a user