-
Notifications
You must be signed in to change notification settings - Fork 582
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
java grpc validation in different projects #185
Comments
I'm not sure I understand why Can you expand a bit on what errors you are seeing when |
Thanks for quick comment, that was really nice.
In configuration class I have
GetDataRequest class looks like this:
But when "GetDataRequest" message comes "ReflectiveValidatorIndex" is trying to load "no.entur.abt.referencedata.exchange.grpc.GetDataRequestValidator" and of course it fails (because GetDataRequestValidator is inside AbtReferenceDataApiValidator) then it returns Validator.ALWAYS_VALID. Maybe if generated class would implement some interface or have annotation, it would be possible to search for these factory classes and then find right one (afaik it generates one such class per proto file, that means there will be a lot of them for us, with hundred different messages). Of course not all messages are used in all modules, so now I can just hardcode factory classes for right project, because I know what type of message are used there. I'm using pga 0.1.0-SNAPSHOT and protoc:3.6.1 |
Can you attach a redacted version of your proto? Are you using nested types in your proto? I have a hunch the structure of your .proto file matters. |
Also, are you using any of the |
More or less every proto file starts like this:
and typical messages looks like this:
and some messages contains fields that come from different files as well, but validation seems to work just fine. At least I had no problems so far. I was just a bit uncertain about best way to initialise grpc interceptor. I ended (so far) with this:
For us it should be fine, there's just 1 place that contains package name, where it scans (could be parameter), but we put all proto files in same package (even if they are in different folders). |
Ok. I've identified an issue with reflection and the |
@rmichela So the Java usage doesn't seem to be documented anywhere. Is it just as simple as adding the |
Yes. It's that easy. // Create a validator index that reflectively loads generated validators
ValidatorIndex index = new ReflectiveValidatorIndex();
// Create a gRPC client and server interceptor to automatically validate messages (requires pgv-java-grpc module)
clientStub = clientStub.withInterceptors(new ValidatingClientInterceptor(index));
serverBuilder.addService(ServerInterceptors.intercept(svc, new ValidatingServerInterceptor(index))); |
Hi,
just wanted to ask, if I'm doing it right or maybe there is better way.
I have few projects that are using different proto files, proto-files themself are extracted to separate project/jar and validator generation is run there. It creates validator file per proto file (I think) that contains static method to get ValidatorImpl for class (and static classes for every message that needs to be validated). It works very well in tests, when I know concrete proto message I want to test. But it gets a bit more complicated when I start using grpc and ServerInterceptor.
I found 2 ValidatorIndex implementations - ExplicitValidatorIndex and ReflectiveValidatorIndex. But seems like I can't really use any of them, ReflectiveValidatorIndex tries to find Validator based on requested class name, but it fails because actual validator class is static inner class. And I can't use ExplicitValidatorIndex because there are too many classes I would need manually add.
So I ended up with implementing Validator interface that caches locally Validator and ValidatorImpl :
And because this looks a bit generic I wander if there is better way and maybe I missed something?
The text was updated successfully, but these errors were encountered: