Skip to content
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

Strict conformance fixes #212

Merged
merged 2 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ mavenPublishing {
dependencies {
annotationProcessor(libs.nullaway)
api(libs.protobuf.java)
implementation(libs.protobuf.java.util)
jchadwick-buf marked this conversation as resolved.
Show resolved Hide resolved
implementation(enforcedPlatform(libs.cel))
implementation(libs.cel.core)
implementation(libs.guava)
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
protovalidate.version = v0.8.2

# Arguments to the protovalidate-conformance CLI
protovalidate.conformance.args = --strict_message --strict_error
protovalidate.conformance.args = --strict --strict_message --strict_error

# Argument to the license-header CLI
license-header.years = 2023-2024
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ junit-bom = { module = "org.junit:junit-bom", version.ref = "junit" }
maven-plugin = { module = "com.vanniktech:gradle-maven-publish-plugin", version.ref = "maven-publish" }
nullaway = { module = "com.uber.nullaway:nullaway", version = "0.12.1" }
protobuf-java = { module = "com.google.protobuf:protobuf-java", version.ref = "protobuf" }
protobuf-java-util = { module = "com.google.protobuf:protobuf-java-util", version.ref = "protobuf" }
spotless = { module = "com.diffplug.spotless:spotless-plugin-gradle", version = "6.25.0" }

[plugins]
Expand Down
16 changes: 11 additions & 5 deletions src/main/java/build/buf/protovalidate/internal/celext/Format.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import com.google.protobuf.Duration;
import com.google.protobuf.Timestamp;
import com.google.protobuf.util.Timestamps;
import java.nio.charset.StandardCharsets;
import java.text.DecimalFormat;
import java.util.List;
Expand Down Expand Up @@ -139,7 +140,10 @@ private static void formatString(StringBuilder builder, Val val) {
if (val.type().typeEnum() == TypeEnum.String) {
builder.append(val.value());
} else if (val.type().typeEnum() == TypeEnum.Bytes) {
builder.append(val.value());
builder.append(new String((byte[]) val.value(), StandardCharsets.UTF_8));
} else if (val.type().typeEnum() == TypeEnum.Double) {
DecimalFormat format = new DecimalFormat();
builder.append(format.format(val.value()));
} else {
formatStringSafe(builder, val, false);
}
Expand All @@ -159,7 +163,11 @@ private static void formatStringSafe(StringBuilder builder, Val val, boolean lis
} else if (type == TypeEnum.Int || type == TypeEnum.Uint) {
formatDecimal(builder, val);
} else if (type == TypeEnum.Double) {
DecimalFormat format = new DecimalFormat("0.#");
// When a double is nested in another type (e.g. a list) it will have a minimum of 6 decimal
// digits. This is to maintain consistency with the Go CEL runtime.
DecimalFormat format = new DecimalFormat();
format.setMaximumFractionDigits(Integer.MAX_VALUE);
format.setMinimumFractionDigits(6);
builder.append(format.format(val.value()));
} else if (type == TypeEnum.String) {
builder.append("\"").append(val.value().toString()).append("\"");
Expand Down Expand Up @@ -205,10 +213,8 @@ private static void formatList(StringBuilder builder, Val val) {
* @param val the value to format.
*/
private static void formatTimestamp(StringBuilder builder, Val val) {
builder.append("timestamp(");
Timestamp timestamp = val.convertToNative(Timestamp.class);
builder.append(timestamp.toString());
builder.append(")");
builder.append(Timestamps.toString(timestamp));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/** Performs validation on a map field's key-value pairs. */
class MapEvaluator implements Evaluator {
Expand Down Expand Up @@ -84,7 +85,10 @@ public ValidationResult evaluate(Value val, boolean failFast) throws ExecutionEx

private List<Violation> evalPairs(Value key, Value value, boolean failFast)
throws ExecutionException {
List<Violation> keyViolations = keyEvaluator.evaluate(key, failFast).getViolations();
List<Violation> keyViolations =
keyEvaluator.evaluate(key, failFast).getViolations().stream()
.map(violation -> violation.toBuilder().setForKey(true).build())
.collect(Collectors.toList());
final List<Violation> valueViolations;
if (failFast && !keyViolations.isEmpty()) {
// Don't evaluate value constraints if failFast is enabled and keys failed validation.
Expand Down