Search code examples
protocol-buffersgrpcquarkusgrpc-javaquarkus-grpc

Unable to use datetime.proto definition after installing proto-google-common-protos


I have a gRPC-based project in Quarkus. I have to return some data fetched from a PostgreSQL database, where some of the columns are of timestamp without time zone type — mapped to LocalDateTime in Java.

create table users (
  id          uuid                        not null default gen_random_uuid(),
  ...
  created_on  timestamp without time zone not null default localtimestamp(6),
  modified_on timestamp without time zone not null default localtimestamp(6),
  primary key (id)
);
syntax = "proto3";

import "google/type/datetime.proto"; <- Doesn't work :/

...

message GetUserResponse {
  optional string id = 1;
  ...
  optional google.type.DateTime created_on = 5;
  optional google.type.DateTime modified_on = 6;
}
public class User {
  private UUID id;

  ...

  private LocalDateTime createdOn;

  private LocalDateTime modifiedOn;
}

I've tried importing com.google.api.grpc:proto-google-common-protos:2.33.0 into the project so I can use google/type/datetime.proto, but somehow the types are not getting recognized.

$ ./gradlew clean quarkusBuild 
> Task :clean
> Task :processResources
google/type/datetime.proto: File not found.
user.proto:12:1: Import "google/type/datetime.proto" was not found or had errors.
user.proto:43:12: "google.type.DateTime" is not defined.
user.proto:44:12: "google.type.DateTime" is not defined.

NOTE: I can certainly use OffsetDateTime in Java because I see that google/protobuf/timestamp.proto is recognized. Both OffsetDateTime and LocalDateTime will let me store what I want, because I use UTC across the board, but I would rather solve the problem and avoid changing too many things which might break some others.

Is there a way to register the types coming from com.google.api.grpc:proto-google-common-protos? My assumption is that just by importing that library they should be available.


Update

There is a Quarkus guide for gRPC code generation. I think this may solve this problem, but so far, using quarkus.generate-code.grpc.scan-for-imports, quarkus.generate-code.grpc.scan-for-proto, or quarkus.generate-code.grpc.scan-for-proto-include. doesn't make any difference.


Solution

  • The gRPC Code Generation Reference Guide documents different ways to scan/include/generate code based on what's available in the project.

    In my case, since I already had com.google.api.grpc:proto-google-common-protos in the dependencies, I just wanted to make all the *.proto files available in the project, so I used scan-for-imports:

    quarkus:
      generate-code:
        grpc:
          scan-for-imports: all
    

    There is also a way to scan only the specified dependencies by group and artifact. This is much better, but the syntax is a little bit awkward.