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: support BodyAsJson for Request in static mapping files. #154

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

Feature: support BodyAsJson for Request in static mapping files. #154

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

Comments

@DavidKorn
Copy link
Collaborator

Per comment from @StefH in #153, I'll work on a PR for this:

I think the shortcoming with using the MatcherModelMapper for JSON body matching is that the Pattern must be a string. A static mapping would have to escape out all the double-quotes and do the whole JSON on one line, which really hampers readability.

Let's support the BodyAsJson property on the static mapping Request definition and let it just take an object (and not a Matcher definition). So this would look just like the BodyAsJson in the static mapping Response. BodyAsJson will 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 on the Body can be used instead.

Further, if the proxy recorder came across a serializable JSON body in the request, it can 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 pushed a commit to DavidKorn/WireMock.Net that referenced this issue Jun 26, 2018
… value equality via JSON serialization to support JSON object matches in request bodies. Update the proxy recorder to use JsonObjectMatcher to record the mapping for a Request with a BodyAsJson. WireMock-Net#154
@DavidKorn
Copy link
Collaborator Author

I pushed up a PR for this. I did not go as far as supporting BodyAsJson having a single JSON value in the Request mapping model, because I wasn't sure if it drifted too far from the existing design. But I did add a JsonObjectMatcher for JSON body matching without escaped strings, and I set it for use with the proxy recorder when recording a request that has a BodyAsJson.

@StefH StefH changed the title Support BodyAsJson for Request in static mapping files. Feature: support BodyAsJson for Request in static mapping files. Jun 26, 2018
StefH added a commit that referenced this issue Jun 26, 2018
@StefH
Copy link
Collaborator

StefH commented Jun 26, 2018

Thanks a lot for your PR, however I did solve this issue in a different way, I hope you're not offended ;-)

I added code to JsonMatcher to support both : object and string. Please review my branch at https://github.com/WireMock-Net/WireMock.Net/tree/stef_JsonMatcherFix

@StefH StefH self-assigned this Jun 26, 2018
@StefH StefH added the feature label Jun 26, 2018
@DavidKorn
Copy link
Collaborator Author

Haha, that's a much bolder refactor! It seems to add some flexibility, so that's great. I wasn't sure you'd want to generalize Pattern that much since it seems to be string in all other places, but I happily defer to your vision.

At this point, is IValueMatcher still needed separately from IObjectMatcher? IStringMatcher already has string [] GetPatterns() so maybe IObjectMatcher could just get object GetValue() ?

I really appreciate you jumping on this, and we're eager to put it to use. Will you be promoting this version to Nuget or shall we depend on a fork for the time being?

@StefH
Copy link
Collaborator

StefH commented Jun 26, 2018

For now I'll keep the IValueMatcher. BTW I did send you invite to join this project.

@StefH
Copy link
Collaborator

StefH commented Jun 26, 2018

And maybe join the gitter @ https://gitter.im/wiremock_dotnet/Lobby

StefH added a commit that referenced this issue Jun 26, 2018
* JsonMatcher (#154)

* small update in example code
@StefH StefH closed this as completed Jun 26, 2018
@StefH
Copy link
Collaborator

StefH commented Jun 26, 2018

New NuGet 1.0.4.2 is uploaded.

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

No branches or pull requests

2 participants