Skip to content

Commit

Permalink
Consider java lang while forming GEN_STRING with validation enabled (#…
Browse files Browse the repository at this point in the history
…306)

* Include protoc gen validate for java lang

* Add test for protoc gen java with validator

* fix: Add validations and assertions for the java validator

* fix: Fix review comments

* fix: Fix review comments

Co-authored-by: Venkat Duddu <[email protected]>
  • Loading branch information
venkatduddu and Venkat Duddu authored Jun 7, 2022
1 parent 2244179 commit d8dca39
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
4 changes: 4 additions & 0 deletions all/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,10 @@ if [[ $GEN_VALIDATOR == true && $GEN_LANG == "gogo" ]]; then
GEN_STRING="$GEN_STRING --validate_out=lang=gogo${VALIDATOR_SOURCE_RELATIVE}:$OUT_DIR"
fi

if [[ $GEN_VALIDATOR == true && $GEN_LANG == "java" ]]; then
GEN_STRING="$GEN_STRING --validate_out=lang=java:$OUT_DIR"
fi

if [[ $GEN_DOCS == true ]]; then
mkdir -p $OUT_DIR/doc
GEN_STRING="$GEN_STRING --doc_opt=$DOCS_FORMAT --doc_out=$OUT_DIR/doc"
Expand Down
24 changes: 21 additions & 3 deletions all/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,8 @@ testGeneration() {
fi
fi

# Test that we have generated the test.pb.go file.
expected_file_name="/all/test.pb.go"
if [[ "$extra_args" == *"--with-validator"* ]]; then
# Test that we have generated the test.pb.go file.
if [[ "$extra_args" == *"--with-validator"* ]] && [[ "$lang" == "go" ]]; then
expected_file_name1="/all/test.pb.go"
expected_file_name2="/all/test.pb.validate.go"
if [[ "$extra_args" == *"--validator-source-relative"* ]]; then
Expand All @@ -207,6 +206,22 @@ testGeneration() {
fi
fi

if [[ "$extra_args" == *"--with-validator"* ]] && [[ "$lang" == "java" ]]; then
# Test that we have generated the Test.java and TestValidator.java files
java_file_count=$(find $expected_output_dir -type f -name "Test.java" | wc -l)
if [ $java_file_count -ne 1 ]; then
echo >&2 "[Fail] $name"
echo >&2 "Test.java file was not generated in $expected_output_dir"
exit 1
fi
java_validator_file_count=$(find $expected_output_dir -type f -name "TestValidator.java" | wc -l)
if [ $java_validator_file_count -ne 1 ]; then
echo >&2 "[Fail] $name"
echo >&2 "TestValidator.java file was not generated in $expected_output_dir"
exit 1
fi
fi

if [[ "$extra_args" == *"--with-go-proto-validator"* ]]; then
expected_file_name1="/all/test.pb.go"
expected_file_name2="/all/test.pb.validate.go"
Expand Down Expand Up @@ -347,6 +362,9 @@ testGeneration "web_with_commonjs_imports" web "gen/pb-web" 0 --grpc-web-out imp
# Test java output
testGeneration "java_test_jar" java "gen" 0 -o gen/test.jar

# Test java output
testGeneration "java_with_validator" java "gen/pb-java" 0 --with-validator

# Generate proto files
for lang in ${LANGS[@]}; do
expected_output_dir=""
Expand Down
3 changes: 2 additions & 1 deletion all/test/test.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package Messages;
import "all/test/include.proto";
import "google/api/annotations.proto";
import "google/protobuf/field_mask.proto";
import "validate/validate.proto";

option go_package = "all/;all";

Expand All @@ -29,7 +30,7 @@ message ListMessageRequest {
}

message UpdateMessageRequest {
string name = 1;
string name = 1 [(validate.rules).string.len = 1];
google.protobuf.FieldMask update_mask = 2;
}

Expand Down

0 comments on commit d8dca39

Please sign in to comment.