Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Version of buf.build/bufbuild/protovalidate to use.
protovalidate.version = v0.13.1
protovalidate.version = v0.13.3

# Arguments to the protovalidate-conformance CLI
protovalidate.conformance.args = --strict_message --strict_error --expected_failures=expected-failures.yaml
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/build/buf/protovalidate/EvaluatorBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ private void buildMessage(Descriptor desc, MessageEvaluator msgEval)
processMessageExpressions(descriptor, msgRules, msgEval, defaultInstance);
processMessageOneofRules(descriptor, msgRules, msgEval);
processOneofRules(descriptor, msgEval);
processFields(descriptor, msgEval);
processFields(descriptor, msgRules, msgEval);
} catch (InvalidProtocolBufferException e) {
throw new CompilationException(
"failed to parse proto definition: " + desc.getFullName(), e);
Expand Down Expand Up @@ -243,12 +243,17 @@ private void processOneofRules(Descriptor desc, MessageEvaluator msgEval)
}
}

private void processFields(Descriptor desc, MessageEvaluator msgEval)
private void processFields(Descriptor desc, MessageRules msgRules, MessageEvaluator msgEval)
throws CompilationException, InvalidProtocolBufferException {
List<FieldDescriptor> fields = desc.getFields();
for (FieldDescriptor fieldDescriptor : fields) {
FieldDescriptor descriptor = desc.findFieldByName(fieldDescriptor.getName());
FieldRules fieldRules = resolver.resolveFieldRules(descriptor);
if (!fieldRules.hasIgnore()
&& msgRules.getOneofList().stream()
.anyMatch(oneof -> oneof.getFieldsList().contains(fieldDescriptor.getName()))) {
fieldRules = fieldRules.toBuilder().setIgnore(Ignore.IGNORE_IF_UNPOPULATED).build();
}
FieldEvaluator fldEval = buildField(descriptor, fieldRules);
msgEval.append(fldEval);
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/buf/validate/validate.proto
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ message MessageRules {
// silently ignored when unmarshalling, with only the last field being set when
// unmarshalling completes.
//
// Note that adding a field to a `oneof` will also set the IGNORE_IF_UNPOPULATED on the fields. This means
// only the field that is set will be validated and the unset fields are not validated according to the field rules.
// This behavior can be overridden by setting `ignore` against a field.
//
// ```proto
// message MyMessage {
Expand Down