Response body parsing - JSON::ParserError 757: unexpected token at '"some-string"'
See original GitHub issueI am currently using PactNet 2.2.0 and PactNet.Windows 2.2.0 with a .NET 4.6.1 project. I have unit tests for my consumer service that generates a pact file (spec v1) against a provider service with an interaction like below. The interaction is for adding a new record and then returning its id in the response body. My provider’s service controller method simply returns a Guid
and below is how the Guid
response gets written to the pact file.
Pact interaction from consumer:
{
"description": "AddWidget request returns 200 and the new widget Id",
"providerState": "MyService does not have widgets",
"request": {
"method": "post",
"path": "/widgets",
"headers": {
"Content-Type": "application/json; charset=utf-8"
},
"body": {
"field1": "foo",
"field2": "bar"
},
"response": {
"status": 200,
"headers": {
"Content-Type": "application/json; charset=utf-8"
},
"body": "\"3e7d3384-26ff-401e-a256-b6eb23bc9402\""
}
}
Provider service controller method:
[HttpPost]
[Route("widgets")]
public async Task<Guid> AddWidgetAsync([FromBody] Widget widget, CancellationToken cancellationToken)
{
return await _widgetService.AddWidgetAsync(widget, cancellationToken).ConfigureAwait(false);
}
This all seems fine and unit tests work for the consumer. However, when I try to verify the pact file against the provider service I get the following error.
Failure/Error: let(:response_body) { parse_body_from_response(response) }
JSON::ParserError:
757: unexpected token at '"3e7d3384-26ff-401e-a256-b6eb23bc9402"'
I’ve tried manually changing the interaction in the pact file to "body": "3e7d3384-26ff-401e-a256-b6eb23bc9402"
(without the escaped double-quote), but I get the same above error.
I did some digging, and it appears that this error is coming from the Ruby JSON parser. I don’t know anything about Ruby, but from my internet searches it sounds like some versions of Ruby have a “quirks” mode to support parsing top level strings like this, but I couldn’t find any such mode in the Ruby distributed with PactNet.
It seems like if the pact file gets written out by PactNet, then PactNet should be able to read that same file back in, right? Are there any workarounds and/fixes to consider? Am I doing something wrong with my controller method? I’m basically looking for any help or recommendations. Thanks
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (5 by maintainers)
Top GitHub Comments
It is something we’ve done for a while with our endpoints, and it seems to work fine when a .NET service is talking to another .NET service. Pact is the first time we’ve hit an issue with the top level values (probably because .NET/Microsoft was letting us get away with it previously). I discussed the options with my team, and I think we are just going to change our endpoints to wrap the
id
in an object - that seems to be the least amount of effort for us right now given how everything else is setup. Thanks for the suggestions.👍 Cool, sounds like the best option.