Skip to content

Add helpers for query params fluent MappingModelBuilder#1425

Merged
StefH merged 5 commits intowiremock:masterfrom
biltongza:feature/param-model-builder
Mar 14, 2026
Merged

Add helpers for query params fluent MappingModelBuilder#1425
StefH merged 5 commits intowiremock:masterfrom
biltongza:feature/param-model-builder

Conversation

@biltongza
Copy link
Copy Markdown
Contributor

@biltongza biltongza commented Feb 16, 2026

This PR adds some helper methods to improve the fluent API syntax when adding query params.

I found that adding query parameters with the fluent API was really tedious, compared to how easy it is to set up a body matcher.

For example, if I wanted to match a request to /api/weather?city=Amsterdam the code might look something like this:

Given(m => m
  .WithRequest(r => r
    .UsingGet()
    .WithPath("/api/weather")
    .WithParams(pb =>pb
      .Add(p => p
        .WithName("city")
        .WithMatchers(mb => mb
          .Add(m => m
            .WithName("ExactMatcher")
            .WithPattern("Amsterdam")
          )
        )
      )
    )
  )
);

Really long and verbose.

With this PR, we can shorten it to:

Given(m => m
  .WithRequest(r => r
    .UsingGet()
    .WithPath("/api/weather")
    .WithParams(pb => pb
      pb.WithParam("city", mb => mb.WithExactMatcher("Amsterdam"))
    )
  )
);

@StefH
Copy link
Copy Markdown
Collaborator

StefH commented Feb 16, 2026

@biltongza
I'm a bit confused. The examples you give are not correct? And I'm not sure your code change is correct. Can you please revisit?

@biltongza
Copy link
Copy Markdown
Contributor Author

The example works with the MappingModelBuilder you get from the IWireMockAdminApi. I've added an example.

@biltongza biltongza force-pushed the feature/param-model-builder branch from dad76f5 to b4335c9 Compare February 16, 2026 18:27
@StefH StefH changed the title Add helpers for query params fluent builder Add helpers for query params fluent MappingBuilder Feb 17, 2026
@StefH StefH changed the title Add helpers for query params fluent MappingBuilder Add helpers for query params fluent MappingModelBuilder Feb 17, 2026
@StefH StefH added the feature label Feb 17, 2026
@StefH
Copy link
Copy Markdown
Collaborator

StefH commented Feb 19, 2026

@biltongza
Would it also make sense to apply the same update to headers and cookies?

@biltongza
Copy link
Copy Markdown
Contributor Author

Sure, it probably would make sense for cookies. For headers, there is already a WithHeader(string, string) helper, which under the hood seems to use a wildcard matcher. Would it make sense to have both that helper and this pattern?

@StefH
Copy link
Copy Markdown
Collaborator

StefH commented Feb 19, 2026

Sure, it probably would make sense for cookies. For headers, there is already a WithHeader(string, string) helper, which under the hood seems to use a wildcard matcher. Would it make sense to have both that helper and this pattern?

yes please

@StefH
Copy link
Copy Markdown
Collaborator

StefH commented Mar 11, 2026

@biltongza did you have time to add cookies logic?

@biltongza
Copy link
Copy Markdown
Contributor Author

Forgot about this! Will get it in tomorrow :)

@biltongza biltongza force-pushed the feature/param-model-builder branch from b4335c9 to d251c33 Compare March 12, 2026 17:17
@biltongza
Copy link
Copy Markdown
Contributor Author

@StefH I pushed the update, wasn't able to test it though. When I try to run the Wiremock.Net.Client app, I get a nice exception from the server:

2026-03-12T18:38:47 fail: WireMock.Net[0]
      HttpStatusCode set to 500 {0}
      System.NotSupportedException: JsonTypeInfo metadata for type 'Newtonsoft.Json.Linq.JArray' was not provided by TypeInfoResolver of type 'WireMock.Net.Json.SourceGenerationContext'. If using source generation, ensure that all root types passed to the serializer have been annotated with 'JsonSerializableAttribute', along with any types that might be serialized polymorphically. The unsupported member type is located on type 'System.Object'. Path: $.BodyAsJson.
       ---> System.NotSupportedException: JsonTypeInfo metadata for type 'Newtonsoft.Json.Linq.JArray' was not provided by TypeInfoResolver of type 'WireMock.Net.Json.SourceGenerationContext'. If using source generation, ensure that all root types passed to the serializer have been annotated with 'JsonSerializableAttribute', along with any types that might be serialized polymorphically.
         at System.Text.Json.ThrowHelper.ThrowNotSupportedException_NoMetadataForType(Type type, IJsonTypeInfoResolver resolver)
         at System.Text.Json.JsonSerializerOptions.GetTypeInfoInternal(Type type, Boolean ensureConfigured, Nullable`1 ensureNotNull, Boolean resolveIfMutable, Boolean fallBackToNearestAncestorType)
         at System.Text.Json.WriteStackFrame.InitializePolymorphicReEntry(Type runtimeType, JsonSerializerOptions options)
         at System.Text.Json.Serialization.JsonConverter.ResolvePolymorphicConverter(Object value, JsonTypeInfo jsonTypeInfo, JsonSerializerOptions options, WriteStack& state)
         at System.Text.Json.Serialization.JsonConverter`1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
         at System.Text.Json.Serialization.Metadata.JsonPropertyInfo`1.GetMemberAndWriteJson(Object obj, WriteStack& state, Utf8JsonWriter writer)
         at System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1.OnTryWrite(Utf8JsonWriter writer, T value, JsonSerializerOptions options, WriteStack& state)
         at System.Text.Json.Serialization.JsonConverter`1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
         at System.Text.Json.Serialization.JsonConverter`1.WriteCore(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
         --- End of inner exception stack trace ---
         at System.Text.Json.ThrowHelper.ThrowNotSupportedException(WriteStack& state, NotSupportedException ex)
         at System.Text.Json.Serialization.JsonConverter`1.WriteCore(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
         at System.Text.Json.Serialization.Metadata.JsonTypeInfo`1.Serialize(Utf8JsonWriter writer, T& rootValue, Object rootValueBoxed)
         at System.Text.Json.JsonSerializer.Serialize[TValue](Utf8JsonWriter writer, TValue value, JsonTypeInfo`1 jsonTypeInfo)
         at WireMock.Net.Json.SourceGenerationContext.LogEntryModelSerializeHandler(Utf8JsonWriter writer, LogEntryModel value) in /_/src/dotnet-WireMock.Net/obj/Release/net8.0/System.Text.Json.SourceGeneration/System.Text.Json.SourceGeneration.JsonSourceGenerator/SourceGenerationContext.LogEntryModel.g.cs:line 236
         at System.Text.Json.Serialization.Metadata.JsonTypeInfo`1.Serialize(Utf8JsonWriter writer, T& rootValue, Object rootValueBoxed)
         at System.Text.Json.JsonSerializer.WriteString[TValue](TValue& value, JsonTypeInfo`1 jsonTypeInfo)
         at System.Text.Json.JsonSerializer.Serialize[TValue](TValue value, JsonTypeInfo`1 jsonTypeInfo)
         at WireMock.Net.WireMockLogger.DebugRequestResponse(LogEntryModel logEntryModel, Boolean isAdminRequest) in /_/src/dotnet-WireMock.Net/WireMockLogger.cs:line 59
         at WireMock.Owin.WireMockMiddlewareLogger.LogLogEntry(LogEntry entry, Boolean addRequest) in /_/src/WireMock.Net.Minimal/Owin/WireMockMiddlewareLogger.cs:line 56
         at WireMock.Owin.WireMockMiddlewareLogger.LogRequestAndResponse(Boolean logRequest, RequestMessage request, IResponseMessage response, MappingMatcherResult match, MappingMatcherResult partialMatch, Activity activity) in /_/src/WireMock.Net.Minimal/Owin/WireMockMiddlewareLogger.cs:line 38
         at WireMock.Owin.WireMockMiddleware.InvokeInternalAsync(HttpContext ctx) in /_/src/WireMock.Net.Minimal/Owin/WireMockMiddleware.cs:line 169
         at WireMock.Owin.GlobalExceptionMiddleware.InvokeInternalAsync(HttpContext ctx) in /_/src/WireMock.Net.Minimal/Owin/GlobalExceptionMiddleware.cs:line 33

This happened on the latest version (v2.0.0).

I also tried it on v1.2.5 and I got a different error:

2026-03-12T18:40:43 fail: WireMock.Net[0]
      HttpStatusCode set to 500 {0}
      System.NotSupportedException: Serialization and deserialization of 'System.Type' instances is not supported. Path: $.RequestMatchResult.MatchDetails.MatcherType.
       ---> System.NotSupportedException: Serialization and deserialization of 'System.Type' instances is not supported.
         at System.Text.Json.Serialization.Converters.UnsupportedTypeConverter`1.Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
         at System.Text.Json.Serialization.JsonConverter`1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
         at System.Text.Json.Serialization.Metadata.JsonPropertyInfo`1.GetMemberAndWriteJson(Object obj, WriteStack& state, Utf8JsonWriter writer)
         at System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1.OnTryWrite(Utf8JsonWriter writer, T value, JsonSerializerOptions options, WriteStack& state)
         at System.Text.Json.Serialization.JsonConverter`1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
         at System.Text.Json.Serialization.Converters.IEnumerableDefaultConverter`2.OnWriteResume(Utf8JsonWriter writer, TCollection value, JsonSerializerOptions options, WriteStack& state)
         at System.Text.Json.Serialization.JsonCollectionConverter`2.OnTryWrite(Utf8JsonWriter writer, TCollection value, JsonSerializerOptions options, WriteStack& state)
         at System.Text.Json.Serialization.JsonConverter`1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
         at System.Text.Json.Serialization.Metadata.JsonPropertyInfo`1.GetMemberAndWriteJson(Object obj, WriteStack& state, Utf8JsonWriter writer)
         at System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1.OnTryWrite(Utf8JsonWriter writer, T value, JsonSerializerOptions options, WriteStack& state)
         at System.Text.Json.Serialization.JsonConverter`1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
         at System.Text.Json.Serialization.Metadata.JsonPropertyInfo`1.GetMemberAndWriteJson(Object obj, WriteStack& state, Utf8JsonWriter writer)
         at System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1.OnTryWrite(Utf8JsonWriter writer, T value, JsonSerializerOptions options, WriteStack& state)
         at System.Text.Json.Serialization.JsonConverter`1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
         at System.Text.Json.Serialization.JsonConverter`1.WriteCore(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
         --- End of inner exception stack trace ---
         at System.Text.Json.ThrowHelper.ThrowNotSupportedException(WriteStack& state, NotSupportedException ex)
         at System.Text.Json.Serialization.JsonConverter`1.WriteCore(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
         at System.Text.Json.Serialization.Metadata.JsonTypeInfo`1.Serialize(Utf8JsonWriter writer, T& rootValue, Object rootValueBoxed)
         at System.Text.Json.JsonSerializer.WriteString[TValue](TValue& value, JsonTypeInfo`1 jsonTypeInfo)
         at System.Text.Json.JsonSerializer.Serialize[TValue](TValue value, JsonSerializerOptions options)
         at WireMock.Net.WireMockLogger.DebugRequestResponse(LogEntryModel logEntryModel, Boolean isAdminRequest) in /_/src/dotnet-WireMock.Net/WireMockLogger.cs:line 58
         at WireMock.Owin.WireMockMiddleware.LogRequest(LogEntry entry, Boolean addRequest) in /_/src/WireMock.Net.Minimal/Owin/WireMockMiddleware.cs:line 362
         at WireMock.Owin.WireMockMiddleware.InvokeInternalCoreAsync(HttpContext ctx, RequestMessage request, Activity activity) in /_/src/WireMock.Net.Minimal/Owin/WireMockMiddleware.cs:line 261
         at WireMock.Owin.WireMockMiddleware.InvokeInternalAsync(HttpContext ctx) in /_/src/WireMock.Net.Minimal/Owin/WireMockMiddleware.cs:line 121
         at WireMock.Owin.GlobalExceptionMiddleware.InvokeInternalAsync(HttpContext ctx) in /_/src/WireMock.Net.Minimal/Owin/GlobalExceptionMiddleware.cs:line 60

@StefH
Copy link
Copy Markdown
Collaborator

StefH commented Mar 13, 2026

@biltongza
Good catch, this is broken in dotnet-WireMock.

It should be fixed here:
#1431

When this is merged to main, you can get latest from main and merge into your branch and test if it works correct.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants