@@ -9,7 +9,7 @@ mod extra_fields;
99mod section;
1010pub ( crate ) mod utils;
1111
12- use algorithm:: Algorithm ;
12+ pub use algorithm:: Algorithm ;
1313use catalyst_types:: {
1414 problem_report:: ProblemReport ,
1515 uuid:: { UuidV4 , UuidV7 } ,
@@ -20,7 +20,7 @@ use coset::iana::CoapContentFormat;
2020pub use document_ref:: DocumentRef ;
2121pub use extra_fields:: ExtraFields ;
2222pub use section:: Section ;
23- use utils:: { cose_protected_header_find, decode_cbor_uuid, encode_cbor_uuid} ;
23+ use utils:: { cose_protected_header_find, decode_cbor_uuid, encode_cbor_uuid, validate_option } ;
2424
2525/// `content_encoding` field COSE key value
2626const CONTENT_ENCODING_KEY : & str = "Content-Encoding" ;
@@ -37,22 +37,22 @@ const VER_KEY: &str = "ver";
3737#[ derive( Clone , Debug , PartialEq , serde:: Deserialize , Default ) ]
3838pub struct Metadata {
3939 /// Cryptographic Algorithm
40- #[ serde( skip_serializing_if = "Option::is_none " ) ]
40+ #[ serde( deserialize_with = "validate_option " ) ]
4141 alg : Option < Algorithm > ,
4242 /// Document Type `UUIDv4`.
43- #[ serde( rename = "type" , skip_serializing_if = "Option::is_none " ) ]
43+ #[ serde( rename = "type" , deserialize_with = "validate_option " ) ]
4444 doc_type : Option < UuidV4 > ,
4545 /// Document ID `UUIDv7`.
46- #[ serde( skip_serializing_if = "Option::is_none " ) ]
46+ #[ serde( deserialize_with = "validate_option " ) ]
4747 id : Option < UuidV7 > ,
4848 /// Document Version `UUIDv7`.
49- #[ serde( skip_serializing_if = "Option::is_none " ) ]
49+ #[ serde( deserialize_with = "validate_option " ) ]
5050 ver : Option < UuidV7 > ,
5151 /// Document Payload Content Type.
52- #[ serde( rename = "content-type" ) ]
52+ #[ serde( rename = "content-type" , deserialize_with = "validate_option" ) ]
5353 content_type : Option < ContentType > ,
5454 /// Document Payload Content Encoding.
55- #[ serde( rename = "content-encoding" , skip_serializing_if = "Option::is_none" ) ]
55+ #[ serde( rename = "content-encoding" ) ]
5656 content_encoding : Option < ContentEncoding > ,
5757 /// Additional Metadata Fields.
5858 #[ serde( flatten) ]
@@ -300,3 +300,69 @@ impl TryFrom<&Metadata> for coset::Header {
300300 Ok ( builder. build ( ) )
301301 }
302302}
303+
304+ #[ cfg( test) ]
305+ mod tests {
306+ use super :: * ;
307+
308+ #[ test]
309+ fn metadata_serde_test ( ) {
310+ let alg = Algorithm :: EdDSA ;
311+ let uuid_v7 = UuidV7 :: new ( ) ;
312+ let uuid_v4 = UuidV4 :: new ( ) ;
313+ let content_type = ContentType :: Json ;
314+
315+ let valid = serde_json:: json!( {
316+ "alg" : alg. to_string( ) ,
317+ "content-type" : content_type. to_string( ) ,
318+ "type" : uuid_v4. to_string( ) ,
319+ "id" : uuid_v7. to_string( ) ,
320+ "ver" : uuid_v7. to_string( ) ,
321+
322+ } ) ;
323+ assert ! ( serde_json:: from_value:: <Metadata >( valid) . is_ok( ) ) ;
324+
325+ let missing_alg = serde_json:: json!( {
326+ "content-type" : content_type. to_string( ) ,
327+ "type" : uuid_v4. to_string( ) ,
328+ "id" : uuid_v7. to_string( ) ,
329+ "ver" : uuid_v7. to_string( ) ,
330+
331+ } ) ;
332+ assert ! ( serde_json:: from_value:: <Metadata >( missing_alg) . is_err( ) ) ;
333+
334+ let missing_content_type = serde_json:: json!( {
335+ "alg" : alg. to_string( ) ,
336+ "type" : uuid_v4. to_string( ) ,
337+ "id" : uuid_v7. to_string( ) ,
338+ "ver" : uuid_v7. to_string( ) ,
339+ } ) ;
340+ assert ! ( serde_json:: from_value:: <Metadata >( missing_content_type) . is_err( ) ) ;
341+
342+ let missing_type = serde_json:: json!( {
343+ "alg" : alg. to_string( ) ,
344+ "content-type" : content_type. to_string( ) ,
345+ "id" : uuid_v7. to_string( ) ,
346+ "ver" : uuid_v7. to_string( ) ,
347+
348+ } ) ;
349+ assert ! ( serde_json:: from_value:: <Metadata >( missing_type) . is_err( ) ) ;
350+
351+ let missing_id = serde_json:: json!( {
352+ "alg" : alg. to_string( ) ,
353+ "content-type" : content_type. to_string( ) ,
354+ "type" : uuid_v4. to_string( ) ,
355+ "ver" : uuid_v7. to_string( ) ,
356+
357+ } ) ;
358+ assert ! ( serde_json:: from_value:: <Metadata >( missing_id) . is_err( ) ) ;
359+
360+ let missing_ver = serde_json:: json!( {
361+ "alg" : alg. to_string( ) ,
362+ "content-type" : content_type. to_string( ) ,
363+ "type" : uuid_v4. to_string( ) ,
364+ "id" : uuid_v7. to_string( ) ,
365+ } ) ;
366+ assert ! ( serde_json:: from_value:: <Metadata >( missing_ver) . is_err( ) ) ;
367+ }
368+ }
0 commit comments