unknown_fields_collector_ut.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #include "json.h"
  2. #include "proto.h"
  3. #include "proto2json.h"
  4. #include <library/cpp/protobuf/json/json2proto.h>
  5. #include <library/cpp/testing/unittest/registar.h>
  6. #include <util/generic/set.h>
  7. #include <util/generic/string.h>
  8. using namespace NProtobufJson;
  9. using namespace NProtobufJsonTest;
  10. struct TTestUnknownFieldsCollector : public IUnknownFieldsCollector {
  11. void OnEnterMapItem(const TString& key) override {
  12. CurrentPath.push_back(key);
  13. }
  14. void OnEnterArrayItem(ui64 id) override {
  15. CurrentPath.push_back(ToString(id));
  16. }
  17. void OnLeaveMapItem() override {
  18. CurrentPath.pop_back();
  19. }
  20. void OnLeaveArrayItem() override {
  21. CurrentPath.pop_back();
  22. }
  23. void OnUnknownField(const TString& key, const google::protobuf::Descriptor& value) override {
  24. TString path;
  25. for (auto& piece : CurrentPath) {
  26. path.append("/");
  27. path.append(piece);
  28. }
  29. path.append("/");
  30. path.append(key);
  31. UnknownKeys.insert(std::move(path));
  32. Y_UNUSED(value);
  33. }
  34. TVector<TString> CurrentPath;
  35. TSet<TString> UnknownKeys;
  36. };
  37. Y_UNIT_TEST_SUITE(TUnknownFieldsCollectorTest) {
  38. Y_UNIT_TEST(TestFlatOptional) {
  39. TFlatOptional proto;
  40. TSimpleSharedPtr<TTestUnknownFieldsCollector> collector = new TTestUnknownFieldsCollector;
  41. TJson2ProtoConfig cfg;
  42. cfg.SetUnknownFieldsCollector(collector).SetAllowUnknownFields(true);
  43. Json2Proto(TStringBuf(R"({"42":42,"I32":11,"test":2,"string":"str","String":"string","obj":{"inner":{}},"arr":[1,2,3]})"), proto, cfg);
  44. TSet<TString> expectedKeys = {
  45. {"/42"},
  46. {"/arr"},
  47. {"/obj"},
  48. {"/string"},
  49. {"/test"},
  50. };
  51. UNIT_ASSERT(collector->CurrentPath.empty());
  52. UNIT_ASSERT_VALUES_EQUAL(collector->UnknownKeys, expectedKeys);
  53. }
  54. Y_UNIT_TEST(TestFlatRepeated) {
  55. TFlatRepeated proto;
  56. TSimpleSharedPtr<TTestUnknownFieldsCollector> collector = new TTestUnknownFieldsCollector;
  57. TJson2ProtoConfig cfg;
  58. cfg.SetUnknownFieldsCollector(collector).SetAllowUnknownFields(true);
  59. Json2Proto(TStringBuf(R"({"42":42,"I32":[11,12],"test":12,"string":"str","String":["string1","string2"],"obj":{"inner":{}},"arr":[1,2,3]})"), proto, cfg);
  60. TSet<TString> expectedKeys = {
  61. {"/42"},
  62. {"/arr"},
  63. {"/obj"},
  64. {"/string"},
  65. {"/test"},
  66. };
  67. UNIT_ASSERT(collector->CurrentPath.empty());
  68. UNIT_ASSERT_VALUES_EQUAL(collector->UnknownKeys, expectedKeys);
  69. }
  70. Y_UNIT_TEST(TestCompositeOptional) {
  71. TCompositeOptional proto;
  72. TSimpleSharedPtr<TTestUnknownFieldsCollector> collector = new TTestUnknownFieldsCollector;
  73. TJson2ProtoConfig cfg;
  74. cfg.SetUnknownFieldsCollector(collector).SetAllowUnknownFields(true);
  75. Json2Proto(TStringBuf(R"({"Part":{"42":42,"I32":11,"test":12,"string":"str","String":"string"},"string2":"str"})"), proto, cfg);
  76. TSet<TString> expectedKeys = {
  77. {"/Part/42"},
  78. {"/Part/string"},
  79. {"/Part/test"},
  80. {"/string2"},
  81. };
  82. UNIT_ASSERT(collector->CurrentPath.empty());
  83. UNIT_ASSERT_VALUES_EQUAL(collector->UnknownKeys, expectedKeys);
  84. }
  85. Y_UNIT_TEST(TestCompositeRepeated) {
  86. TCompositeRepeated proto;
  87. TSimpleSharedPtr<TTestUnknownFieldsCollector> collector = new TTestUnknownFieldsCollector;
  88. TJson2ProtoConfig cfg;
  89. cfg.SetUnknownFieldsCollector(collector).SetAllowUnknownFields(true);
  90. Json2Proto(TStringBuf(R"({"Part":[)"
  91. R"( {"42":42,"I32":11,"test":12,"string":"str","String":"string"},)"
  92. R"( {"abc":"d"})"
  93. R"(],)"
  94. R"("string2":"str"})"), proto, cfg);
  95. TSet<TString> expectedKeys = {
  96. {"/Part/0/42"},
  97. {"/Part/0/string"},
  98. {"/Part/0/test"},
  99. {"/Part/1/abc"},
  100. {"/string2"},
  101. };
  102. UNIT_ASSERT(collector->CurrentPath.empty());
  103. UNIT_ASSERT_VALUES_EQUAL(collector->UnknownKeys, expectedKeys);
  104. }
  105. Y_UNIT_TEST(TestCompleMapType) {
  106. TComplexMapType proto;
  107. TSimpleSharedPtr<TTestUnknownFieldsCollector> collector = new TTestUnknownFieldsCollector;
  108. TJson2ProtoConfig cfg;
  109. cfg.SetUnknownFieldsCollector(collector).SetAllowUnknownFields(true);
  110. Json2Proto(TStringBuf(R"({"42":42,)"
  111. R"("Nested":[)"
  112. R"( {"key":"abc","value":{"string":"string","Nested":[{"key":"def","value":{"string2":"string2"}}]}},)"
  113. R"( {"key":"car","value":{"string3":"string3"}})"
  114. R"(]})"), proto, cfg);
  115. TSet<TString> expectedKeys = {
  116. {"/42"},
  117. {"/Nested/0/value/Nested/0/value/string2"},
  118. {"/Nested/0/value/string"},
  119. {"/Nested/1/value/string3"},
  120. };
  121. UNIT_ASSERT(collector->CurrentPath.empty());
  122. UNIT_ASSERT_VALUES_EQUAL(collector->UnknownKeys, expectedKeys);
  123. }
  124. Y_UNIT_TEST(TestCompleMapTypeMapAsObject) {
  125. TComplexMapType proto;
  126. TSimpleSharedPtr<TTestUnknownFieldsCollector> collector = new TTestUnknownFieldsCollector;
  127. TJson2ProtoConfig cfg;
  128. cfg.SetUnknownFieldsCollector(collector).SetAllowUnknownFields(true).SetMapAsObject(true);
  129. Json2Proto(TStringBuf(R"({"42":42,)"
  130. R"("Nested":{)"
  131. R"( "abc":{"string":"string","Nested":{"def":{"string2":"string2"}}},)"
  132. R"( "car":{"string3":"string3"})"
  133. R"(}})"), proto, cfg);
  134. TSet<TString> expectedKeys = {
  135. {"/42"},
  136. {"/Nested/abc/Nested/def/string2"},
  137. {"/Nested/abc/string"},
  138. {"/Nested/car/string3"},
  139. };
  140. UNIT_ASSERT(collector->CurrentPath.empty());
  141. UNIT_ASSERT_VALUES_EQUAL(collector->UnknownKeys, expectedKeys);
  142. }
  143. } // TJson2ProtoTest