Null string values in map in Java
What version of protobuf and what language are you using? Version: 3.20.0 Language: Java
What operating system (Linux, Windows, ...) and version? Mac
What runtime / compiler are you using (e.g., python version or gcc version) Java 17
What did you do? Steps to reproduce the behavior:
- Create a message:
message MapContainer {
map<string,string> my_map = 1;
}
- Run the following:
FieldDescriptor field = MapContainer.getDescriptor().findFieldByName("my_map");
Descriptor entryDescriptor = field.getMessageType();
FieldDescriptor keyDescriptor = entryDescriptor.findFieldByName("key");
FieldDescriptor valueDescriptor = entryDescriptor.findFieldByName("value");
Builder b = MapContainer.newBuilder().newBuilderForField(field);
b.setField(keyDescriptor, "foo");
b.setField(valueDescriptor, null);
MapContainer out = MapContainer.newBuilder().setField(field, Collections.singletonList(b.build())).build();
System.out.println(out.toString());
What did you expect to see The setField or build should throw an exception.
I understand the setField is not the same as the specific set or put methods on the builder. The putters throw exceptions so this should generally fail fast.
MapContainer.newBuilder().putMyMap("key", null).build();
MapContainer.newBuilder().putMyMap(Collections.singletonMap("key", null)).build();
It seems to be bad to allow this to be fully built.
What did you see instead? There is a NPE when toString(), getSerializedSize(), or toByteArray() are called.
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.getBytes(java.nio.charset.Charset)" because "text" is null
at com.google.protobuf.ByteString.copyFromUtf8(ByteString.java:522)
at com.google.protobuf.TextFormatEscaper.escapeText(TextFormatEscaper.java:153)
at com.google.protobuf.TextFormat$Printer.printFieldValue(TextFormat.java:590)
at com.google.protobuf.TextFormat$Printer.printSingleField(TextFormat.java:757)
at com.google.protobuf.TextFormat$Printer.printField(TextFormat.java:459)
at com.google.protobuf.TextFormat$Printer.printMessage(TextFormat.java:719)
at com.google.protobuf.TextFormat$Printer.print(TextFormat.java:369)
at com.google.protobuf.TextFormat$Printer.printFieldValue(TextFormat.java:611)
at com.google.protobuf.TextFormat$Printer.printSingleField(TextFormat.java:757)
at com.google.protobuf.TextFormat$Printer.printField(TextFormat.java:451)
at com.google.protobuf.TextFormat$Printer.printMessage(TextFormat.java:719)
at com.google.protobuf.TextFormat$Printer.print(TextFormat.java:369)
at com.google.protobuf.TextFormat$Printer.print(TextFormat.java:355)
at com.google.protobuf.TextFormat$Printer.printToString(TextFormat.java:620)
at com.google.protobuf.AbstractMessage.toString(AbstractMessage.java:111)
...
Make sure you include information that can help us debug (full error message, exception listing, stack trace, logs).
Anything else we should know about your project / environment
I think a fix may be to add some validation somewhere in this method: https://github.com/protocolbuffers/protobuf/blob/v3.20.0/java/core/src/main/java/com/google/protobuf/MapEntry.java#L342 but I don't know what the semantics should be with respect to default values or other non-string types or other non-JavaObject types. I'm generally of the position it should fail fast.
I found the following in my research:
- https://developers.google.com/protocol-buffers/docs/reference/csharp/class/google/protobuf/collections/map-field-t-key-t-value-
- https://developers.google.com/protocol-buffers/docs/proto3#json
In general I was troubleshooting something from https://github.com/HubSpot/jackson-datatype-protobuf/blob/5d403ef47a07a13433cfdca08fb6e6fe1ec7a96b/src/main/java/com/hubspot/jackson/datatype/protobuf/ProtobufDeserializer.java#L122
I was tried to deserialize JSON {"myMap": {"key": null}} and expecting to get an empty myMap field in my protobuf message.
Elliotte, did this change recently?
please tell me how to ressolve a issue i am new in github
I don't think anything here changed recently, but I'll check. I have noticed that descriptors in maps have some holes in design and testing so this could easily have been lurking for a while.
I've reproduced this at head. I need to think a little more about what should happen here, but the existing behavior certainly doesn't feel right.
Probably need a null check here in GeneratedMessage:
public BuilderType setField(final FieldDescriptor field, final Object value) { internalGetFieldAccessorTable().getField(field).set(this, value);
return (BuilderType) this;
}
There's also a setField method in MapEntry we need to fix too.
Turns out GeneratedMessage didn't need a new null check but MapEntry did. In any case, this is fixed in google3 and will make its way into github in the next sync. Thanks for the report!