-
Notifications
You must be signed in to change notification settings - Fork 13
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
Improve support for dynamic messages #48
Conversation
Update protovalidate-java to determine if the field, message, or oneof options contains an unknown field for the protovalidate extension. If so, reparse the options type to correctly interpret the options and enable validation. This will enable protovalidate to run when the inputs are a FileDescriptorSet (with preserved options).
public class CompilationException extends ValidationException { | ||
public CompilationException(String message) { | ||
super(message); | ||
} | ||
|
||
public CompilationException(String message, Throwable cause) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added overridden constructor so if we have an Exception cause, we can preserve it for debugging.
throws InvalidProtocolBufferException, CompilationException { | ||
DescriptorProtos.MessageOptions options = desc.getOptions(); | ||
// If the protovalidate message extension is unknown, reparse using extension registry. | ||
if (options.getUnknownFields().hasField(ValidateProto.message.getNumber())) { | ||
options = DescriptorProtos.MessageOptions.parseFrom(options.toByteString(), registry); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the key fix for handling dynamic messages created from a file descriptor set. If we determine that the protovalidate extension is in the unknown fields, we'll reparse it using our extension registry.
public class ValidatorDynamicMessageTest { | ||
|
||
@Test | ||
public void testFieldConstraintDynamicMessage() throws Exception { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All of these tests previously failed for field, message, and oneof constraints.
return Descriptors.FileDescriptor.buildFrom(fdProto, dependencies); | ||
} | ||
|
||
private static DynamicMessage.Builder createMessageWithUnknownOptions(Message message) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just a round trip operation to go from a message created from Java generated code -> FileDescriptorSet -> DynamicMessage.Builder. The key thing is on line 126-127 where we reparse the FileDescriptorSet from binary format (losing any resolved extensions, etc.).
Update protovalidate-java to determine if the field, message, or oneof options contains an unknown field for the protovalidate extension. If so, reparse the options type to correctly interpret the options and enable validation.
This will enable protovalidate to run when the inputs are a FileDescriptorSet (with preserved options).