Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JsonPartialMatcher - match guid and string #971

Closed
timurnes opened this issue Jul 19, 2023 · 2 comments · Fixed by #972
Closed

JsonPartialMatcher - match guid and string #971

timurnes opened this issue Jul 19, 2023 · 2 comments · Fixed by #972
Labels

Comments

@timurnes
Copy link
Contributor

timurnes commented 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);
}
@StefH
Copy link
Collaborator

StefH commented 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)

@timurnes
Copy link
Contributor Author

timurnes commented 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

StefH pushed a commit that referenced this issue Jul 19, 2023
* JsonPartialMatcher - match guid and string (#971)

* JsonPartialMatcher - match guid and string. Add Regex with Guid test (#971)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants