This is an automated email from the ASF dual-hosted git repository.
mgrigorov pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/avro.git
The following commit(s) were added to refs/heads/main by this push:
new 5e3be8074 AVRO-3962: [Rust] Add support for field attribute Rustdoc to
AvroSchema (#2813)
5e3be8074 is described below
commit 5e3be80745678bb58b28d7ddd7684f659347ae2c
Author: Martin Grigorov <[email protected]>
AuthorDate: Tue Mar 19 15:37:32 2024 +0200
AVRO-3962: [Rust] Add support for field attribute Rustdoc to AvroSchema
(#2813)
Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
---
lang/rust/avro_derive/src/lib.rs | 3 ++-
lang/rust/avro_derive/tests/derive.rs | 24 ++++++++++++++++++++++++
2 files changed, 26 insertions(+), 1 deletion(-)
diff --git a/lang/rust/avro_derive/src/lib.rs b/lang/rust/avro_derive/src/lib.rs
index 419f34cab..8f0138823 100644
--- a/lang/rust/avro_derive/src/lib.rs
+++ b/lang/rust/avro_derive/src/lib.rs
@@ -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
}
diff --git a/lang/rust/avro_derive/tests/derive.rs
b/lang/rust/avro_derive/tests/derive.rs
index 1fab03203..ec7a96d75 100644
--- a/lang/rust/avro_derive/tests/derive.rs
+++ b/lang/rust/avro_derive/tests/derive.rs
@@ -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")
+ }
+ }
}