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
18 changes: 12 additions & 6 deletions src/main/java/build/buf/protovalidate/FieldEvaluator.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,7 @@ public List<RuleViolation.Builder> evaluate(Value val, boolean failFast)
if (message == null) {
return RuleViolation.NO_VIOLATIONS;
}
boolean hasField;
if (descriptor.isRepeated()) {
hasField = message.getRepeatedFieldCount(descriptor) != 0;
} else {
hasField = message.hasField(descriptor);
}
boolean hasField = isFieldSet(message, descriptor);
if (required && !hasField) {
return Collections.singletonList(
RuleViolation.newBuilder()
Expand All @@ -121,4 +116,15 @@ public List<RuleViolation.Builder> evaluate(Value val, boolean failFast)
return valueEvaluator.evaluate(
new ObjectValue(descriptor, message.getField(descriptor)), failFast);
}

/**
* Returns whether the given field is set on the message. Handles repeated and map fields, which
* are not supported by {@link Message#hasField}.
*/
static boolean isFieldSet(Message message, FieldDescriptor field) {
if (field.isRepeated()) {
return message.getRepeatedFieldCount(field) != 0;
}
return message.hasField(field);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public List<RuleViolation.Builder> evaluate(Value val, boolean failFast)
}
int hasCount = 0;
for (FieldDescriptor field : fields) {
if (msg.hasField(field)) {
if (FieldEvaluator.isFieldSet(msg, field)) {
hasCount++;
}
}
Expand Down
59 changes: 59 additions & 0 deletions src/test/java/build/buf/protovalidate/Issue427Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2023-2025 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package build.buf.protovalidate;

import static org.assertj.core.api.Assertions.assertThat;

import build.buf.validate.Violation;
import org.junit.jupiter.api.Test;

public class Issue427Test {
@Test
public void testMessageOneofWithNameOnly() throws Exception {
com.example.imports.validationtest.Issue427 msg =
com.example.imports.validationtest.Issue427.newBuilder().setName("foo").build();
Validator validator = ValidatorFactory.newBuilder().build();
assertThat(validator.validate(msg).toProto().getViolationsList()).isEmpty();
}

@Test
public void testMessageOneofWithTagsOnly() throws Exception {
com.example.imports.validationtest.Issue427 msg =
com.example.imports.validationtest.Issue427.newBuilder().addTags("a").addTags("b").build();
Validator validator = ValidatorFactory.newBuilder().build();
assertThat(validator.validate(msg).toProto().getViolationsList()).isEmpty();
}

@Test
public void testMessageOneofWithMappingsOnly() throws Exception {
com.example.imports.validationtest.Issue427 msg =
com.example.imports.validationtest.Issue427.newBuilder().putMappings("k", "v").build();
Validator validator = ValidatorFactory.newBuilder().build();
assertThat(validator.validate(msg).toProto().getViolationsList()).isEmpty();
}

@Test
public void testMessageOneofNoneSet() throws Exception {
com.example.imports.validationtest.Issue427 msg =
com.example.imports.validationtest.Issue427.getDefaultInstance();
Validator validator = ValidatorFactory.newBuilder().build();
assertThat(validator.validate(msg).toProto().getViolationsList())
.containsExactly(
Violation.newBuilder()
.setRuleId("message.oneof")
.setMessage("one of name, tags, mappings must be set")
.build());
}
}
29 changes: 29 additions & 0 deletions src/test/resources/proto/validationtest/issue427.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2023-2025 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto3";

package validationtest;

import "buf/validate/validate.proto";

message Issue427 {
option (buf.validate.message).oneof = {
fields: ["name", "tags", "mappings"]
required: true
};
string name = 1;
repeated string tags = 2;
map<string, string> mappings = 3;
}