Skip to content

AVRO-4314: [csharp] Validate names against the Avro name grammar#3895

Merged
RyanSkraba merged 3 commits into
apache:mainfrom
iemejia:AVRO-4314-csharp-name-validation
Jul 22, 2026
Merged

AVRO-4314: [csharp] Validate names against the Avro name grammar#3895
RyanSkraba merged 3 commits into
apache:mainfrom
iemejia:AVRO-4314-csharp-name-validation

Conversation

@iemejia

@iemejia iemejia commented Jul 22, 2026

Copy link
Copy Markdown
Member

What is the purpose of the change

Record/fixed/enum names, record field names and protocol message names were
accepted verbatim when parsing, unlike enum symbols which were already
validated. Because the code generator splices some of these values directly
into generated C# source (for example protocol message names into a case
label and field names into Get/Put switch bodies), an out-of-spec name
produced malformed or unexpected generated code.

This change adds a shared, Unicode-aware name validator (first character a
letter or _, remaining characters letters, digits or _) and applies it to
schema names, field names and message names during parsing.

Notes:

  • The rule is Unicode-aware to preserve the existing support for non-ASCII
    names (e.g. the TestRecordFieldNames case).
  • Namespaces are intentionally not validated, because the code generator's
    namespace-mapping feature rewrites them to C#-specific values (such as
    @return for reserved words) and re-parses the schema.

Verifying this change

This change added tests and can be verified as follows:

  • Added negative cases to Schema/SchemaTests.cs (invalid record name, invalid
    field name, and a field name attempting identifier injection) and
    Protocol/ProtocolTest.cs (TestInvalidMessageName).
  • Verified locally: the full Avro.test suite (1526 tests) passes, including
    the AvroGen code-generation tests.

Documentation

  • Does this pull request introduce a new feature? no
  • If yes, how is the feature documented? not applicable

iemejia added 3 commits July 22, 2026 07:37
Record/fixed/enum names, record field names and protocol message names
were accepted verbatim when parsing, unlike enum symbols which were
already validated. Because the code generator splices some of these
values directly into generated C# source (for example protocol message
names into a case label and field names into Get/Put switch bodies), an
out-of-spec name produced malformed or unexpected generated code.

Add a shared, Unicode-aware name validator (first character a letter or
'_', remaining characters letters, digits or '_') and apply it to
schema names, field names and message names during parsing. The rule is
Unicode-aware to preserve the existing support for non-ASCII names, and
namespaces are intentionally not validated because the code generator's
namespace-mapping feature rewrites them to C#-specific values (such as
"@return") and re-parses the schema. Add negative tests for invalid
field, record and message names.
Address review feedback:
- Validate record field aliases with the same rule as field names, since
  aliases participate in schema resolution and are names per the Avro
  grammar. Previously they were accepted verbatim.
- Escape control characters (and quote the value) when embedding an
  invalid name in the SchemaParseException message, so a crafted name
  containing newlines or other control characters cannot produce
  multi-line or ambiguous error output.

Add a negative test for an invalid field alias.
Address review feedback: ValidateName inspected individual UTF-16 code
units, which rejected valid supplementary-plane letters and digits that
are encoded as surrogate pairs. Iterate by Unicode scalar value using
the char.IsLetter(string, int) / char.IsLetterOrDigit(string, int)
overloads and advance past surrogate pairs. Add a test that a name
containing a supplementary-plane letter (U+20000) is accepted.
@RyanSkraba
RyanSkraba merged commit 787adb1 into apache:main Jul 22, 2026
15 checks passed
RyanSkraba pushed a commit that referenced this pull request Jul 22, 2026
* AVRO-4314: [csharp] Validate names against the Avro name grammar

Record/fixed/enum names, record field names and protocol message names
were accepted verbatim when parsing, unlike enum symbols which were
already validated. Because the code generator splices some of these
values directly into generated C# source (for example protocol message
names into a case label and field names into Get/Put switch bodies), an
out-of-spec name produced malformed or unexpected generated code.

Add a shared, Unicode-aware name validator (first character a letter or
'_', remaining characters letters, digits or '_') and apply it to
schema names, field names and message names during parsing. The rule is
Unicode-aware to preserve the existing support for non-ASCII names, and
namespaces are intentionally not validated because the code generator's
namespace-mapping feature rewrites them to C#-specific values (such as
"@return") and re-parses the schema. Add negative tests for invalid
field, record and message names.

* AVRO-4314: [csharp] Validate field aliases and sanitize name errors

Address review feedback:
- Validate record field aliases with the same rule as field names, since
  aliases participate in schema resolution and are names per the Avro
  grammar. Previously they were accepted verbatim.
- Escape control characters (and quote the value) when embedding an
  invalid name in the SchemaParseException message, so a crafted name
  containing newlines or other control characters cannot produce
  multi-line or ambiguous error output.

Add a negative test for an invalid field alias.

* AVRO-4314: [csharp] Handle supplementary-plane characters in names

Address review feedback: ValidateName inspected individual UTF-16 code
units, which rejected valid supplementary-plane letters and digits that
are encoded as surrogate pairs. Iterate by Unicode scalar value using
the char.IsLetter(string, int) / char.IsLetterOrDigit(string, int)
overloads and advance past surrogate pairs. Add a test that a name
containing a supplementary-plane letter (U+20000) is accepted.
@RyanSkraba

RyanSkraba commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Cherry-picked to branch-1.12.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants