how to set datetime for grpc field #660

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

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

Originally assigned to: @StefH on GitHub.

        GetPolicyStatusResponse pr =
            new()
            {
                PolicyNumber = 12345
                InceptionDate = Timestamp.FromDateTime(DateTime.UtcNow),
                ExpirationDate = Timestamp.FromDateTime(DateTime.UtcNow),

            };

           .RespondWith(
               Response
                   .Create()
                   .WithHeader("Content-Type", "application/grpc")
                   .WithTrailingHeader("grpc-status", "0")
                   .WithBodyAsProtoBuf(protoDefinition, "Policy.GetPolicyStatusResponse", pr)
                   .WithTransformer()
           );

This throws "Bad Response"

Originally created by @GomesNayagam on GitHub (Dec 30, 2024). Originally assigned to: @StefH on GitHub. ``` c# GetPolicyStatusResponse pr = new() { PolicyNumber = 12345 InceptionDate = Timestamp.FromDateTime(DateTime.UtcNow), ExpirationDate = Timestamp.FromDateTime(DateTime.UtcNow), }; .RespondWith( Response .Create() .WithHeader("Content-Type", "application/grpc") .WithTrailingHeader("grpc-status", "0") .WithBodyAsProtoBuf(protoDefinition, "Policy.GetPolicyStatusResponse", pr) .WithTransformer() ); ``` This throws "Bad Response"
adam added the bug 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):

I think that there is an issue with the Timestamp field, I need to investigate.

@StefH commented on GitHub (Dec 31, 2024): I think that there is an issue with the Timestamp field, I need to investigate.
Author
Owner

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

https://github.com/StefH/ProtoBufJsonConverter/pull/24

@StefH commented on GitHub (Jan 3, 2025): https://github.com/StefH/ProtoBufJsonConverter/pull/24
Author
Owner

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

https://github.com/WireMock-Net/WireMock.Net/pull/1231

@StefH commented on GitHub (Jan 3, 2025): https://github.com/WireMock-Net/WireMock.Net/pull/1231
Author
Owner

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

@GomesNayagam

can you please try preview version 1.6.11-ci-19550 ?

@StefH commented on GitHub (Jan 3, 2025): @GomesNayagam can you please try preview version 1.6.11-ci-19550 ?
Author
Owner

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

@GomesNayagam

can you please try preview version 1.6.11-ci-19550 ?

sure i will test and reply here

@GomesNayagam commented on GitHub (Jan 4, 2025): > @GomesNayagam > > can you please try preview version 1.6.11-ci-19550 ? sure i will test and reply here
Author
Owner

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

but WireMock.Net 1.6.11-ci-19550 was not found. An approximate best match of WireMock.Net 1.6.11 was resolved.
here the issue is same.i dont find any preview in package management too.

@GomesNayagam commented on GitHub (Jan 4, 2025): but WireMock.Net 1.6.11-ci-19550 was not found. An approximate best match of WireMock.Net 1.6.11 was resolved. here the issue is same.i dont find any preview in package management too.
Author
Owner

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

but WireMock.Net 1.6.11-ci-19550 was not found. An approximate best match of WireMock.Net 1.6.11 was resolved. here the issue is same.i dont find any preview in package management too.

See this page for info on how to use preview packages:
https://github.com/WireMock-Net/WireMock.Net/wiki/MyGet-preview-versions

@StefH commented on GitHub (Jan 4, 2025): > but WireMock.Net 1.6.11-ci-19550 was not found. An approximate best match of WireMock.Net 1.6.11 was resolved. here the issue is same.i dont find any preview in package management too. See this page for info on how to use preview packages: https://github.com/WireMock-Net/WireMock.Net/wiki/MyGet-preview-versions
Author
Owner

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

hey @StefH it did not work.

@GomesNayagam commented on GitHub (Jan 6, 2025): hey @StefH it did not work.
Author
Owner

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

Can you share the proto file?

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

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

Can you share the proto file?

sure, here u go

syntax = "proto3";

import "google/protobuf/timestamp.proto";

package Policy2;

service PolicyService2 {	
    rpc GetVersion (GetVersionRequest) returns (GetVersionResponse);
	rpc GetVersion2 (GetVersion2Request) returns (GetVersion2Response);
}

// REQUEST/RESPONSE DEFINITIONS

message GetVersion2Request {
  Client Client = 1;

}
message GetVersion2Response {
  string Version = 1;

}
message GetVersionRequest {
  Client Client = 1;

}
message GetVersionResponse {
  string Version = 1;
  google.protobuf.Timestamp DateHired = 2;
}

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;
}

code:

This below works:

            var res = JsonConvert.DeserializeObject(
                "{ \"Version\" : \"DevTr1\", \"DateHired\" : \"2022-09-13T00:00:00Z\" }"
            );

This below is not working:

            Policy2.GetVersionResponse res1 = new() { Version = "DevTr1", DateHired = Timestamp.FromDateTime(DateTime.UtcNow) };
@GomesNayagam commented on GitHub (Jan 6, 2025): > Can you share the proto file? sure, here u go ``` proto syntax = "proto3"; import "google/protobuf/timestamp.proto"; package Policy2; service PolicyService2 { rpc GetVersion (GetVersionRequest) returns (GetVersionResponse); rpc GetVersion2 (GetVersion2Request) returns (GetVersion2Response); } // REQUEST/RESPONSE DEFINITIONS message GetVersion2Request { Client Client = 1; } message GetVersion2Response { string Version = 1; } message GetVersionRequest { Client Client = 1; } message GetVersionResponse { string Version = 1; google.protobuf.Timestamp DateHired = 2; } 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; } ``` code: This below works: ``` c# var res = JsonConvert.DeserializeObject( "{ \"Version\" : \"DevTr1\", \"DateHired\" : \"2022-09-13T00:00:00Z\" }" ); ``` This below is not working: ``` c# Policy2.GetVersionResponse res1 = new() { Version = "DevTr1", DateHired = Timestamp.FromDateTime(DateTime.UtcNow) }; ```
Author
Owner

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

Can you try this preview?
1.6.11-ci-19552

@StefH commented on GitHub (Jan 7, 2025): Can you try this preview? 1.6.11-ci-19552
Author
Owner

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

@StefH datetime issue is fixed in this preview but the namespace is still not working.

here is the namespace line i added in my proto
option csharp_namespace = "NarrowIntegrationTest.Lookup";

using NarrowIntegrationTest.Lookup;
var channel = GrpcChannel.ForAddress(server.Url!);

        var client = new PolicyService2.PolicyService2Client(channel);
        NarrowIntegrationTest.Lookup.Client cl =
            new() { ClientName = NarrowIntegrationTest.Lookup.Client.Types.Clients.Pas, CorrelationId = "12345" };

        //var reply = await client.GetVersion2Async(new GetVersion2Request { Client = cl });
        var reply2 = await client.GetVersionAsync(
            new NarrowIntegrationTest.Lookup.GetVersionRequest { Client = cl }
        );
@GomesNayagam commented on GitHub (Jan 8, 2025): @StefH datetime issue is fixed in this preview but the namespace is still not working. here is the namespace line i added in my proto option csharp_namespace = "NarrowIntegrationTest.Lookup"; using NarrowIntegrationTest.Lookup; var channel = GrpcChannel.ForAddress(server.Url!); var client = new PolicyService2.PolicyService2Client(channel); NarrowIntegrationTest.Lookup.Client cl = new() { ClientName = NarrowIntegrationTest.Lookup.Client.Types.Clients.Pas, CorrelationId = "12345" }; //var reply = await client.GetVersion2Async(new GetVersion2Request { Client = cl }); var reply2 = await client.GetVersionAsync( new NarrowIntegrationTest.Lookup.GetVersionRequest { Client = cl } );
Author
Owner

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

@GomesNayagam
I'm using this proto:

syntax = "proto3";

option csharp_namespace = "NarrowIntegrationTest.Lookup";

import "google/protobuf/timestamp.proto";

package Policy;

service PolicyService {	
    rpc GetVersion (GetVersionRequest) returns (GetVersionResponse);
}

message GetVersionRequest {
  Client Client = 1;

}
message GetVersionResponse {
  string Version = 1;
  google.protobuf.Timestamp DateHired = 2;
  Client Client = 3;
}

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;
}

and this c# test code:

[Fact]
public async Task WireMockServer_WithBodyAsProtoBuf_Enum_UsingPolicyGrpcGeneratedClient()
{
    // Arrange
    const int seconds = 1722301323;
    const int nanos = 12300;
    const string version = "test";
    const string correlationId = "correlation";
    var definition = await System.IO.File.ReadAllTextAsync("./Grpc/policy.proto");

    using var server = WireMockServer.Start(useHttp2: true);

    server
        .Given(Request.Create()
            .UsingPost()
            .WithPath("/Policy.PolicyService/GetVersion")
            .WithBody(new NotNullOrEmptyMatcher())
        )
        .RespondWith(Response.Create()
            .WithHeader("Content-Type", "application/grpc")
            .WithTrailingHeader("grpc-status", "0")
            .WithBodyAsProtoBuf(definition, "NarrowIntegrationTest.Lookup.GetVersionResponse",
                new GetVersionResponse
                {
                    Version = version,
                    DateHired = new Timestamp
                    {
                        Seconds = seconds,
                        Nanos = nanos
                    },
                    Client = new NarrowIntegrationTest.Lookup.Client
                    {
                        ClientName = NarrowIntegrationTest.Lookup.Client.Types.Clients.BillingCenter,
                        CorrelationId = correlationId
                    }
                }
            )
        );

    // Act
    var channel = GrpcChannel.ForAddress(server.Url!);
    var client = new PolicyService.PolicyServiceClient(channel);

    var reply = await client.GetVersionAsync(new GetVersionRequest());

    // Assert
    reply.Version.Should().Be(version);
    reply.DateHired.Should().Be(new Timestamp { Seconds = seconds, Nanos = nanos });
    reply.Client.ClientName.Should().Be(NarrowIntegrationTest.Lookup.Client.Types.Clients.BillingCenter);
    reply.Client.CorrelationId.Should().Be(correlationId);
}

And it works fine.

@StefH commented on GitHub (Jan 8, 2025): @GomesNayagam I'm using this proto: ``` proto syntax = "proto3"; option csharp_namespace = "NarrowIntegrationTest.Lookup"; import "google/protobuf/timestamp.proto"; package Policy; service PolicyService { rpc GetVersion (GetVersionRequest) returns (GetVersionResponse); } message GetVersionRequest { Client Client = 1; } message GetVersionResponse { string Version = 1; google.protobuf.Timestamp DateHired = 2; Client Client = 3; } 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; } ``` and this c# test code: ``` c# [Fact] public async Task WireMockServer_WithBodyAsProtoBuf_Enum_UsingPolicyGrpcGeneratedClient() { // Arrange const int seconds = 1722301323; const int nanos = 12300; const string version = "test"; const string correlationId = "correlation"; var definition = await System.IO.File.ReadAllTextAsync("./Grpc/policy.proto"); using var server = WireMockServer.Start(useHttp2: true); server .Given(Request.Create() .UsingPost() .WithPath("/Policy.PolicyService/GetVersion") .WithBody(new NotNullOrEmptyMatcher()) ) .RespondWith(Response.Create() .WithHeader("Content-Type", "application/grpc") .WithTrailingHeader("grpc-status", "0") .WithBodyAsProtoBuf(definition, "NarrowIntegrationTest.Lookup.GetVersionResponse", new GetVersionResponse { Version = version, DateHired = new Timestamp { Seconds = seconds, Nanos = nanos }, Client = new NarrowIntegrationTest.Lookup.Client { ClientName = NarrowIntegrationTest.Lookup.Client.Types.Clients.BillingCenter, CorrelationId = correlationId } } ) ); // Act var channel = GrpcChannel.ForAddress(server.Url!); var client = new PolicyService.PolicyServiceClient(channel); var reply = await client.GetVersionAsync(new GetVersionRequest()); // Assert reply.Version.Should().Be(version); reply.DateHired.Should().Be(new Timestamp { Seconds = seconds, Nanos = nanos }); reply.Client.ClientName.Should().Be(NarrowIntegrationTest.Lookup.Client.Types.Clients.BillingCenter); reply.Client.CorrelationId.Should().Be(correlationId); } ``` And it works fine.
Author
Owner

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

@StefH apologies, type in my code forgot to update the namespace in this line
.WithBodyAsProtoBuf(definition, "NarrowIntegrationTest.Lookup.GetVersionResponse", all good appreciate for the tolerance from the ignorant folks like me.

@GomesNayagam commented on GitHub (Jan 8, 2025): @StefH apologies, type in my code forgot to update the namespace in this line .WithBodyAsProtoBuf(definition, "NarrowIntegrationTest.Lookup.GetVersionResponse", all good appreciate for the tolerance from the ignorant folks like me.
Author
Owner

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

which version the fix available to use? @StefH

@GomesNayagam commented on GitHub (Jan 9, 2025): which version the fix available to use? @StefH
Author
Owner

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

Today I'll release an official new version on NuGet.

@StefH commented on GitHub (Jan 9, 2025): Today I'll release an official new version on NuGet.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#660