You can validate it, but you should also be aware of the nuance of Protobuf Enumerations.
Always include an UNKNOWN = 0;
enum value for every enum. When Protobuf doesn't know what an enum value corresponds to, it sets it to the default value. This lets you detect when a newer client with new codes uses a value the server doesn't understand. Also, if the field is unset, it naturally is equal to UNKNOWN
which let's you check if the field is absent. (If you do want to allow absent enums, wrap them in a message{}
).
In your server application handler, you should check to see which one of the supported values the client provided. In the case that it isn't one of them, you should fail the RPC with an INVALID_ARGUMENT
status code:
out: {
switch (req.getCurrency()) {
case EUR:
case GBP:
case USD: break out;
case UNKNOWN:
responseObserver.onError(
Status.INVALID_ARGUMENT
.withDescription("bad currency " + req.getCurrency())
.asRuntimeException());
return;
}
throw new AssertionError("missed case!");
}
// keep handling the request
This code checks that the code is one of the supported ones. If an unsupported code comes in, it will be handled by the UNKNOWN
case, and return an error early. In case you modify your proto and more cases are added, static analysis will catch the missing case (or throw an AssertionError). Note that this won't happen if the client updates their proto. Avoid using the default
case, because it can be easily missed when modifying proto. Alternatively, you could put all the supported cases in a map and check if it is present.