WireMock-Net / WireMock.Net

WireMock.Net is a flexible product for stubbing and mocking web HTTP responses using advanced request matching and response templating. Based on the functionality from http://WireMock.org, but extended with more functionality.
Apache License 2.0
1.36k stars 200 forks source link

JsonPartialMatcher - match guid and string #971

Closed timurnes closed 11 months ago

timurnes commented 11 months ago

As a WireMock user I'd like to setup JsonPartialMatcher using anonymous object with some Guid fields. As of now I have to cast Guid fields to string because this condition in AbstractJsonPartialMatcher rejects match - value type is JTokenType.Guid and input type is JTokenType.String.

Generally, guid in json is a string so I think this feature can simplify user code a bit and won't be a breaking change

Possible solution is to add a check before the condition I've specified above

if (input != null &&
    ((value.Type == JTokenType.Guid && input.Type == JTokenType.String) ||
    (value.Type == JTokenType.String && input.Type == JTokenType.Guid)))
{
    return IsMatch(value.ToString(), input.ToString());
}

Unit test in JsonPartialMatcherTests:

[Fact]
public void JsonPartialMatcher_IsMatch_GuidAsString()
{
    // Assign
    var name = Guid.NewGuid();
    var matcher = new JsonPartialMatcher(new { Id = 1, Name = name });

    // Act
    var jObject = new JObject
    {
        { "Id", new JValue(1) },
        { "Name", new JValue(name.ToString()) }
    };
    double match = matcher.IsMatch(jObject);

    // Assert
    Assert.Equal(1.0, match);
}
StefH commented 11 months ago

@timurnes

Looks ok to me. Can you make a PR with code change + unit tests?

However I think that this same logic needs to be applied to line 66 if a Regex is used:

if (Regex && value.Type == JTokenType.String && input != null)
timurnes commented 11 months ago

@StefH Created PR #972

I haven't added any changes to Regex logic because I can't imagine how we can create valid regex with Guid type. So Guid field will be skipped and matched on the next condition that is added with this PR