JsonPartialWildcardMatcher for grpc protobuf is not working #658

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

Originally created by @GomesNayagam on GitHub (Dec 31, 2024).

Originally assigned to: @StefH on GitHub.

rpc GetVersion (GetVersionRequest) returns (GetVersionResponse);
message GetVersionRequest {
  Client Client = 1;

}
message Client {
	string CorrelationId = 1;
}
var protoBufJsonMatcher = new JsonPartialWildcardMatcher(new { CorrelationId = "*" });

                .Given(
                    Request
                        .Create()
                        .UsingPost()
                        .WithHttpVersion("2")
                        .WithPath("/Policy2.PolicyService2/GetVersion")
                        .WithBodyAsProtoBuf(
                            protoDefinition,
                            "Policy2.GetVersionRequest",
                            protoBufJsonMatcher
                    )
                )
Originally created by @GomesNayagam on GitHub (Dec 31, 2024). Originally assigned to: @StefH on GitHub. ``` proto rpc GetVersion (GetVersionRequest) returns (GetVersionResponse); message GetVersionRequest { Client Client = 1; } message Client { string CorrelationId = 1; } ``` ``` c# var protoBufJsonMatcher = new JsonPartialWildcardMatcher(new { CorrelationId = "*" }); .Given( Request .Create() .UsingPost() .WithHttpVersion("2") .WithPath("/Policy2.PolicyService2/GetVersion") .WithBodyAsProtoBuf( protoDefinition, "Policy2.GetVersionRequest", protoBufJsonMatcher ) ) ```
adam added the question label 2025-12-29 15:29:58 +01:00
adam closed this issue 2025-12-29 15:29:58 +01:00
Author
Owner

@StefH commented on GitHub (Dec 31, 2024):

In case you want to use a wildcard, use the JsonPartialWildcardMatcher.

https://github.com/WireMock-Net/WireMock.Net/wiki/Request-Matching-JsonPartialWildcardMatcher

@StefH commented on GitHub (Dec 31, 2024): In case you want to use a wildcard, use the **JsonPartialWildcardMatcher**. https://github.com/WireMock-Net/WireMock.Net/wiki/Request-Matching-JsonPartialWildcardMatcher
Author
Owner

@GomesNayagam commented on GitHub (Dec 31, 2024):

tried that as well it is not working @StefH

@GomesNayagam commented on GitHub (Dec 31, 2024): tried that as well it is not working @StefH
Author
Owner

@StefH commented on GitHub (Jan 1, 2025):

@GomesNayagam

Can you verify which matches are working, and which are not?
See
https://github.com/WireMock-Net/WireMock.Net/wiki/Request-Matching-Tips

@StefH commented on GitHub (Jan 1, 2025): @GomesNayagam Can you verify which matches are working, and which are not? See https://github.com/WireMock-Net/WireMock.Net/wiki/Request-Matching-Tips
Author
Owner

@StefH commented on GitHub (Jan 1, 2025):

This unit test works fine:

    private const string ProtoDefinition = @"
syntax = ""proto3"";

package greet;

service Greeter {
  rpc SayHello (HelloRequest) returns (HelloReply);
}

message HelloRequest {
  string name = 1;
}

message HelloReply {
  string message = 1;
}
";

[Fact]
public async Task WireMockServer_WithBodyAsProtoBuf_JsonPartialWildcardMatcher()
{
    // Arrange
    var bytes = Convert.FromBase64String("CgRzdGVm");
    var jsonMatcher = new JsonPartialWildcardMatcher(new { name = "*" });

    using var server = WireMockServer.Start();

    server
        .WhenRequest(r => r
            .UsingPost()
            .WithPath("/grpc/greet.Greeter/SayHello")
            .WithBodyAsProtoBuf(ProtoDefinition, "greet.HelloRequest", jsonMatcher)
        )
        .ThenRespondWith(r => r
            .WithTrailingHeader("grpc-status", "0")
        );

    // Act
    var protoBuf = new ByteArrayContent(bytes);
    protoBuf.Headers.ContentType = new MediaTypeHeaderValue("application/grpc-web");

    var client = server.CreateClient();
    var response = await client.PostAsync("/grpc/greet.Greeter/SayHello", protoBuf);

    // Assert
    response.StatusCode.Should().Be(HttpStatusCode.OK);

    server.Stop();
}
@StefH commented on GitHub (Jan 1, 2025): This unit test works fine: ``` c# private const string ProtoDefinition = @" syntax = ""proto3""; package greet; service Greeter { rpc SayHello (HelloRequest) returns (HelloReply); } message HelloRequest { string name = 1; } message HelloReply { string message = 1; } "; [Fact] public async Task WireMockServer_WithBodyAsProtoBuf_JsonPartialWildcardMatcher() { // Arrange var bytes = Convert.FromBase64String("CgRzdGVm"); var jsonMatcher = new JsonPartialWildcardMatcher(new { name = "*" }); using var server = WireMockServer.Start(); server .WhenRequest(r => r .UsingPost() .WithPath("/grpc/greet.Greeter/SayHello") .WithBodyAsProtoBuf(ProtoDefinition, "greet.HelloRequest", jsonMatcher) ) .ThenRespondWith(r => r .WithTrailingHeader("grpc-status", "0") ); // Act var protoBuf = new ByteArrayContent(bytes); protoBuf.Headers.ContentType = new MediaTypeHeaderValue("application/grpc-web"); var client = server.CreateClient(); var response = await client.PostAsync("/grpc/greet.Greeter/SayHello", protoBuf); // Assert response.StatusCode.Should().Be(HttpStatusCode.OK); server.Stop(); } ```
Author
Owner

@GomesNayagam commented on GitHub (Jan 3, 2025):

@StefH thanks for the sample, for simple sample it works unfortunately none of the jsonmatcher works for actual project codebase, i guess it could be namespace in proto issue but i am not sure. is there anyway to debug in detail to see whats going wrong?

@GomesNayagam commented on GitHub (Jan 3, 2025): @StefH thanks for the sample, for simple sample it works unfortunately none of the jsonmatcher works for actual project codebase, i guess it could be namespace in proto issue but i am not sure. is there anyway to debug in detail to see whats going wrong?
Author
Owner

@StefH commented on GitHub (Jan 3, 2025):

Can you share the proto file?

@StefH commented on GitHub (Jan 3, 2025): Can you share the proto file?
Author
Owner

@GomesNayagam commented on GitHub (Jan 3, 2025):

thanks for reopening @StefH here is the proto content

syntax = "proto3";

import "google/protobuf/timestamp.proto";

//option csharp_namespace = "SCIF.PM.RPC.Policy";

package Policy;

service PolicyService {	
	rpc GetPolicyStatus(GetPolicyStatusRequest) returns (GetPolicyStatusResponse);
}


message GetPolicyStatusRequest {
	Client Client = 1;
	LegacyPolicyKey LegacyPolicyKey = 2;
}

message GetPolicyStatusResponse {
	ResponseStatus Status = 1;
	LegacyPolicyKey LegacyPolicyKey = 2;
	int32 PolicyNumber = 3;
	google.protobuf.Timestamp InceptionDate = 4;
	google.protobuf.Timestamp ExpirationDate = 5;
	string PolicyStatus = 6;
        string PolicySubStatus = 7;
	string RenewalStatus = 8;
}

message LegacyPolicyKey {
	string Group = 1;
	int32 UnitNumber = 2;
	int32 Year = 3;
	string Suffix = 4;
}

// Common message. Add to all request messages
message Client {
	string CorrelationId = 1;
	enum Clients {
	    Unknown = 0;
            QMS = 1;
            BillingCenter = 2;
            PAS = 3;
            Payroll = 4;
            Portal = 5;
            SFO = 6;
            QuoteAndBind = 7;
            LegacyConversion = 8;
            BindNow = 9;
	    PaymentPortal = 10 ;
	    PricingEngine = 11;
	}
	Clients ClientName = 2;
}
@GomesNayagam commented on GitHub (Jan 3, 2025): thanks for reopening @StefH here is the proto content ``` proto syntax = "proto3"; import "google/protobuf/timestamp.proto"; //option csharp_namespace = "SCIF.PM.RPC.Policy"; package Policy; service PolicyService { rpc GetPolicyStatus(GetPolicyStatusRequest) returns (GetPolicyStatusResponse); } message GetPolicyStatusRequest { Client Client = 1; LegacyPolicyKey LegacyPolicyKey = 2; } message GetPolicyStatusResponse { ResponseStatus Status = 1; LegacyPolicyKey LegacyPolicyKey = 2; int32 PolicyNumber = 3; google.protobuf.Timestamp InceptionDate = 4; google.protobuf.Timestamp ExpirationDate = 5; string PolicyStatus = 6; string PolicySubStatus = 7; string RenewalStatus = 8; } message LegacyPolicyKey { string Group = 1; int32 UnitNumber = 2; int32 Year = 3; string Suffix = 4; } // Common message. Add to all request messages message Client { string CorrelationId = 1; enum Clients { Unknown = 0; QMS = 1; BillingCenter = 2; PAS = 3; Payroll = 4; Portal = 5; SFO = 6; QuoteAndBind = 7; LegacyConversion = 8; BindNow = 9; PaymentPortal = 10 ; PricingEngine = 11; } Clients ClientName = 2; } ```
Author
Owner

@StefH commented on GitHub (Jan 3, 2025):

@GomesNayagam
I think in your case, the matcher should be like this:

var protoBufJsonMatcher = new JsonPartialWildcardMatcher(new { Client = new { CorrelationId = "*" } });

Because CorrelationId is a property of Client which is a property of the GetVersionRequest?

@StefH commented on GitHub (Jan 3, 2025): @GomesNayagam I think in your case, the matcher should be like this: ``` c# var protoBufJsonMatcher = new JsonPartialWildcardMatcher(new { Client = new { CorrelationId = "*" } }); ``` Because CorrelationId is a property of Client which is a property of the GetVersionRequest?
Author
Owner

@GomesNayagam commented on GitHub (Jan 4, 2025):

@GomesNayagam I think in your case, the matcher should be like this:

var protoBufJsonMatcher = new JsonPartialWildcardMatcher(new { Client = new { CorrelationId = "*" } });

Because CorrelationId is a property of Client which is a property of the GetVersionRequest?

thanks it works, apologies for the silly mistake.

@GomesNayagam commented on GitHub (Jan 4, 2025): > @GomesNayagam I think in your case, the matcher should be like this: > > ```cs > var protoBufJsonMatcher = new JsonPartialWildcardMatcher(new { Client = new { CorrelationId = "*" } }); > ``` > > Because CorrelationId is a property of Client which is a property of the GetVersionRequest? thanks it works, apologies for the silly mistake.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#658