Skip to content

Commit

Permalink
Throw if one sets a null to a omit-identity enum in Java
Browse files Browse the repository at this point in the history
  • Loading branch information
oldergod committed Aug 4, 2020
1 parent 98dafdf commit 8343d8a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1404,9 +1404,11 @@ private MethodSpec messageConstructor(
result.addParameter(param.build());
}

if (field.getEncodeMode() == Field.EncodeMode.OMIT_IDENTITY && field.getType().isScalar()) {
if (field.getEncodeMode() == Field.EncodeMode.OMIT_IDENTITY) {
// Other scalars use not-boxed types to guarantee a value.
if (field.getType() == ProtoType.STRING || field.getType() == ProtoType.BYTES) {
if (field.getType().isScalar()
&& (field.getType() == ProtoType.STRING || field.getType() == ProtoType.BYTES)
|| isEnum(field.getType())) {
result.beginControlFlow("if ($L == null)", fieldAccessName);
result.addStatement("throw new IllegalArgumentException($S)",
fieldAccessName + " == null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,15 @@ class Proto3WireProtocCompatibilityTests {
}
}

@Test fun cannotPassNullToIdentityEnum(){
try {
AllTypesJ.Builder().nested_enum(null).build()
fail()
} catch (exception: IllegalArgumentException) {
assertThat(exception).hasMessage("builder.nested_enum == null")
}
}

companion object {
private val defaultAllTypesProtoc = AllTypesOuterClass.AllTypes.newBuilder()
.setMyInt32(111)
Expand Down

0 comments on commit 8343d8a

Please sign in to comment.