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

Feature: Add JsonMatcher to support Json mapping #153

Closed
DavidKorn opened this issue Jun 25, 2018 · 6 comments
Closed

Feature: Add JsonMatcher to support Json mapping #153

DavidKorn opened this issue Jun 25, 2018 · 6 comments
Assignees

Comments

@DavidKorn
Copy link
Collaborator

I'm mocking an API that makes heavy use of JSON objects in the request body, and I'm trying to create readable static mapping files for requests with different JSON bodies.

I was hoping to leverage the Reqest's BodyAsJson tag to just define the exact request body I'm wanting to match., rather than create long JsonPathMatcher pattern strings for the Request Body.

This seems to parse ok, but isn't working as I expected. I think under the hood WireMock.Net is deserializing the JSON in the mapping file and in the request, and then doing reference equality on the two resulting objects to see if they are the same, which naturally they are not.

Is there a better way to use BodyAsJson in static mapping files, or maybe a workaround?

@StefH StefH changed the title Using Request.BodyAsJson in static mapping files Question: Using Request.BodyAsJson in static mapping files Jun 25, 2018
@StefH StefH self-assigned this Jun 25, 2018
@StefH
Copy link
Collaborator

StefH commented Jun 25, 2018

Correct, the JsonPathMatcher can only be used when you want to match a json path pattern.

Solution would be:

  • add a new JsonMatcher or JsonExactMatcher <-- preferred
  • update the JsonPathMatcher to accept a json object as pattern

@StefH StefH changed the title Question: Using Request.BodyAsJson in static mapping files Feature: Add JsonMatcher to support Json mapping Jun 25, 2018
StefH added a commit that referenced this issue Jun 25, 2018
@StefH
Copy link
Collaborator

StefH commented Jun 25, 2018

@StefH StefH closed this as completed Jun 25, 2018
StefH added a commit that referenced this issue Jun 25, 2018
@DavidKorn
Copy link
Collaborator Author

I'm happy to work on a contribution to fix improve this. I think the shortcoming with using the MatcherModelMapper is that the pattern is a string. A static mapping user would have to escape out all the double-quotes and do the whole JSON on one line, which really hampers readability.

Would you allow a BodyAsJson property on the static mapping Request definition that just took an object (and not a Matcher definition)? So this would look just like the BodyAsJson in the static mapping Response. It would trigger an exact JSON serialization comparison with the incoming request body. This would be very clean to read and alter in the mapping file, and I think it would well suit a common use case. If more flexibility is needed, the JsonPathMatcher can be used instead.

Further, if the proxy recorder came across a serializable JSON body in the request, it could record this as a BodyAsJson property for the static mapping file rather than translate it into an ExactMatcher as it currently does. (The drawback we're seeing for the ExactMatcher is that if the recorded call indented the JSON body but a later caller doesn't, we don't get a match.)

@DavidKorn
Copy link
Collaborator Author

LOL, by the time I got that comment written you'd already implemented JsonMatcher! WOW!

@DavidKorn
Copy link
Collaborator Author

So this is a nice improvement, thanks so much for the rapid response. I'm really jazzed about this tool, and I much appreciate your continuing work to enhance it.

I'd still be interested in support for BodyAsJson object definitions in the Request mapping, for the reasons I had outlined above. If I came up with a PR, would you be open to incorporating that?

@StefH
Copy link
Collaborator

StefH commented Jun 25, 2018

Further, if the proxy recorder came across a serializable JSON body in the request, it could record this as a BodyAsJson property for the static mapping file rather than translate it into an ExactMatcher as it currently does. (The drawback we're seeing for the ExactMatcher is that if the recorded call indented the JSON body but a later caller doesn't, we don't get a match.)

Good point. Can you make new issue, or maybe an PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants