Skip to content

Commit 961b35a

Browse files
committedOct 27, 2020
duplicated field names in records are invalid
1 parent 705b8d8 commit 961b35a

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed
 

‎src/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ pub enum AvrowErr {
113113
ParsingCanonicalForm,
114114
#[error("Duplicate definition of named schema")]
115115
DuplicateSchema,
116+
#[error("Duplicate field name in record schema")]
117+
DuplicateField,
116118
#[error("Invalid default value for union. Must be the first entry from union definition")]
117119
FailedDefaultUnion,
118120
#[error("Invalid default value for given schema")]

‎src/schema/parser.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ impl Registry {
152152

153153
let aliases = parse_aliases(o.get("aliases"));
154154

155+
if fields_parsed.contains_key(name) {
156+
return Err(AvrowErr::DuplicateField);
157+
}
158+
155159
fields_parsed.insert(
156160
name.to_string(),
157161
Field::new(name, ty, default, order, aliases)?,

‎src/schema/tests.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,3 +435,21 @@ fn test_two_instance_schema_equality() {
435435
let schema2 = Schema::from_str(raw_schema).unwrap();
436436
assert_eq!(schema, schema2);
437437
}
438+
439+
#[test]
440+
#[should_panic(expected = "DuplicateField")]
441+
fn duplicate_field_name_in_record_fails() {
442+
let raw_schema = r#"
443+
{
444+
"type": "record",
445+
"name": "Person",
446+
"doc": "Hi there.",
447+
"fields": [
448+
{"name": "id", "type": "string", "default": "dsf8e8"},
449+
{"name": "id", "type": "int", "default": 56}
450+
]
451+
}
452+
"#;
453+
454+
Schema::from_str(raw_schema).unwrap();
455+
}

0 commit comments

Comments
 (0)
Please sign in to comment.