WithCallback circumvent the rest of the builder #329

Closed
opened 2025-12-29 15:21:01 +01:00 by adam · 5 comments
Owner

Originally created by @rlf on GitHub (Jan 27, 2021).

Originally assigned to: @StefH on GitHub.

Describe the bug

The code-line listed here:
ebb9f128c5/src/WireMock.Net/ResponseBuilders/Response.cs (L374)

Effectively disables part of the builder - which IMO ruins the builder-pattern for the response builder.

Expected behavior:

I would expect the following code:

Response.Create()
  .WithCallback(req => new ResponseMessage())
  .WithStatusCode(HttpStatusCode.Created)
  .WithHeader("X-UserId", "NA")

to generate a responsemessage with what-ever the callback provides, but with the status-code Created and the additional header X-UserId.

Test to reproduce

Create any Given().RespondWith() chain that mixes both WithCallback and other builder-inputs.
Verify the Callback short-circuits anything else you put into the Builder-pattern.

I do get, how-ever, that if the callback changes the default value of say the Status - that status should prevail - but not if you specifically add a non-default status to your response, using the builder.

None that I can think of.

Originally created by @rlf on GitHub (Jan 27, 2021). Originally assigned to: @StefH on GitHub. ### Describe the bug The code-line listed here: https://github.com/WireMock-Net/WireMock.Net/blob/ebb9f128c52351662ee80afa92cd58a689668f07/src/WireMock.Net/ResponseBuilders/Response.cs#L374 Effectively disables part of the builder - which IMO ruins the builder-pattern for the response builder. ### Expected behavior: I would expect the following code: ```csharp Response.Create() .WithCallback(req => new ResponseMessage()) .WithStatusCode(HttpStatusCode.Created) .WithHeader("X-UserId", "NA") ``` to generate a responsemessage with what-ever the callback provides, but with the status-code `Created` and the additional header `X-UserId`. ### Test to reproduce Create any `Given().RespondWith()` chain that mixes both `WithCallback` and other builder-inputs. Verify the Callback short-circuits anything else you put into the Builder-pattern. I do get, how-ever, that if the callback changes the default value of say the Status - that status should prevail - but not if you specifically add a non-default status to your response, using the builder. ### Other related info None that I can think of.
adam added the bug label 2025-12-29 15:21:01 +01:00
adam closed this issue 2025-12-29 15:21:02 +01:00
Author
Owner

@StefH commented on GitHub (Feb 1, 2021):

@rlf I understand your issue.

I cannot remember anymore why I decided that WithCallback does behave like this.
(Maybe I thought that you want to do all logic in the callback, so also defining the status-codes and headers)

However, I see that in that case the fluentbuilder should not return a chainable thing so that it seems that you can add more fluent methods.

I'll check if it's easy to change that all additional .WIth*** are used.

@StefH commented on GitHub (Feb 1, 2021): @rlf I understand your issue. I cannot remember anymore why I decided that WithCallback does behave like this. (Maybe I thought that you want to do all logic in the callback, so also defining the status-codes and headers) However, I see that in that case the fluentbuilder should not return a chainable thing so that it seems that you can add more fluent methods. I'll check if it's easy to change that all additional .WIth*** are used.
Author
Owner

@StefH commented on GitHub (Feb 18, 2021):

Hello @rlf,

Can you please try preview NuGet WireMock.Net.1.4.6-ci-14668 from MyGet (https://github.com/WireMock-Net/WireMock.Net/wiki/MyGet-preview-versions)? This should fix your issue.

@StefH commented on GitHub (Feb 18, 2021): Hello @rlf, Can you please try preview NuGet `WireMock.Net.1.4.6-ci-14668` from MyGet (https://github.com/WireMock-Net/WireMock.Net/wiki/MyGet-preview-versions)? This should fix your issue.
Author
Owner

@StefH commented on GitHub (Feb 22, 2021):

Dear @rlf ; did you have time to test this?

@StefH commented on GitHub (Feb 22, 2021): Dear @rlf ; did you have time to test this?
Author
Owner

@rlf commented on GitHub (Feb 26, 2021):

Verified. At least the tests I wrote when discovering this bug is now green.
Thanks!
Awesome work man!

@rlf commented on GitHub (Feb 26, 2021): Verified. At least the tests I wrote when discovering this bug is now green. Thanks! Awesome work man!
Author
Owner

@StefH commented on GitHub (Feb 26, 2021):

@rlf Thank you for testing.

I'll release a new official version today.

@StefH commented on GitHub (Feb 26, 2021): @rlf Thank you for testing. I'll release a new official version today.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#329