Skip to content

Commit

Permalink
AVRO-3962: [Rust] Add support for field attribute Rustdoc to AvroSche…
Browse files Browse the repository at this point in the history
…ma (#2813)

Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
  • Loading branch information
martin-g authored Mar 19, 2024
1 parent dad0d20 commit 5e3be80
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lang/rust/avro_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ fn get_data_struct_schema_def(
}
let field_attrs =
FieldOptions::from_attributes(&field.attrs[..]).map_err(darling_to_syn)?;
let doc = preserve_optional(field_attrs.doc);
let doc =
preserve_optional(field_attrs.doc.or_else(|| extract_outer_doc(&field.attrs)));
if let Some(rename) = field_attrs.rename {
name = rename
}
Expand Down
24 changes: 24 additions & 0 deletions lang/rust/avro_derive/tests/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1571,4 +1571,28 @@ mod test_derive {
panic!("Unexpected schema type for {derived_schema:?}")
}
}

#[test]
fn avro_3962_fields_documentation() {
/// Foo docs
#[derive(AvroSchema)]
#[allow(dead_code)]
struct Foo {
/// a's Rustdoc
a: i32,
/// b's Rustdoc
#[avro(doc = "attribute doc has priority over Rustdoc")]
b: i32,
}

if let Schema::Record(RecordSchema { fields, .. }) = Foo::get_schema() {
assert_eq!(fields[0].doc, Some("a's Rustdoc".to_string()));
assert_eq!(
fields[1].doc,
Some("attribute doc has priority over Rustdoc".to_string())
);
} else {
panic!("Unexpected schema type for Foo")
}
}
}

0 comments on commit 5e3be80

Please sign in to comment.