aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorColin Cross <ccross@android.com>2019-08-09 16:45:22 -0700
committerColin Cross <ccross@android.com>2019-09-05 14:44:01 -0700
commite2022b3814ff3ef0a48a6d56c9cf3e893bf56a93 (patch)
treea2ff5b4b54d21324fe2cbcefd8db2d5d48213178 /src
parentb00632a0b053838a7e14f80b42165f8dc24c2121 (diff)
downloadprotobuf-e2022b3814ff3ef0a48a6d56c9cf3e893bf56a93.tar.gz
Revert "Backport upstream changes to allow printing field names verbatim."
This reverts commit 6979a8253ba28d1cbe53d5cbec83faaf4491320e. Replacing with merge from upstream. Bug: 117607748 Test: m checkbuild Change-Id: Idb49dde7db8d76e06f211e21db63ce3fbfad4a2e Merged-In: Idb49dde7db8d76e06f211e21db63ce3fbfad4a2e
Diffstat (limited to 'src')
-rw-r--r--src/google/protobuf/util/internal/default_value_objectwriter.cc14
-rw-r--r--src/google/protobuf/util/internal/default_value_objectwriter.h12
-rw-r--r--src/google/protobuf/util/internal/protostream_objectsource.cc10
-rw-r--r--src/google/protobuf/util/internal/protostream_objectsource.h8
-rw-r--r--src/google/protobuf/util/internal/protostream_objectsource_test.cc59
-rw-r--r--src/google/protobuf/util/internal/testdata/books.proto3
-rw-r--r--src/google/protobuf/util/json_util.cc4
-rw-r--r--src/google/protobuf/util/json_util.h5
-rw-r--r--src/google/protobuf/util/json_util_test.cc36
9 files changed, 6 insertions, 145 deletions
diff --git a/src/google/protobuf/util/internal/default_value_objectwriter.cc b/src/google/protobuf/util/internal/default_value_objectwriter.cc
index 22fa0e42e..21d7a2e4a 100644
--- a/src/google/protobuf/util/internal/default_value_objectwriter.cc
+++ b/src/google/protobuf/util/internal/default_value_objectwriter.cc
@@ -64,7 +64,6 @@ DefaultValueObjectWriter::DefaultValueObjectWriter(
type_(type),
current_(NULL),
root_(NULL),
- preserve_proto_field_names_(false),
field_scrub_callback_(NULL),
ow_(ow) {}
@@ -190,7 +189,6 @@ DefaultValueObjectWriter::Node::Node(const string& name,
NodeKind kind, const DataPiece& data,
bool is_placeholder,
const vector<string>& path,
- bool preserve_proto_field_names,
FieldScrubCallBack* field_scrub_callback)
: name_(name),
type_(type),
@@ -199,7 +197,6 @@ DefaultValueObjectWriter::Node::Node(const string& name,
data_(data),
is_placeholder_(is_placeholder),
path_(path),
- preserve_proto_field_names_(preserve_proto_field_names),
field_scrub_callback_(field_scrub_callback) {}
DefaultValueObjectWriter::Node* DefaultValueObjectWriter::Node::FindChild(
@@ -369,7 +366,7 @@ void DefaultValueObjectWriter::Node::PopulateChildren(
field.json_name(), field_type, kind,
kind == PRIMITIVE ? CreateDefaultDataPieceForField(field, typeinfo)
: DataPiece::NullData(),
- true, path, preserve_proto_field_names_, field_scrub_callback_));
+ true, path, field_scrub_callback_));
new_children.push_back(child.release());
}
// Adds all leftover nodes in children_ to the beginning of new_child.
@@ -465,8 +462,7 @@ DefaultValueObjectWriter* DefaultValueObjectWriter::StartObject(
if (current_ == NULL) {
vector<string> path;
root_.reset(new Node(name.ToString(), &type_, OBJECT, DataPiece::NullData(),
- false, path, preserve_proto_field_names_,
- field_scrub_callback_.get()));
+ false, path, field_scrub_callback_.get()));
root_->PopulateChildren(typeinfo_);
current_ = root_.get();
return this;
@@ -482,7 +478,6 @@ DefaultValueObjectWriter* DefaultValueObjectWriter::StartObject(
: NULL),
OBJECT, DataPiece::NullData(), false,
child == NULL ? current_->path() : child->path(),
- preserve_proto_field_names_,
field_scrub_callback_.get()));
child = node.get();
current_->AddChild(node.release());
@@ -514,8 +509,7 @@ DefaultValueObjectWriter* DefaultValueObjectWriter::StartList(
if (current_ == NULL) {
vector<string> path;
root_.reset(new Node(name.ToString(), &type_, LIST, DataPiece::NullData(),
- false, path, preserve_proto_field_names_,
- field_scrub_callback_.get()));
+ false, path, field_scrub_callback_.get()));
current_ = root_.get();
return this;
}
@@ -525,7 +519,6 @@ DefaultValueObjectWriter* DefaultValueObjectWriter::StartList(
google::protobuf::scoped_ptr<Node> node(
new Node(name.ToString(), NULL, LIST, DataPiece::NullData(), false,
child == NULL ? current_->path() : child->path(),
- preserve_proto_field_names_,
field_scrub_callback_.get()));
child = node.get();
current_->AddChild(node.release());
@@ -584,7 +577,6 @@ void DefaultValueObjectWriter::RenderDataPiece(StringPiece name,
google::protobuf::scoped_ptr<Node> node(
new Node(name.ToString(), NULL, PRIMITIVE, data, false,
child == NULL ? current_->path() : child->path(),
- preserve_proto_field_names_,
field_scrub_callback_.get()));
child = node.get();
current_->AddChild(node.release());
diff --git a/src/google/protobuf/util/internal/default_value_objectwriter.h b/src/google/protobuf/util/internal/default_value_objectwriter.h
index 7a925f7f0..1d85bed85 100644
--- a/src/google/protobuf/util/internal/default_value_objectwriter.h
+++ b/src/google/protobuf/util/internal/default_value_objectwriter.h
@@ -122,11 +122,6 @@ class LIBPROTOBUF_EXPORT DefaultValueObjectWriter : public ObjectWriter {
// field_scrub_callback pointer is also transferred to this class
void RegisterFieldScrubCallBack(FieldScrubCallBackPtr field_scrub_callback);
- // If set to true, original proto field names are used
- void set_preserve_proto_field_names(bool value) {
- preserve_proto_field_names_ = value;
- }
-
private:
enum NodeKind {
PRIMITIVE = 0,
@@ -141,7 +136,6 @@ class LIBPROTOBUF_EXPORT DefaultValueObjectWriter : public ObjectWriter {
public:
Node(const string& name, const google::protobuf::Type* type, NodeKind kind,
const DataPiece& data, bool is_placeholder, const vector<string>& path,
- bool preserve_proto_field_names,
FieldScrubCallBack* field_scrub_callback);
virtual ~Node() {
for (int i = 0; i < children_.size(); ++i) {
@@ -218,9 +212,6 @@ class LIBPROTOBUF_EXPORT DefaultValueObjectWriter : public ObjectWriter {
// Path of the field of this node
std::vector<string> path_;
- // Whether to preserve original proto field names
- bool preserve_proto_field_names_;
-
// Pointer to function for determining whether a field needs to be scrubbed
// or not. This callback is owned by the creator of this node.
FieldScrubCallBack* field_scrub_callback_;
@@ -266,9 +257,6 @@ class LIBPROTOBUF_EXPORT DefaultValueObjectWriter : public ObjectWriter {
// The stack to hold the path of Nodes from current_ to root_;
std::stack<Node*> stack_;
- // Whether to preserve original proto field names
- bool preserve_proto_field_names_;
-
// Unique Pointer to function for determining whether a field needs to be
// scrubbed or not.
FieldScrubCallBackPtr field_scrub_callback_;
diff --git a/src/google/protobuf/util/internal/protostream_objectsource.cc b/src/google/protobuf/util/internal/protostream_objectsource.cc
index a9c586312..1f3781a4b 100644
--- a/src/google/protobuf/util/internal/protostream_objectsource.cc
+++ b/src/google/protobuf/util/internal/protostream_objectsource.cc
@@ -120,7 +120,6 @@ ProtoStreamObjectSource::ProtoStreamObjectSource(
own_typeinfo_(true),
type_(type),
use_lower_camel_for_enums_(false),
- preserve_proto_field_names_(false),
recursion_depth_(0),
max_recursion_depth_(kDefaultMaxRecursionDepth) {
GOOGLE_LOG_IF(DFATAL, stream == NULL) << "Input stream is NULL.";
@@ -134,7 +133,6 @@ ProtoStreamObjectSource::ProtoStreamObjectSource(
own_typeinfo_(false),
type_(type),
use_lower_camel_for_enums_(false),
- preserve_proto_field_names_(false),
recursion_depth_(0),
max_recursion_depth_(kDefaultMaxRecursionDepth) {
GOOGLE_LOG_IF(DFATAL, stream == NULL) << "Input stream is NULL.";
@@ -195,11 +193,7 @@ Status ProtoStreamObjectSource::WriteMessage(const google::protobuf::Type& type,
last_tag = tag;
field = FindAndVerifyField(type, tag);
if (field != NULL) {
- if (preserve_proto_field_names_) {
- field_name = field->name();
- } else {
- field_name = field->json_name();
- }
+ field_name = field->json_name();
}
}
if (field == NULL) {
@@ -634,8 +628,6 @@ Status ProtoStreamObjectSource::RenderAny(const ProtoStreamObjectSource* os,
// using a nested ProtoStreamObjectSource using our nested type information.
ProtoStreamObjectSource nested_os(&in_stream, os->typeinfo_, *nested_type);
- nested_os.set_use_lower_camel_for_enums(os->use_lower_camel_for_enums_);
- nested_os.set_preserve_proto_field_names(os->preserve_proto_field_names_);
// We manually call start and end object here so we can inject the @type.
ow->StartObject(field_name);
ow->RenderString("@type", type_url);
diff --git a/src/google/protobuf/util/internal/protostream_objectsource.h b/src/google/protobuf/util/internal/protostream_objectsource.h
index a6cee4e15..d7d4347b3 100644
--- a/src/google/protobuf/util/internal/protostream_objectsource.h
+++ b/src/google/protobuf/util/internal/protostream_objectsource.h
@@ -110,11 +110,6 @@ class LIBPROTOBUF_EXPORT ProtoStreamObjectSource : public ObjectSource {
use_lower_camel_for_enums_ = value;
}
- // Sets whether to use original proto field names
- void set_preserve_proto_field_names(bool value) {
- preserve_proto_field_names_ = value;
- }
-
// Sets the max recursion depth of proto message to be deserialized. Proto
// messages over this depth will fail to be deserialized.
// Default value is 64.
@@ -286,9 +281,6 @@ class LIBPROTOBUF_EXPORT ProtoStreamObjectSource : public ObjectSource {
// Whether to render enums using lowerCamelCase. Defaults to false.
bool use_lower_camel_for_enums_;
- // Whether to preserve proto field names
- bool preserve_proto_field_names_;
-
// Tracks current recursion depth.
mutable int recursion_depth_;
diff --git a/src/google/protobuf/util/internal/protostream_objectsource_test.cc b/src/google/protobuf/util/internal/protostream_objectsource_test.cc
index 4099b8f03..3f6fdf973 100644
--- a/src/google/protobuf/util/internal/protostream_objectsource_test.cc
+++ b/src/google/protobuf/util/internal/protostream_objectsource_test.cc
@@ -100,8 +100,7 @@ class ProtostreamObjectSourceTest
: helper_(GetParam()),
mock_(),
ow_(&mock_),
- use_lower_camel_for_enums_(false),
- use_preserve_proto_field_names_(false) {
+ use_lower_camel_for_enums_(false) {
helper_.ResetTypeInfo(Book::descriptor());
}
@@ -122,7 +121,6 @@ class ProtostreamObjectSourceTest
google::protobuf::scoped_ptr<ProtoStreamObjectSource> os(
helper_.NewProtoSource(&in_stream, GetTypeUrl(descriptor)));
if (use_lower_camel_for_enums_) os->set_use_lower_camel_for_enums(true);
- if (use_preserve_proto_field_names_) os->set_preserve_proto_field_names(true);
os->set_max_recursion_depth(64);
return os->WriteTo(&mock_);
}
@@ -270,14 +268,11 @@ class ProtostreamObjectSourceTest
void UseLowerCamelForEnums() { use_lower_camel_for_enums_ = true; }
- void UsePreserveProtoFieldNames() { use_preserve_proto_field_names_ = true; }
-
testing::TypeInfoTestHelper helper_;
::testing::NiceMock<MockObjectWriter> mock_;
ExpectingObjectWriter ow_;
bool use_lower_camel_for_enums_;
- bool use_preserve_proto_field_names_;
};
INSTANTIATE_TEST_CASE_P(DifferentTypeInfoSourceTest,
@@ -498,16 +493,6 @@ TEST_P(ProtostreamObjectSourceTest, EnumCaseIsUnchangedByDefault) {
DoTest(book, Book::descriptor());
}
-TEST_P(ProtostreamObjectSourceTest, UsePreserveProtoFieldNames) {
- Book book;
- book.set_snake_field("foo");
-
- UsePreserveProtoFieldNames();
-
- ow_.StartObject("")->RenderString("snake_field", "foo")->EndObject();
- DoTest(book, Book::descriptor());
-}
-
TEST_P(ProtostreamObjectSourceTest, CyclicMessageDepthTest) {
Cyclic cyclic;
cyclic.set_m_int(123);
@@ -717,48 +702,6 @@ TEST_P(ProtostreamObjectSourceAnysTest, BasicAny) {
DoTest(out, AnyOut::descriptor());
}
-TEST_P(ProtostreamObjectSourceAnysTest, LowerCamelEnumOutputSnakeCase) {
- AnyOut out;
- ::google::protobuf::Any* any = out.mutable_any();
-
- Book book;
- book.set_type(Book::arts_and_photography);
- any->PackFrom(book);
-
- UseLowerCamelForEnums();
-
- ow_.StartObject("")
- ->StartObject("any")
- ->RenderString("@type",
- "type.googleapis.com/proto_util_converter.testing.Book")
- ->RenderString("type", "artsAndPhotography")
- ->EndObject()
- ->EndObject();
-
- DoTest(out, AnyOut::descriptor());
-}
-
-TEST_P(ProtostreamObjectSourceAnysTest, UsePreserveProtoFieldNames) {
- AnyOut out;
- ::google::protobuf::Any* any = out.mutable_any();
-
- Book book;
- book.set_snake_field("foo");
- any->PackFrom(book);
-
- UsePreserveProtoFieldNames();
-
- ow_.StartObject("")
- ->StartObject("any")
- ->RenderString("@type",
- "type.googleapis.com/proto_util_converter.testing.Book")
- ->RenderString("snake_field", "foo")
- ->EndObject()
- ->EndObject();
-
- DoTest(out, AnyOut::descriptor());
-}
-
TEST_P(ProtostreamObjectSourceAnysTest, RecursiveAny) {
AnyOut out;
::google::protobuf::Any* any = out.mutable_any();
diff --git a/src/google/protobuf/util/internal/testdata/books.proto b/src/google/protobuf/util/internal/testdata/books.proto
index 95af37c98..1cbbba478 100644
--- a/src/google/protobuf/util/internal/testdata/books.proto
+++ b/src/google/protobuf/util/internal/testdata/books.proto
@@ -63,9 +63,6 @@ message Book {
}
optional Type type = 11;
- // Useful for testing JSON snake/camel-case conversions.
- optional string snake_field = 12;
-
extensions 200 to 499;
}
diff --git a/src/google/protobuf/util/json_util.cc b/src/google/protobuf/util/json_util.cc
index d72575766..2659320a1 100644
--- a/src/google/protobuf/util/json_util.cc
+++ b/src/google/protobuf/util/json_util.cc
@@ -79,16 +79,12 @@ util::Status BinaryToJsonStream(TypeResolver* resolver,
google::protobuf::Type type;
RETURN_IF_ERROR(resolver->ResolveMessageType(type_url, &type));
converter::ProtoStreamObjectSource proto_source(&in_stream, resolver, type);
- proto_source.set_preserve_proto_field_names(
- options.preserve_proto_field_names);
io::CodedOutputStream out_stream(json_output);
converter::JsonObjectWriter json_writer(options.add_whitespace ? " " : "",
&out_stream);
if (options.always_print_primitive_fields) {
converter::DefaultValueObjectWriter default_value_writer(
resolver, type, &json_writer);
- default_value_writer.set_preserve_proto_field_names(
- options.preserve_proto_field_names);
return proto_source.WriteTo(&default_value_writer);
} else {
return proto_source.WriteTo(&json_writer);
diff --git a/src/google/protobuf/util/json_util.h b/src/google/protobuf/util/json_util.h
index 83547c706..1718bfb52 100644
--- a/src/google/protobuf/util/json_util.h
+++ b/src/google/protobuf/util/json_util.h
@@ -53,12 +53,9 @@ struct JsonOptions {
// set to 0 will be omitted. Set this flag to true will override the default
// behavior and print primitive fields regardless of their values.
bool always_print_primitive_fields;
- // Whether to preserve proto field names
- bool preserve_proto_field_names;
JsonOptions() : add_whitespace(false),
- always_print_primitive_fields(false),
- preserve_proto_field_names(false) {
+ always_print_primitive_fields(false) {
}
};
diff --git a/src/google/protobuf/util/json_util_test.cc b/src/google/protobuf/util/json_util_test.cc
index e23bff46b..a4d3cc983 100644
--- a/src/google/protobuf/util/json_util_test.cc
+++ b/src/google/protobuf/util/json_util_test.cc
@@ -131,42 +131,6 @@ TEST_F(JsonUtilTest, TestDefaultValues) {
"\"repeatedMessageValue\":[]"
"}",
ToJson(m, options));
- options.preserve_proto_field_names = true;
- m.set_string_value("i am a test string value");
- m.set_bytes_value("i am a test bytes value");
- EXPECT_EQ(
- "{\"bool_value\":false,"
- "\"int32_value\":0,"
- "\"int64_value\":\"0\","
- "\"uint32_value\":0,"
- "\"uint64_value\":\"0\","
- "\"float_value\":0,"
- "\"double_value\":0,"
- "\"string_value\":\"i am a test string value\","
- "\"bytes_value\":\"aSBhbSBhIHRlc3QgYnl0ZXMgdmFsdWU=\","
- "\"enum_value\":\"FOO\","
- "\"repeated_bool_value\":[],"
- "\"repeated_int32_value\":[],"
- "\"repeated_int64_value\":[],"
- "\"repeated_uint32_value\":[],"
- "\"repeated_uint64_value\":[],"
- "\"repeated_float_value\":[],"
- "\"repeated_double_value\":[],"
- "\"repeated_string_value\":[],"
- "\"repeated_bytes_value\":[],"
- "\"repeated_enum_value\":[],"
- "\"repeated_message_value\":[]"
- "}",
- ToJson(m, options));
-}
-
-TEST_F(JsonUtilTest, TestPreserveProtoFieldNames) {
- TestMessage m;
- m.mutable_message_value();
-
- JsonPrintOptions options;
- options.preserve_proto_field_names = true;
- EXPECT_EQ("{\"message_value\":{}}", ToJson(m, options));
}
TEST_F(JsonUtilTest, ParseMessage) {