Skip to content

Commit

Permalink
Code generator improvements (#940)
Browse files Browse the repository at this point in the history
* Fix quotation marks escaping in multiline string

* Add support for JsonPartialMatcher and JsonPartialWildcardMatcher in mapping code generator
  • Loading branch information
cezarypiatek authored May 25, 2023
1 parent 7c3a0c8 commit e5cc6f5
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 7 deletions.
21 changes: 19 additions & 2 deletions src/WireMock.Net/Serialization/MappingConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,28 @@ public string ToCSharpCode(IMapping mapping, MappingConverterSettings? settings

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(@" )");
Expand Down
18 changes: 14 additions & 4 deletions src/WireMock.Net/Util/CSharpFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,25 @@ public static string ConvertJsonToAnonymousObjectDefinition(JToken token, int in
};
}

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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,21 @@ text
})
);

server
.Given(Request.Create()
.UsingMethod("POST")
.WithPath("/foo3")
.WithBody(new JsonPartialMatcher(
value: "{ a = 1, b = 2 }",
ignoreCase: false,
throwException: false,
regex: false
))
)
.WithGuid("4126dec8-470b-4eff-93bb-c24f83b8b1fd")
.RespondWith(Response.Create()
.WithStatusCode(200)
.WithBody(@"Line1
Some ""value"" in Line2")
);

18 changes: 17 additions & 1 deletion test/WireMock.Net.Tests/WireMockAdminApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using WireMock.Client;
using WireMock.Handlers;
using WireMock.Logging;
using WireMock.Matchers;
using WireMock.Models;
using WireMock.Net.Tests.VerifyExtensions;
using WireMock.RequestBuilders;
Expand Down Expand Up @@ -719,6 +720,7 @@ public async Task IWireMockAdminApi_GetMappingsCode()
var guid1 = Guid.Parse("90356dba-b36c-469a-a17e-669cd84f1f05");
var guid2 = Guid.Parse("1b731398-4a5b-457f-a6e3-d65e541c428f");
var guid3 = Guid.Parse("f74fd144-df53-404f-8e35-da22a640bd5f");
var guid4 = Guid.Parse("4126DEC8-470B-4EFF-93BB-C24F83B8B1FD");
var server = WireMockServer.StartWithAdminInterface();

server
Expand Down Expand Up @@ -769,11 +771,25 @@ public async Task IWireMockAdminApi_GetMappingsCode()
" })
);

server
.Given(
Request.Create()
.WithPath("/foo3")
.WithBody(new JsonPartialMatcher(new { a=1, b=2}))
.UsingPost()
)
.WithGuid(guid4)
.RespondWith(
Response.Create()
.WithStatusCode(200)
.WithBody("Line1\r\nSome \"value\" in Line2")
);

// Act
var api = RestClient.For<IWireMockAdminApi>(server.Url);

var mappings = await api.GetMappingsAsync().ConfigureAwait(false);
mappings.Should().HaveCount(3);
mappings.Should().HaveCount(4);

var code = await api.GetMappingsCodeAsync().ConfigureAwait(false);

Expand Down

0 comments on commit e5cc6f5

Please sign in to comment.