Skip to content

Commit 118ea23

Browse files
#39: Validate default value for proto3 (illegal)
1 parent 015ba09 commit 118ea23

File tree

5 files changed

+39
-2
lines changed

5 files changed

+39
-2
lines changed

src/main/java/io/protostuff/jetbrains/plugin/annotator/ProtoErrorsAnnotator.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,18 @@
1717
import io.protostuff.jetbrains.plugin.psi.FieldNode;
1818
import io.protostuff.jetbrains.plugin.psi.MessageField;
1919
import io.protostuff.jetbrains.plugin.psi.MessageNode;
20+
import io.protostuff.jetbrains.plugin.psi.OptionNode;
2021
import io.protostuff.jetbrains.plugin.psi.ProtoRootNode;
2122
import io.protostuff.jetbrains.plugin.psi.RangeNode;
2223
import io.protostuff.jetbrains.plugin.psi.RpcMethodNode;
2324
import io.protostuff.jetbrains.plugin.psi.ServiceNode;
2425
import io.protostuff.jetbrains.plugin.psi.Syntax;
26+
import io.protostuff.jetbrains.plugin.reference.FieldReferenceProviderImpl;
2527
import java.util.Collection;
2628
import java.util.HashMap;
2729
import java.util.List;
2830
import java.util.Map;
31+
import java.util.Objects;
2932
import java.util.Optional;
3033
import java.util.Set;
3134
import org.jetbrains.annotations.NotNull;
@@ -62,7 +65,10 @@ public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder hold
6265
checkReservedFieldTags(message, fields);
6366
checkReservedFieldNames(message, fields);
6467
} else if (element instanceof FieldNode) {
65-
checkFieldLabel((FieldNode) element, syntax);
68+
FieldNode field = (FieldNode) element;
69+
checkFieldLabel(field, syntax);
70+
} else if (element instanceof OptionNode) {
71+
checkDefaultValue((OptionNode)element, syntax);
6672
} else if (element instanceof EnumNode) {
6773
EnumNode anEnum = (EnumNode) element;
6874
List<EnumConstantNode> constants = anEnum.getConstants();
@@ -78,6 +84,24 @@ public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder hold
7884
}
7985
}
8086

87+
private void checkDefaultValue(@NotNull OptionNode option, Syntax syntax) {
88+
if (syntax != Syntax.PROTO3) {
89+
return;
90+
}
91+
PsiElement optionEntry = option.getParent();
92+
if (optionEntry != null) {
93+
PsiElement field = optionEntry.getParent();
94+
if (field instanceof MessageField) {
95+
String optionName = option.getOptionNameText();
96+
if (Objects.equals(optionName, FieldReferenceProviderImpl.DEFAULT)) {
97+
String message = message("error.default.value.not.supported");
98+
markError(option.getNode(), null, message);
99+
}
100+
}
101+
}
102+
}
103+
104+
81105
private void checkFieldLabel(FieldNode field, Syntax syntax) {
82106
switch (syntax) {
83107
case PROTO2:

src/main/java/io/protostuff/jetbrains/plugin/reference/FieldReferenceProviderImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ public class FieldReferenceProviderImpl implements FieldReferenceProvider {
5353

5454
private static final Logger LOGGER = Logger.getInstance(FieldReferenceProviderImpl.class);
5555
// "default" field option (a special case)
56-
private static final String DEFAULT = "default";
56+
public static final String DEFAULT = "default";
57+
5758
private static final Map<Class<? extends PsiElement>, String> TARGET_MAPPING
5859
= ImmutableMap.<Class<? extends PsiElement>, String>builder()
5960
.put(FieldNode.class, MSG_FIELD_OPTIONS)

src/main/resources/io/protostuff/protobuf-jetbrains-plugin/messages/ProtostuffBundle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ error.duplicate.method.name=Duplicate RPC method name: {0}
2121
error.missing.field.label=Missing field label
2222
error.illegal.field.label=Field label "{0}" is not allowed in proto3
2323
element.context.display={0} in {1}
24+
error.default.value.not.supported=Default values are not supported in proto3

src/test/java/io/protostuff/jetbrains/plugin/annotator/ProtoErrorsAnnotatorTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ public void testProto3IllegalRequiredFieldLabel() {
7878
check();
7979
}
8080

81+
public void testProto3IllegalDefaultValueOption() {
82+
check();
83+
}
84+
8185
private void check() {
8286
String file = getTestName(false) + ".proto";
8387
myFixture.configureByFiles(file);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
syntax = "proto3";
2+
3+
package annotator;
4+
5+
message TestMessage {
6+
int32 field = 1 [<error descr="Default values are not supported in proto3">default = 5</error>];
7+
}

0 commit comments

Comments
 (0)