Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to set datetime for grpc field #1228

Closed
GomesNayagam opened this issue Dec 30, 2024 · 16 comments
Closed

how to set datetime for grpc field #1228

GomesNayagam opened this issue Dec 30, 2024 · 16 comments
Assignees
Labels

Comments

@GomesNayagam
Copy link

GomesNayagam commented Dec 30, 2024

        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"

@StefH StefH self-assigned this Dec 31, 2024
@StefH StefH added bug and removed question labels Dec 31, 2024
@StefH
Copy link
Collaborator

StefH commented Dec 31, 2024

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

@StefH
Copy link
Collaborator

StefH commented Jan 3, 2025

@StefH
Copy link
Collaborator

StefH commented Jan 3, 2025

#1231

@StefH
Copy link
Collaborator

StefH commented Jan 3, 2025

@GomesNayagam

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

@GomesNayagam
Copy link
Author

@GomesNayagam

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

sure i will test and reply here

@GomesNayagam
Copy link
Author

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.

@StefH
Copy link
Collaborator

StefH commented 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

@GomesNayagam
Copy link
Author

hey @StefH it did not work.

@StefH
Copy link
Collaborator

StefH commented Jan 6, 2025

Can you share the proto file?

@GomesNayagam
Copy link
Author

GomesNayagam commented 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) };

@StefH
Copy link
Collaborator

StefH commented Jan 7, 2025

Can you try this preview?
1.6.11-ci-19552

@GomesNayagam
Copy link
Author

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

@StefH
Copy link
Collaborator

StefH commented 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.

@GomesNayagam
Copy link
Author

@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.

@StefH StefH closed this as completed Jan 8, 2025
@GomesNayagam
Copy link
Author

which version the fix available to use? @StefH

@StefH
Copy link
Collaborator

StefH commented Jan 9, 2025

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants