Fix a bad cast in put<X>BuilderIfAbsent when already present due to using the result of put() directly (which is null if it currently has no value)

PiperOrigin-RevId: 565425310
pull/14088/head
Protobuf Team Bot 1 year ago committed by Copybara-Service
parent bc0a87423e
commit 785c82dbde
  1. 5
      java/core/src/test/java/com/google/protobuf/MapTest.java
  2. 9
      src/google/protobuf/compiler/java/map_field.cc

@ -553,11 +553,16 @@ public class MapTest {
public void testPutBuilderIfAbsent() {
TestMap.Builder builder = TestMap.newBuilder();
MessageValue.Builder subBuilder = builder.putInt32ToMessageFieldBuilderIfAbsent(1);
assertThat(builder.putInt32ToMessageFieldBuilderIfAbsent(1)).isSameInstanceAs(subBuilder);
subBuilder.setValue(11);
assertThat(builder.getInt32ToMessageFieldOrThrow(1).getValue()).isEqualTo(11);
builder.putInt32ToMessageFieldBuilderIfAbsent(1).setValue(22);
assertThat(builder.getInt32ToMessageFieldOrThrow(1).getValue()).isEqualTo(22);
builder.putInt32ToMessageField(2, MessageValue.newBuilder().setValue(33).build());
assertThat(builder.putInt32ToMessageFieldBuilderIfAbsent(2).getValue()).isEqualTo(33);
}
@Test

@ -877,10 +877,11 @@ void ImmutableMapFieldGenerator::GenerateMessageMapBuilderMembers(
" entry = $value_type$.newBuilder();\n"
" builderMap.put(key, entry);\n"
" }\n"
" if (entry instanceof $value_builder_type$) { return "
"($value_builder_type$) entry; }\n"
" return ($value_builder_type$) builderMap.put(key, "
"(($value_type$) entry).toBuilder());\n"
" if (entry instanceof $value_type$) {\n"
" entry = (($value_type$) entry).toBuilder();\n"
" builderMap.put(key, entry);\n"
" }\n"
" return ($value_builder_type$) entry;\n"
"}\n");
printer->Annotate("{", "}", descriptor_, Semantic::kSet);
}

Loading…
Cancel
Save