Skip to content

Commit 17b113c

Browse files
authored
Update protovalidate (#329)
This implements the following upstream changes: - bufbuild/protovalidate#397 - bufbuild/protovalidate#396 - bufbuild/protovalidate#394 --------- Signed-off-by: Sri Krishna <[email protected]>
1 parent 0c843b5 commit 17b113c

File tree

4 files changed

+136
-214
lines changed

4 files changed

+136
-214
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Version of buf.build/bufbuild/protovalidate to use.
2-
protovalidate.version = v0.13.3
2+
protovalidate.version = v0.14.0
33

44
# Arguments to the protovalidate-conformance CLI
55
protovalidate.conformance.args = --strict_message --strict_error --expected_failures=expected-failures.yaml

src/main/java/build/buf/protovalidate/EvaluatorBuilder.java

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,6 @@ private void buildMessage(Descriptor desc, MessageEvaluator msgEval)
174174
DynamicMessage defaultInstance = DynamicMessage.newBuilder(desc).buildPartial();
175175
Descriptor descriptor = defaultInstance.getDescriptorForType();
176176
MessageRules msgRules = resolver.resolveMessageRules(descriptor);
177-
if (msgRules.getDisabled()) {
178-
return;
179-
}
180177
processMessageExpressions(descriptor, msgRules, msgEval, defaultInstance);
181178
processMessageOneofRules(descriptor, msgRules, msgEval);
182179
processOneofRules(descriptor, msgEval);
@@ -252,7 +249,7 @@ private void processFields(Descriptor desc, MessageRules msgRules, MessageEvalua
252249
if (!fieldRules.hasIgnore()
253250
&& msgRules.getOneofList().stream()
254251
.anyMatch(oneof -> oneof.getFieldsList().contains(fieldDescriptor.getName()))) {
255-
fieldRules = fieldRules.toBuilder().setIgnore(Ignore.IGNORE_IF_UNPOPULATED).build();
252+
fieldRules = fieldRules.toBuilder().setIgnore(Ignore.IGNORE_IF_ZERO_VALUE).build();
256253
}
257254
FieldEvaluator fldEval = buildField(descriptor, fieldRules);
258255
msgEval.append(fldEval);
@@ -262,30 +259,19 @@ private void processFields(Descriptor desc, MessageRules msgRules, MessageEvalua
262259
private FieldEvaluator buildField(FieldDescriptor fieldDescriptor, FieldRules fieldRules)
263260
throws CompilationException {
264261
ValueEvaluator valueEvaluatorEval = new ValueEvaluator(fieldDescriptor, null);
265-
boolean ignoreDefault = fieldDescriptor.hasPresence() && shouldIgnoreDefault(fieldRules);
266-
Object zero = null;
267-
if (ignoreDefault) {
268-
zero = zeroValue(fieldDescriptor, false);
269-
}
270262
FieldEvaluator fieldEvaluator =
271263
new FieldEvaluator(
272264
valueEvaluatorEval,
273265
fieldDescriptor,
274266
fieldRules.getRequired(),
275267
fieldDescriptor.hasPresence(),
276-
fieldRules.getIgnore(),
277-
zero);
268+
fieldRules.getIgnore());
278269
buildValue(fieldDescriptor, fieldRules, fieldEvaluator.valueEvaluator);
279270
return fieldEvaluator;
280271
}
281272

282273
private static boolean shouldIgnoreEmpty(FieldRules rules) {
283-
return rules.getIgnore() == Ignore.IGNORE_IF_UNPOPULATED
284-
|| rules.getIgnore() == Ignore.IGNORE_IF_DEFAULT_VALUE;
285-
}
286-
287-
private static boolean shouldIgnoreDefault(FieldRules rules) {
288-
return rules.getIgnore() == Ignore.IGNORE_IF_DEFAULT_VALUE;
274+
return rules.getIgnore() == Ignore.IGNORE_IF_ZERO_VALUE;
289275
}
290276

291277
private void buildValue(

src/main/java/build/buf/protovalidate/FieldEvaluator.java

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
import com.google.protobuf.Message;
2323
import java.util.Collections;
2424
import java.util.List;
25-
import java.util.Objects;
26-
import org.jspecify.annotations.Nullable;
2725

2826
/** Performs validation on a single message field, defined by its descriptor. */
2927
final class FieldEvaluator implements Evaluator {
@@ -52,23 +50,19 @@ final class FieldEvaluator implements Evaluator {
5250
/** Whether the field distinguishes between unpopulated and default values. */
5351
private final boolean hasPresence;
5452

55-
@Nullable private final Object zero;
56-
5753
/** Constructs a new {@link FieldEvaluator} */
5854
FieldEvaluator(
5955
ValueEvaluator valueEvaluator,
6056
FieldDescriptor descriptor,
6157
boolean required,
6258
boolean hasPresence,
63-
Ignore ignore,
64-
@Nullable Object zero) {
59+
Ignore ignore) {
6560
this.helper = new RuleViolationHelper(valueEvaluator);
6661
this.valueEvaluator = valueEvaluator;
6762
this.descriptor = descriptor;
6863
this.required = required;
6964
this.hasPresence = hasPresence;
7065
this.ignore = ignore;
71-
this.zero = zero;
7266
}
7367

7468
@Override
@@ -92,17 +86,7 @@ private boolean shouldIgnoreAlways() {
9286
* set.
9387
*/
9488
private boolean shouldIgnoreEmpty() {
95-
return this.hasPresence
96-
|| this.ignore == Ignore.IGNORE_IF_UNPOPULATED
97-
|| this.ignore == Ignore.IGNORE_IF_DEFAULT_VALUE;
98-
}
99-
100-
/**
101-
* Returns whether a field should skip validation on its zero value, including for fields which
102-
* have field presence and are set to the zero value.
103-
*/
104-
private boolean shouldIgnoreDefault() {
105-
return this.hasPresence && this.ignore == Ignore.IGNORE_IF_DEFAULT_VALUE;
89+
return this.hasPresence || this.ignore == Ignore.IGNORE_IF_ZERO_VALUE;
10690
}
10791

10892
@Override
@@ -134,11 +118,7 @@ public List<RuleViolation.Builder> evaluate(Value val, boolean failFast)
134118
if (this.shouldIgnoreEmpty() && !hasField) {
135119
return RuleViolation.NO_VIOLATIONS;
136120
}
137-
Object fieldValue = message.getField(descriptor);
138-
if (this.shouldIgnoreDefault()
139-
&& Objects.equals(zero, ProtoAdapter.toCel(descriptor, fieldValue))) {
140-
return RuleViolation.NO_VIOLATIONS;
141-
}
142-
return valueEvaluator.evaluate(new ObjectValue(descriptor, fieldValue), failFast);
121+
return valueEvaluator.evaluate(
122+
new ObjectValue(descriptor, message.getField(descriptor)), failFast);
143123
}
144124
}

0 commit comments

Comments
 (0)