Skip to content

fix: Don't let a record field alias shadow the name of another field - #752

Open
janwaleed09 wants to merge 1 commit into
apache:mainfrom
janwaleed09:field_alias_shadowing
Open

janwaleed09 wants to merge 1 commit into
apache:mainfrom
janwaleed09:field_alias_shadowing

Conversation

@janwaleed09

Copy link
Copy Markdown
Contributor

When a field alias equals the name of another field, the parser overwrites that field's entry in RecordSchema::lookup:

{"type": "record", "name": "R", "fields": [
  {"name": "x", "type": "int"},
  {"name": "y", "type": "long", "aliases": ["x"]}
]}

Here lookup is {"x": 1, "y": 1}, so serializing a struct with the fields x and y fails, because x is written with the schema of y. Value::validate and Record::put("x") also end up at y. With the two fields the other way around, the schema doesn't parse at all ("Duplicate field name x"). This is the same on 0.22.0 and 0.23.0.

This PR inserts all names first and adds an alias only if it isn't already the name of a field, so a name always maps to its own field. I also wrote that down in the doc comment of lookup. That's the order encode.rs already uses (name first, then aliases). Aliases that two fields share still point to the last of them, as before.

Strictly speaking, such a schema isn't valid: since AVRO-3833 the spec says aliases are "subject to the same uniqueness constraints as names". The implementations don't agree, though. Java, Python, fastavro and the Go libraries accept both orders and write by field name, while C# and C++ reject both. I went with accepting them, so nothing that parses today stops parsing. If you'd rather reject these schemas like C# and C++, that's a small change and I can switch the PR to it.

RecordSchema::builder(), and as far as I can see the new parser in #672, don't put aliases into lookup at all. Should they be there?

I ran into this while working on a faster record serializer, which relies on every field name mapping to its own field. I'll open that PR right after this one; it builds on this fix.

@Kriskras99

Copy link
Copy Markdown
Contributor

I think I'd prefer if we return an error on duplicate names/aliases. RecordSchema::builder() and the new parser should insert into lookup.

@martin-g what do you think, error or silently accept?

@martin-g

Copy link
Copy Markdown
Member

@martin-g what do you think, error or silently accept?

Error.

Unless the Java SDK does something else. People often come with it is against the spec / common sense but the Java SDK already does it. In this case we should be compatible and a user provided policy would be the best.

@martin-g martin-g added this to the 0.24.0 milestone Nov 30, 2026
@janwaleed09 janwaleed09 closed this Dec 1, 2026
@janwaleed09 janwaleed09 reopened this Dec 1, 2026
@janwaleed09

Copy link
Copy Markdown
Contributor Author

@martin-g what do you think, error or silently accept?

Error.

Unless the Java SDK does something else. People often come with it is against the spec / common sense but the Java SDK already does it. In this case we should be compatible and a user provided policy would be the best.

hmm, Thanks. I will continue working on this tomorrow 👍

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants