Skip to content

Feature/early mismatch#1451

Open
Stepami wants to merge 11 commits intowiremock:masterfrom
Stepami:feature/early-mismatch
Open

Feature/early mismatch#1451
Stepami wants to merge 11 commits intowiremock:masterfrom
Stepami:feature/early-mismatch

Conversation

@Stepami
Copy link
Copy Markdown

@Stepami Stepami commented Apr 24, 2026

Description

  • added new method WireMock.RequestBuilders.IRequestBuilder.WithEarlyMismatch
  • covered feature with tests (WireMockServerTests.Grpc and RequestMessageCompositeMatcherTests)

Method WithEarlyMismatch sets selector which chooses matcher to return immediate mismatch during mapping processing.
Selector is a Func delegate run on available matchers.

References

Closes #1442

@StefH
Copy link
Copy Markdown
Collaborator

StefH commented Apr 25, 2026

I understand the code, and this works, however it needs deep understanding of the code.

A better approach could be to introduce an enum

public enum MatcherType
{
    Path = 0,

    Url = 1,

    // ...
}

And pass this to the WithEarlyMismatch(MatcherType.Path).
Internally the code can translate that enum to a real request matcher.

Also this logic could be added to the Mapping + MappingModel.

@Stepami
Copy link
Copy Markdown
Author

Stepami commented Apr 25, 2026

@StefH I agree that design with delegate is a bit complicated for an arbitrary user. I do like the idea with type discriminator for RequestMatchers! But i don't think that enum is a solid choice in this situation.

My concern is that IRequestMatcher is a public interface, which means i can make my own implementation outside of WireMock. Thus, i would like to be ensured that WireMock is capable to support my custom RequestMatcher in a way that i can introduce new RequestMatcherType outside of library. enum doesn't allow that as it's not extensible.

I would make RequestMatcherType as the Value Object with predefined instances rather than enum. It would allow instantiate new types outside of library's source code.

The part about logic addition to Mapping + MappingModel is unclear to me

@StefH
Copy link
Copy Markdown
Collaborator

StefH commented Apr 26, 2026

I think this design could be a possible solution:

  1. Add a Name property to IRequestMatcher, just as I did for IMatcher.

  2. Just call the WithEarlyMismatch with a string like "Path".

  3. Now the internal logic will loop all request matchers on a mapping and take the one with matching name.

  4. In case you use a custom matcher like "MyNamespace.MyMatcher", then use TypeLoader.TryLoadStaticInstanceByFullName<IRequestMatcher>("MyNamespace.MyMatcher"); to resolve it.

  5. For step 4, I'm not sure this will work 100%, but I don't know if there any users who do build their own RequestMatcher.


The part about logic addition to Mapping + MappingModel is unclear to me

It's also possible to define the mappings using a MappingModel (which can you can post as json) to WireMock.Net (https://wiremock.org/dotnet/request-matching/#json-example)
So this new logic should also be added to the mapping, so that you can configure this new functionality using the MappingModel.

Stepami added 4 commits April 28, 2026 00:37
Add `RequestMatcherType` to request matchers for improved type
identification

Closes wiremock#1442
Replace `EarlyMatcherSelector` with `EarlyMatcherType` for improved
clarity and consistency

Closes wiremock#1442
Add EarlyMatcherType support in request models and mapping conversion

Closes wiremock#1442
add unit tests for EarlyMatcherType in mapping conversion and
serialization

Closes wiremock#1442
@Stepami
Copy link
Copy Markdown
Author

Stepami commented Apr 27, 2026

@StefH I investigated the code and decided to implement the idea with enum. There are two big reasons for this:

  1. No info on your side about custom request matchers
  2. Property string Name is reserved in the Headers and Cookie matchers where it has different meaning. Thus i decided to avoid breaking change

Comment thread src/WireMock.Net.Abstractions/Admin/Mappings/RequestModel.cs Outdated
Comment thread test/WireMock.Net.Tests/Serialization/MappingConverterTests.cs
Stepami added 3 commits April 29, 2026 00:02
Replaced inline `EarlyMatcherType` logic with the new
`RequestMessageEarlyMatcher` class to support cases when several
matchers of the same type are present. For instance - Header, Cookie,
Param

Closes wiremock#1442
add unit tests for early mismatch scenarios with several matchers of
same type. Currently, headers and parameters

Closes wiremock#1442
use fully qualified enum for EarlyMatcherType in serialization

Closes wiremock#1442
Comment thread src/WireMock.Net.Minimal/Util/StringUtils.cs Outdated
- removed unused method
- added missing curly brackets

Closes wiremock#1442
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.

Bug: [grpc] WithBodyAsProtoBuf exception on match

2 participants