|
29 | 29 | import com.google.common.collect.ImmutableSet;
|
30 | 30 | import com.google.errorprone.annotations.Immutable;
|
31 | 31 | import com.google.protobuf.Descriptors.FieldDescriptor.JavaType;
|
| 32 | +import io.spine.base.CommandMessage; |
| 33 | +import io.spine.base.EntityState; |
32 | 34 | import io.spine.code.proto.FieldContext;
|
33 | 35 | import io.spine.code.proto.FieldDeclaration;
|
34 | 36 | import io.spine.logging.WithLogging;
|
@@ -101,6 +103,18 @@ public boolean shouldValidate(FieldContext context) {
|
101 | 103 | void checkUsage(FieldDeclaration field) {
|
102 | 104 | var type = field.javaType();
|
103 | 105 | 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 | + } |
104 | 118 | var typeName = field.descriptor().getType().name();
|
105 | 119 | logger().atWarning().log(() -> format(
|
106 | 120 | "The field `%s.%s` has the type %s and" +
|
|
0 commit comments