|
12 | 12 |
|
13 | 13 | #include <cstddef> |
14 | 14 | #include <cstdint> |
| 15 | +#include <cstdlib> |
| 16 | +#include <cstring> |
15 | 17 |
|
16 | 18 | #include <gtest/gtest.h> |
17 | 19 | #include "google/protobuf/test_messages_proto2.upb.h" |
18 | 20 | #include "google/protobuf/test_messages_proto3.upb.h" |
19 | 21 | #include "upb/base/status.h" |
20 | 22 | #include "upb/base/string_view.h" |
| 23 | +#include "upb/mem/alloc.h" |
| 24 | +#include "upb/mem/arena.h" |
21 | 25 | #include "upb/mem/arena.hpp" |
22 | 26 | #include "upb/message/array.h" |
23 | 27 | #include "upb/message/map.h" |
| 28 | +#include "upb/message/message.h" |
24 | 29 | #include "upb/test/test.upb.h" |
| 30 | +#include "upb/wire/decode.h" |
| 31 | +#include "upb/wire/encode.h" |
25 | 32 |
|
26 | 33 | // Must be last. |
27 | 34 | #include "upb/port/def.inc" |
@@ -910,3 +917,40 @@ TEST(GeneratedCode, Maps) { |
910 | 917 |
|
911 | 918 | upb_Map_Set(sb, key, val, arena.ptr()); |
912 | 919 | } |
| 920 | + |
| 921 | +TEST(GeneratedCode, MapWithRequiredFields) { |
| 922 | + upb::Arena arena; |
| 923 | + upb_test_ModelWithMaps* msg = upb_test_ModelWithMaps_new(arena.ptr()); |
| 924 | + |
| 925 | + auto im_required = |
| 926 | + _upb_test_ModelWithMaps_map_im_required_mutable_upb_map(msg, arena.ptr()); |
| 927 | + |
| 928 | + ASSERT_NE(im_required, nullptr); |
| 929 | + |
| 930 | + upb_MessageValue key, val; |
| 931 | + key.int32_val = 0; |
| 932 | + val.msg_val = |
| 933 | + (const upb_Message*)upb_test_ModelWithRequiredFields_new(arena.ptr()); |
| 934 | + |
| 935 | + upb_Map_Set(im_required, key, val, arena.ptr()); |
| 936 | + |
| 937 | + // Serializing fails if we are checking required fields, but succeeds if we |
| 938 | + // don't. |
| 939 | + size_t size; |
| 940 | + char* serialized = upb_test_ModelWithMaps_serialize_ex( |
| 941 | + msg, kUpb_EncodeOption_CheckRequired, arena.ptr(), &size); |
| 942 | + ASSERT_EQ(serialized, nullptr); |
| 943 | + |
| 944 | + serialized = upb_test_ModelWithMaps_serialize_ex(msg, 0, arena.ptr(), &size); |
| 945 | + ASSERT_NE(serialized, nullptr); |
| 946 | + |
| 947 | + // Likewise, parsing fails if we are checking required fields, but succeeds |
| 948 | + // if we don't. |
| 949 | + upb_test_ModelWithMaps* msg2 = upb_test_ModelWithMaps_parse_ex( |
| 950 | + serialized, size, nullptr, kUpb_DecodeOption_CheckRequired, arena.ptr()); |
| 951 | + ASSERT_EQ(msg2, nullptr); |
| 952 | + |
| 953 | + msg2 = upb_test_ModelWithMaps_parse_ex(serialized, size, nullptr, 0, |
| 954 | + arena.ptr()); |
| 955 | + ASSERT_NE(msg2, nullptr); |
| 956 | +} |
0 commit comments