diff options
author | AlexSm <alex@ydb.tech> | 2023-12-22 20:30:08 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-12-22 20:30:08 +0100 |
commit | 6b4f46a6883f21c16eef367106167ea59932515e (patch) | |
tree | 5833709b16bba3948f15e88b9038229c5065605b /library/cpp/protobuf/json/proto2json_printer.cpp | |
parent | a8b737f2a6df02f6e45ed703246c75f0f4cf7a34 (diff) | |
download | ydb-6b4f46a6883f21c16eef367106167ea59932515e.tar.gz |
Import libs 3 (#679)
Diffstat (limited to 'library/cpp/protobuf/json/proto2json_printer.cpp')
-rw-r--r-- | library/cpp/protobuf/json/proto2json_printer.cpp | 10 |
1 files changed, 7 insertions, 3 deletions
diff --git a/library/cpp/protobuf/json/proto2json_printer.cpp b/library/cpp/protobuf/json/proto2json_printer.cpp index 456d1c43c5..aa0823de0e 100644 --- a/library/cpp/protobuf/json/proto2json_printer.cpp +++ b/library/cpp/protobuf/json/proto2json_printer.cpp @@ -195,6 +195,9 @@ namespace NProtobufJson { using namespace google::protobuf; auto type = proto.GetDescriptor()->well_known_type(); + // XXX static_cast will cause UB if used with dynamic messages + // (can be created by a DynamicMessageFactory with SetDelegateToGeneratedFactory(false). Unlikely, but still possible). + // See workaround with CopyFrom in JsonString2Duration, JsonString2Timestamp (json2proto.cpp) if (type == Descriptor::WellKnownType::WELLKNOWNTYPE_DURATION) { const auto& duration = static_cast<const Duration&>(proto); json.Write(util::TimeUtil::ToString(duration)); @@ -210,7 +213,8 @@ namespace NProtobufJson { void TProto2JsonPrinter::PrintSingleField(const Message& proto, const FieldDescriptor& field, IJsonOutput& json, - TStringBuf key) { + TStringBuf key, + bool inProtoMap) { Y_ABORT_UNLESS(!field.is_repeated(), "field is repeated."); if (!key) { @@ -236,7 +240,7 @@ namespace NProtobufJson { const Reflection* reflection = proto.GetReflection(); - bool shouldPrintField = reflection->HasField(proto, &field); + bool shouldPrintField = inProtoMap || reflection->HasField(proto, &field); if (!shouldPrintField && GetConfig().MissingSingleKeyMode == TProto2JsonConfig::MissingKeyExplicitDefaultThrowRequired) { if (field.has_default_value()) { shouldPrintField = true; @@ -409,7 +413,7 @@ namespace NProtobufJson { TString key = MakeKey(proto, *keyField); const FieldDescriptor* valueField = proto.GetDescriptor()->FindFieldByName("value"); Y_ABORT_UNLESS(valueField, "Map entry value field not found."); - PrintField(proto, *valueField, json, key); + PrintSingleField(proto, *valueField, json, key, true); } TString TProto2JsonPrinter::MakeKey(const NProtoBuf::Message& proto, |