Fluent API for RestClient MappingModel creation #350

Closed
opened 2025-12-29 08:26:45 +01:00 by adam · 6 comments
Owner

Originally created by @danspark on GitHub (Jul 17, 2021).

Originally assigned to: @StefH on GitHub.

Is your feature request related to a problem? Please describe.
I'm thinking about using WireMock as a container to run alongside my application during tests, and as far as I searched, looks like I have to create a mapping by instantiating a new MappingModel directly.

Describe the solution you'd like
I think it would be really useful for users to have an experience similar to the fluent API to used to create requests when WireMock is used on the same process. I am no expert on the library but I imagine there would be some limitations, but for simple cases like matching a request and building a response, it would be really useful, since I wouldn't have to learn two ways to configure mappings.

Describe alternatives you've considered
Build the mapping model directly.

Is your feature request supported by WireMock (java version)? Please provide details.
I'm not familiar with the java version, but as far as I researched, I couldn't find anything like that yet.

Additional context
I don't if this is something that would be interesting for the main library or maybe it would be something that the creators would want the community to develop. But if this is something that is planned or wanted, I would be glad to help as I can. But as I said, I'm no expert and I'm not sure if I would be able to cover every possible scenario the current fluent API has.

I'm building a simple example and I'll post here when I make some progress.

Originally created by @danspark on GitHub (Jul 17, 2021). Originally assigned to: @StefH on GitHub. **Is your feature request related to a problem? Please describe.** I'm thinking about using WireMock as a container to run alongside my application during tests, and as far as I searched, looks like I have to create a mapping by instantiating a new `MappingModel` directly. **Describe the solution you'd like** I think it would be really useful for users to have an experience similar to the fluent API to used to create requests when WireMock is used on the same process. I am no expert on the library but I imagine there would be some limitations, but for simple cases like matching a request and building a response, it would be really useful, since I wouldn't have to learn two ways to configure mappings. **Describe alternatives you've considered** Build the mapping model directly. **Is your feature request supported by [WireMock (java version)](https://www.wiremock.org)? Please provide details.** I'm not familiar with the java version, but as far as I researched, I couldn't find anything like that yet. **Additional context** I don't if this is something that would be interesting for the main library or maybe it would be something that the creators would want the community to develop. But if this is something that is planned or wanted, I would be glad to help as I can. But as I said, I'm no expert and I'm not sure if I would be able to cover every possible scenario the current fluent API has. I'm building a simple example and I'll post here when I make some progress.
adam added the feature label 2025-12-29 08:26:45 +01:00
adam closed this issue 2025-12-29 08:26:45 +01:00
Author
Owner

@StefH commented on GitHub (Jul 19, 2021):

I can build a standard FluentBuilder for all the client model objects, however this builder will be only a simple version.

Like

var matcher = FluentBuilder.MatcherModel()
    .WithName("n")
    .WithPattern("p")
    .Build();

Will this be what you need?

@StefH commented on GitHub (Jul 19, 2021): I can build a standard FluentBuilder for all the client model objects, however this builder will be only a simple version. Like ``` c# var matcher = FluentBuilder.MatcherModel() .WithName("n") .WithPattern("p") .Build(); ``` Will this be what you need?
Author
Owner

@StefH commented on GitHub (Jul 20, 2021):

@danspark

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

WireMock.Net.RestClient.1.4.19-preview-01-ci-15231.nupkg

@StefH commented on GitHub (Jul 20, 2021): @danspark See preview version on MyGet https://github.com/WireMock-Net/WireMock.Net/wiki/MyGet-preview-versions `WireMock.Net.RestClient.1.4.19-preview-01-ci-15231.nupkg`
Author
Owner

@danspark commented on GitHub (Jul 20, 2021):

I can build a standard FluentBuilder for all the client model objects, however this builder will be only a simple version.

Like

var matcher = FluentBuilder.MatcherModel()
    .WithName("n")
    .WithPattern("p")
    .Build();

Will this be what you need?

Yes! That would make the experience similar to the fluent API, I'll take a look soon. Thank you!

@danspark commented on GitHub (Jul 20, 2021): > I can build a standard FluentBuilder for all the client model objects, however this builder will be only a simple version. > > Like > > ```cs > var matcher = FluentBuilder.MatcherModel() > .WithName("n") > .WithPattern("p") > .Build(); > ``` > > Will this be what you need? Yes! That would make the experience similar to the fluent API, I'll take a look soon. Thank you!
Author
Owner

@StefH commented on GitHub (Aug 4, 2021):

@danspark
Did you have time to take a look yet ?

@StefH commented on GitHub (Aug 4, 2021): @danspark Did you have time to take a look yet ?
Author
Owner

@danspark commented on GitHub (Aug 4, 2021):

Apologies for taking so long, I've been a bit busy. I took a look at the most recent version of the CI build, the API looks a little different, I don't know if it made a lot of difference. My simplest setup looked like this:

			var response = await _api.PostMappingAsync(new MappingModel {
				Request = new RequestModel {
					Url = "*"
				},
				Response = new ResponseModel {
					StatusCode = (int)statusCode
				}
			});

With the modification, it looks like this:

			var requestBuilder = new FluentBuilder.MappingModelBuilder()
				.WithRequest(new RequestModel {
					Path = "*"
				})
				.WithResponse(new ResponseModel {
					StatusCode = (int)statusCode
				}).Build();

Maybe the MappingModelBuilder WithRequest and WithResponse methods could have an overload that asks for an Action<RequestModelBuilder> and Action<ResponseModelBuilder>? That way it could look like this:

			var requestBuilder = new FluentBuilder.MappingModelBuilder()
				.WithRequest(b => b.WithPath("*"))
				.WithResponse(b => b.WithStatusCode((int) statusCode)}).Build();
@danspark commented on GitHub (Aug 4, 2021): Apologies for taking so long, I've been a bit busy. I took a look at the most recent version of the CI build, the API looks a little different, I don't know if it made a lot of difference. My simplest setup looked like this: ```csharp var response = await _api.PostMappingAsync(new MappingModel { Request = new RequestModel { Url = "*" }, Response = new ResponseModel { StatusCode = (int)statusCode } }); ``` With the modification, it looks like this: ``` csharp var requestBuilder = new FluentBuilder.MappingModelBuilder() .WithRequest(new RequestModel { Path = "*" }) .WithResponse(new ResponseModel { StatusCode = (int)statusCode }).Build(); ``` Maybe the MappingModelBuilder `WithRequest` and `WithResponse` methods could have an overload that asks for an `Action<RequestModelBuilder>` and `Action<ResponseModelBuilder>`? That way it could look like this: ``` csharp var requestBuilder = new FluentBuilder.MappingModelBuilder() .WithRequest(b => b.WithPath("*")) .WithResponse(b => b.WithStatusCode((int) statusCode)}).Build(); ```
Author
Owner

@StefH commented on GitHub (Aug 4, 2021):

You can work-around this problem by using a FluentBuilder.ResponseModelBuilder() and then assign the value to WithResponse(...)

To support your scenario, I've to see if I can update the FluentBuilder.
Can you create a new issue here? --> https://github.com/StefH/FluentBuilder

And for now, I'll merge the PR and close this issue.

In case I can implement your scenario in the FluentBuilder project, I'll post an update here and release a new WireMock.Net NuGet.

@StefH commented on GitHub (Aug 4, 2021): You can work-around this problem by using a `FluentBuilder.ResponseModelBuilder()` and then assign the value to `WithResponse(...)` To support your scenario, I've to see if I can update the FluentBuilder. Can you create a new issue here? --> https://github.com/StefH/FluentBuilder And for now, I'll merge the PR and close this issue. In case I can implement your scenario in the FluentBuilder project, I'll post an update here and release a new WireMock.Net NuGet.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net#350