Skip to content

Commit

Permalink
Copybara import of the project:
Browse files Browse the repository at this point in the history
--
3eac250 by Josh Humphries <jhumphries@buf.build>:

add check for custom JSON name conflicts
- also, include check for default JSON name conflicts even in proto2
  files (but only warn)
- if custom JSON name conflicts with other default name, only a
  warning in proto2

--
b23b387 by Josh Humphries <jhumphries@buf.build>:

update existing test expectations and add new tests

--
aa34e0e by Josh Humphries <jhumphries@buf.build>:

JSON -> Json

--
fdaa464 by Josh Humphries <jhumphries@buf.build>:

address review feedback wrt absl string functions
also moves helpers into anonymous namespace

--
6d5f2fc by Josh Humphries <jhumphries@buf.build>:

apply clang-format changes; change really long pair type to auto

--
81b5cbe by Josh Humphries <jhumphries@buf.build>:

address review feedback

--
8fa8b10 by Josh Humphries <jhumphries@buf.build>:

return struct instead of using out param

--
b405717 by Josh Humphries <jhumphries@buf.build>:

address review feedback

COPYBARA_INTEGRATE_REVIEW=#10750 from jhump:jh/custom-json-name-validation b405717
PiperOrigin-RevId: 496041006
  • Loading branch information
jhump authored and copybara-github committed Dec 17, 2022
1 parent 10f869a commit 535069e
Show file tree
Hide file tree
Showing 3 changed files with 388 additions and 77 deletions.
119 changes: 116 additions & 3 deletions src/google/protobuf/compiler/parser_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2073,11 +2073,124 @@ TEST_F(ParserValidationErrorTest, Proto3JsonConflictError) {
ExpectHasValidationErrors(
"syntax = 'proto3';\n"
"message TestMessage {\n"
" uint32 foo = 1;\n"
" uint32 _foo = 1;\n"
" uint32 Foo = 2;\n"
"}\n",
"3:9: The JSON camel-case name of field \"Foo\" conflicts with field "
"\"foo\". This is not allowed in proto3.\n");
"3:9: The default JSON name of field \"Foo\" (\"Foo\") conflicts "
"with the default JSON name of field \"_foo\".\n");
}

TEST_F(ParserValidationErrorTest, Proto2JsonConflictError) {
ExpectParsesTo(
"syntax = 'proto2';\n"
"message TestMessage {\n"
" optional uint32 _foo = 1;\n"
" optional uint32 Foo = 2;\n"
"}\n",
"syntax: 'proto2'\n"
"message_type {\n"
" name: 'TestMessage'\n"
" field {\n"
" label: LABEL_OPTIONAL type: TYPE_UINT32 name: '_foo' number: 1\n"
" }\n"
" field {\n"
" label: LABEL_OPTIONAL type: TYPE_UINT32 name: 'Foo' number: 2\n"
" }\n"
"}\n");
}

TEST_F(ParserValidationErrorTest, Proto3CustomJsonConflictWithDefaultError) {
ExpectHasValidationErrors(
"syntax = 'proto3';\n"
"message TestMessage {\n"
" uint32 foo = 1 [json_name='bar'];\n"
" uint32 bar = 2;\n"
"}\n",
"3:9: The default JSON name of field \"bar\" (\"bar\") conflicts "
"with the custom JSON name of field \"foo\".\n");
}

TEST_F(ParserValidationErrorTest, Proto2CustomJsonConflictWithDefaultError) {
ExpectParsesTo(
"syntax = 'proto2';\n"
"message TestMessage {\n"
" optional uint32 foo = 1 [json_name='bar'];\n"
" optional uint32 bar = 2;\n"
"}\n",
"syntax: 'proto2'\n"
"message_type {\n"
" name: 'TestMessage'\n"
" field {\n"
" label: LABEL_OPTIONAL type: TYPE_UINT32 name: 'foo' number: 1 "
"json_name: 'bar'\n"
" }\n"
" field {\n"
" label: LABEL_OPTIONAL type: TYPE_UINT32 name: 'bar' number: 2\n"
" }\n"
"}\n");
}

TEST_F(ParserValidationErrorTest, Proto3CustomJsonConflictError) {
ExpectHasValidationErrors(
"syntax = 'proto3';\n"
"message TestMessage {\n"
" uint32 foo = 1 [json_name='baz'];\n"
" uint32 bar = 2 [json_name='baz'];\n"
"}\n",
"3:9: The custom JSON name of field \"bar\" (\"baz\") conflicts "
"with the custom JSON name of field \"foo\".\n");
}

TEST_F(ParserValidationErrorTest, Proto2CustomJsonConflictError) {
ExpectHasValidationErrors(
"syntax = 'proto2';\n"
"message TestMessage {\n"
" optional uint32 foo = 1 [json_name='baz'];\n"
" optional uint32 bar = 2 [json_name='baz'];\n"
"}\n",
"3:18: The custom JSON name of field \"bar\" (\"baz\") conflicts "
"with the custom JSON name of field \"foo\".\n");
}

TEST_F(ParserValidationErrorTest, Proto3JsonConflictLegacy) {
ExpectHasValidationErrors(
"syntax = 'proto3';\n"
"message TestMessage {\n"
" option deprecated_legacy_json_field_conflicts = true;\n"
" uint32 fooBar = 1;\n"
" uint32 foo_bar = 2;\n"
"}\n",
"4:9: The default JSON name of field \"foo_bar\" (\"fooBar\") conflicts "
"with the default JSON name of field \"fooBar\".\n");
}

TEST_F(ParserValidationErrorTest, Proto2JsonConflictLegacy) {
ExpectParsesTo(
"syntax = 'proto2';\n"
"message TestMessage {\n"
" option deprecated_legacy_json_field_conflicts = true;\n"
" optional uint32 fooBar = 1;\n"
" optional uint32 foo_bar = 2;\n"
"}\n",
"syntax: 'proto2'\n"
"message_type {\n"
" name: 'TestMessage'\n"
" field {\n"
" label: LABEL_OPTIONAL type: TYPE_UINT32 name: 'fooBar' number: 1\n"
" }\n"
" field {\n"
" label: LABEL_OPTIONAL type: TYPE_UINT32 name: 'foo_bar' number: 2\n"
" }\n"
" options {\n"
" uninterpreted_option {\n"
" name {\n"
" name_part: 'deprecated_legacy_json_field_conflicts'\n"
" is_extension: false\n"
" }\n"
" identifier_value: 'true'\n"
" }\n"
" }\n"
"}\n");
}

TEST_F(ParserValidationErrorTest, EnumNameError) {
Expand Down
Loading

0 comments on commit 535069e

Please sign in to comment.