JsonPartialMatcher - match guid and string #529

Closed
opened 2025-12-29 15:26:00 +01:00 by adam · 2 comments
Owner

Originally created by @timurnes on GitHub (Jul 19, 2023).

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);
}
Originally created by @timurnes on GitHub (Jul 19, 2023). 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](https://github.com/WireMock-Net/WireMock.Net/blob/9c51548d2b664b8c6a7417495491e2f5b2de53c2/src/WireMock.Net/Matchers/AbstractJsonPartialMatcher.cs#L77C9-L80C10) 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); } ```
adam added the feature label 2025-12-29 15:26:00 +01:00
adam closed this issue 2025-12-29 15:26:00 +01:00
Author
Owner

@StefH commented on GitHub (Jul 19, 2023):

@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)
@StefH commented on GitHub (Jul 19, 2023): @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: ``` c# if (Regex && value.Type == JTokenType.String && input != null) ```
Author
Owner

@timurnes commented on GitHub (Jul 19, 2023):

@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

@timurnes commented on GitHub (Jul 19, 2023): @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
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#529