Skip to content

Commit e25e267

Browse files
Fix a bug in the main C++ JSON parser/serializer camelcasing of certain non-style-compliant names incorrectly, in a way that would prevent it from interoperating with any other implementation on those fields.
Before this fix, the camelcasing that it would use under certain non-style compliant names like `_field` are mutually incompatible with any other implementation. This case was already covered by the conformance test suite but was not hooked up to this implementation and instead used a lesser-used path in the same JSON package which is based on type.proto instead of Descriptor. A subsequent CL will switch the cc conformance testee to use this implementation as it is the primary one that our customers. PiperOrigin-RevId: 798047846
1 parent 8dbd4ef commit e25e267

File tree

3 files changed

+12
-10
lines changed

3 files changed

+12
-10
lines changed

src/google/protobuf/json/internal/descriptor_traits.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ struct Proto2Descriptor {
178178

179179
for (int i = 0; i < d.field_count(); ++i) {
180180
const auto* field = d.field(i);
181-
if (field->has_json_name() && field->json_name() == name) {
181+
if (field->json_name() == name) {
182182
return field;
183183
}
184184
}
@@ -206,9 +206,7 @@ struct Proto2Descriptor {
206206
/// Functions for introspecting fields. ///
207207

208208
static absl::string_view FieldName(Field f) { return f->name(); }
209-
static absl::string_view FieldJsonName(Field f) {
210-
return f->has_json_name() ? f->json_name() : f->camelcase_name();
211-
}
209+
static absl::string_view FieldJsonName(Field f) { return f->json_name(); }
212210
static absl::string_view FieldFullName(Field f) { return f->full_name(); }
213211

214212
static absl::string_view FieldTypeName(Field f) {

src/google/protobuf/json/json_test.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,9 @@
77

88
#include "google/protobuf/json/json.h"
99

10-
#include <algorithm>
1110
#include <cstdint>
12-
#include <list>
1311
#include <memory>
1412
#include <string>
15-
#include <vector>
1613

1714
#include "google/protobuf/duration.pb.h"
1815
#include "google/protobuf/field_mask.pb.h"
@@ -23,11 +20,11 @@
2320
#include <gtest/gtest.h>
2421
#include "absl/status/status.h"
2522
#include "absl/status/statusor.h"
23+
#include "absl/strings/str_cat.h"
2624
#include "absl/strings/string_view.h"
2725
#include "google/protobuf/descriptor_database.h"
2826
#include "google/protobuf/dynamic_message.h"
2927
#include "google/protobuf/io/test_zero_copy_stream.h"
30-
#include "google/protobuf/io/zero_copy_stream.h"
3128
#include "google/protobuf/io/zero_copy_stream_impl_lite.h"
3229
#include "google/protobuf/util/json_format.pb.h"
3330
#include "google/protobuf/util/json_format_proto3.pb.h"
@@ -68,6 +65,7 @@ MATCHER_P(IsOkAndHolds, inner,
6865
}
6966

7067
absl::Status GetStatus(const absl::Status& s) { return s; }
68+
7169
template <typename T>
7270
absl::Status GetStatus(const absl::StatusOr<T>& s) {
7371
return s.status();
@@ -200,7 +198,8 @@ TEST_P(JsonTest, TestAlwaysPrintFieldsWithNoPresence) {
200198
R"("repeatedStringValue":[],)"
201199
R"("repeatedBytesValue":[],)"
202200
R"("repeatedEnumValue":[],)"
203-
R"("repeatedMessageValue":[])"
201+
R"("repeatedMessageValue":[],)"
202+
R"("NonStandardName":0)"
204203
"}"));
205204

206205
m.set_string_value("i am a test string value");
@@ -232,7 +231,8 @@ TEST_P(JsonTest, TestAlwaysPrintFieldsWithNoPresence) {
232231
R"("repeatedMessageValue":[],)"
233232
R"("optionalBoolValue":false,)"
234233
R"("optionalStringValue":"",)"
235-
R"("optionalBytesValue":"")"
234+
R"("optionalBytesValue":"",)"
235+
R"("NonStandardName":0)"
236236
"}"));
237237

238238
EXPECT_THAT(

src/google/protobuf/util/json_format_proto3.proto

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
// license that can be found in the LICENSE file or at
66
// https://developers.google.com/open-source/licenses/bsd
77

8+
// LINT: LEGACY_NAMES
9+
810
syntax = "proto3";
911

1012
package proto3;
@@ -66,6 +68,8 @@ message TestMessage {
6668
optional bytes optional_bytes_value = 49;
6769
optional EnumType optional_enum_value = 50;
6870
optional MessageType optional_message_value = 51;
71+
72+
int32 _nonStandard_name_ = 52;
6973
}
7074

7175
message TestOneof {

0 commit comments

Comments
 (0)