Response body is missing in generated pact file when IBodyResponseBuilder.WithBody is used #433

Closed
opened 2025-12-29 15:23:42 +01:00 by adam · 4 comments
Owner

Originally created by @shack05 on GitHub (Aug 10, 2022).

Describe the bug

If I use the IBodyResponseBuilder.WithBody method to specify a response based on a string and then generate a pact file then the pact file will not include the response body.
If I use WithBodyAsJson the response body is defined as expected in the pact file.

Expected behavior:

The response body is defined in the pact file.

Test to reproduce

server
   .WithConsumer("Consumer1")
   .WithProvider("Provider1");

server
   .Given(Request.Create().WithPath("/foo").UsingGet())
   .WithTitle("Example1")
   .RespondWith(Response.Create()
      .WithStatusCode(200)
      .WithBody(@"{ foo: ""bar"" }"));

server.SavePact("...")

Expected pact:

{
   "consumer": {
      "name": "Consumer1"
   },
   "interactions": [
      {
         "providerState": "Example1",
         "request": {
            "method": "GET",
            "path": "/foo",
         },
         "response": {
            "body": {
               "foo": "bar"
            },
            "status": "200",
         } 
      }
   ],
   "provider": {
      "name": "Provider1"
   }
}

Actual pact:
The resulting pact file is:

{
   "consumer": {
      "name": "Consumer1"
   },
   "interactions": [
      {
         "providerState": "Example1",
         "request": {
            "method": "GET",
            "path": "/foo",
         },
         "response": {
            "status": "200",
         } 
      }
   ],
   "provider": {
      "name": "Provider1"
   }
}

Wiremock version 1.5.3

The reason I am using WithBody rather than WithBodyAsJson is because, as far as I am aware, the json I am trying to describe cannot be declared as an anonymous object because some json property names start with a dollar sign and some property names include colons. Is there an alternative to using WithBody such as being able to configure a json serializer to be used in the WithBodyAsJson method?

Originally created by @shack05 on GitHub (Aug 10, 2022). ### Describe the bug If I use the `IBodyResponseBuilder.WithBody` method to specify a response based on a string and then generate a pact file then the pact file will not include the response body. If I use `WithBodyAsJson` the response body is defined as expected in the pact file. ### Expected behavior: The response body is defined in the pact file. ### Test to reproduce ``` csharp server .WithConsumer("Consumer1") .WithProvider("Provider1"); server .Given(Request.Create().WithPath("/foo").UsingGet()) .WithTitle("Example1") .RespondWith(Response.Create() .WithStatusCode(200) .WithBody(@"{ foo: ""bar"" }")); server.SavePact("...") ``` Expected pact: ``` json { "consumer": { "name": "Consumer1" }, "interactions": [ { "providerState": "Example1", "request": { "method": "GET", "path": "/foo", }, "response": { "body": { "foo": "bar" }, "status": "200", } } ], "provider": { "name": "Provider1" } } ``` Actual pact: The resulting pact file is: ``` json { "consumer": { "name": "Consumer1" }, "interactions": [ { "providerState": "Example1", "request": { "method": "GET", "path": "/foo", }, "response": { "status": "200", } } ], "provider": { "name": "Provider1" } } ``` ### Other related info Wiremock version 1.5.3 The reason I am using `WithBody` rather than `WithBodyAsJson` is because, as far as I am aware, the json I am trying to describe cannot be declared as an anonymous object because some json property names start with a dollar sign and some property names include colons. Is there an alternative to using `WithBody` such as being able to configure a json serializer to be used in the `WithBodyAsJson` method?
adam added the bug label 2025-12-29 15:23:42 +01:00
adam closed this issue 2025-12-29 15:23:42 +01:00
Author
Owner

@StefH commented on GitHub (Aug 10, 2022):

Hello @shack05

I'll take a look at the code.


For this question:

. Is there an alternative to using WithBody such as being able to configure a json serializer to be used in the WithBodyAsJson method?

--> This looks a bit like this one? https://github.com/WireMock-Net/WireMock.Net/issues/774

@StefH commented on GitHub (Aug 10, 2022): Hello @shack05 I'll take a look at the code. --- For this question: > . Is there an alternative to using WithBody such as being able to configure a json serializer to be used in the WithBodyAsJson method? --> This looks a bit like this one? https://github.com/WireMock-Net/WireMock.Net/issues/774
Author
Owner

@StefH commented on GitHub (Aug 10, 2022):

@shack05
Can you try preview version 1.5.3-ci-16331 from MyGet?

https://github.com/WireMock-Net/WireMock.Net/wiki/MyGet-preview-versions

@StefH commented on GitHub (Aug 10, 2022): @shack05 Can you try preview version `1.5.3-ci-16331` from MyGet? https://github.com/WireMock-Net/WireMock.Net/wiki/MyGet-preview-versions
Author
Owner

@shack05 commented on GitHub (Aug 11, 2022):

Hi,
Thanks for the quick response.

--> This looks a bit like this one? #774
Yes, although with the change you've made in the preview version I no longer have a use case for a separate serializer.

Can you try preview version 1.5.3-ci-16331 from MyGet?
Thanks, I can confirm the response is as expected for that preview version.
I did notice however that the request body is not defined in the pact file if I create a body.
I was using a JsonPartialMatcher to define the constraints for the body.
Are request bodies supported?

@shack05 commented on GitHub (Aug 11, 2022): Hi, Thanks for the quick response. > --> This looks a bit like this one? #774 Yes, although with the change you've made in the preview version I no longer have a use case for a separate serializer. > Can you try preview version 1.5.3-ci-16331 from MyGet? Thanks, I can confirm the response is as expected for that preview version. I did notice however that the request body is not defined in the pact file if I create a body. I was using a JsonPartialMatcher to define the constraints for the body. Are request bodies supported?
Author
Owner

@StefH commented on GitHub (Aug 11, 2022):

@shack05

Can you open a new issue for this:

I did notice however that the request body is not defined in the pact file if I create a body.
I was using a JsonPartialMatcher to define the constraints for the body.

I'll merge the PR. For now just keep using the preview version, in some time a new official version will be released.

@StefH commented on GitHub (Aug 11, 2022): @shack05 Can you open a new issue for this: ``` I did notice however that the request body is not defined in the pact file if I create a body. I was using a JsonPartialMatcher to define the constraints for the body. ``` I'll merge the PR. For now just keep using the preview version, in some time a new official version will be released.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#433