Skip to content

Commit 99424d6

Browse files
Avoid warning for assumed ID fields
1 parent 46c22f7 commit 99424d6

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

java-runtime/src/main/java/io/spine/validate/option/Required.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import com.google.common.collect.ImmutableSet;
3030
import com.google.errorprone.annotations.Immutable;
3131
import com.google.protobuf.Descriptors.FieldDescriptor.JavaType;
32+
import io.spine.base.CommandMessage;
33+
import io.spine.base.EntityState;
3234
import io.spine.code.proto.FieldContext;
3335
import io.spine.code.proto.FieldDeclaration;
3436
import io.spine.logging.WithLogging;
@@ -101,6 +103,18 @@ public boolean shouldValidate(FieldContext context) {
101103
void checkUsage(FieldDeclaration field) {
102104
var type = field.javaType();
103105
if (!CAN_BE_REQUIRED.contains(type) && field.isNotCollection()) {
106+
var isTheFirstField = field.descriptor().getIndex() == 0;
107+
if (isTheFirstField) {
108+
// The first field declared in a message type could be assumed as required
109+
// because by convention it is an ID field of the message.
110+
// If so, do not log the warning message for this field because ID fields
111+
// could be of any reasonable type.
112+
var messageClass = field.messageType().javaClass();
113+
if (CommandMessage.class.isAssignableFrom(messageClass)
114+
|| EntityState.class.isAssignableFrom(messageClass)) {
115+
return;
116+
}
117+
}
104118
var typeName = field.descriptor().getType().name();
105119
logger().atWarning().log(() -> format(
106120
"The field `%s.%s` has the type %s and" +

0 commit comments

Comments
 (0)