@@ -504,3 +504,90 @@ fn error_source_location() {
504
504
"myfile.proto:2:9: name 'my.package.Foo' is defined twice"
505
505
) ;
506
506
}
507
+
508
+ #[ test]
509
+ fn field_is_required_proto2 ( ) {
510
+ let file_descriptor_set = FileDescriptorSet {
511
+ file : vec ! [ FileDescriptorProto {
512
+ name: Some ( "myfile.proto" . to_owned( ) ) ,
513
+ package: Some ( "my.package" . to_owned( ) ) ,
514
+ syntax: Some ( "proto2" . to_owned( ) ) ,
515
+ message_type: vec![ DescriptorProto {
516
+ name: Some ( "MyMessage" . to_owned( ) ) ,
517
+ field: vec![
518
+ FieldDescriptorProto {
519
+ name: Some ( "required_field" . to_owned( ) ) ,
520
+ number: Some ( 1 ) ,
521
+ label: Some ( Label :: Required as i32 ) ,
522
+ r#type: Some ( Type :: String as i32 ) ,
523
+ ..Default :: default ( )
524
+ } ,
525
+ FieldDescriptorProto {
526
+ name: Some ( "optional_field" . to_owned( ) ) ,
527
+ number: Some ( 2 ) ,
528
+ label: Some ( Label :: Optional as i32 ) ,
529
+ r#type: Some ( Type :: String as i32 ) ,
530
+ ..Default :: default ( )
531
+ } ,
532
+ ] ,
533
+ ..Default :: default ( )
534
+ } ] ,
535
+ ..Default :: default ( )
536
+ } ] ,
537
+ } ;
538
+
539
+ let descriptor_pool = DescriptorPool :: from_file_descriptor_set ( file_descriptor_set) . unwrap ( ) ;
540
+ let message = descriptor_pool
541
+ . get_message_by_name ( "my.package.MyMessage" )
542
+ . unwrap ( ) ;
543
+
544
+ let required_field = message. get_field_by_name ( "required_field" ) . unwrap ( ) ;
545
+ let optional_field = message. get_field_by_name ( "optional_field" ) . unwrap ( ) ;
546
+
547
+ assert ! ( required_field. is_required( ) ) ;
548
+ assert ! ( !optional_field. is_required( ) ) ;
549
+ }
550
+
551
+ #[ test]
552
+ fn field_is_required_proto3 ( ) {
553
+ let file_descriptor_set = FileDescriptorSet {
554
+ file : vec ! [ FileDescriptorProto {
555
+ name: Some ( "myfile.proto" . to_owned( ) ) ,
556
+ package: Some ( "my.package" . to_owned( ) ) ,
557
+ syntax: Some ( "proto3" . to_owned( ) ) ,
558
+ message_type: vec![ DescriptorProto {
559
+ name: Some ( "MyMessage" . to_owned( ) ) ,
560
+ field: vec![
561
+ FieldDescriptorProto {
562
+ name: Some ( "optional_field" . to_owned( ) ) ,
563
+ number: Some ( 1 ) ,
564
+ label: Some ( Label :: Optional as i32 ) ,
565
+ r#type: Some ( Type :: String as i32 ) ,
566
+ ..Default :: default ( )
567
+ } ,
568
+ FieldDescriptorProto {
569
+ name: Some ( "repeated_field" . to_owned( ) ) ,
570
+ number: Some ( 2 ) ,
571
+ label: Some ( Label :: Repeated as i32 ) ,
572
+ r#type: Some ( Type :: String as i32 ) ,
573
+ ..Default :: default ( )
574
+ } ,
575
+ ] ,
576
+ ..Default :: default ( )
577
+ } ] ,
578
+ ..Default :: default ( )
579
+ } ] ,
580
+ } ;
581
+
582
+ let descriptor_pool = DescriptorPool :: from_file_descriptor_set ( file_descriptor_set) . unwrap ( ) ;
583
+ let message = descriptor_pool
584
+ . get_message_by_name ( "my.package.MyMessage" )
585
+ . unwrap ( ) ;
586
+
587
+ let optional_field = message. get_field_by_name ( "optional_field" ) . unwrap ( ) ;
588
+ let repeated_field = message. get_field_by_name ( "repeated_field" ) . unwrap ( ) ;
589
+
590
+ // In proto3, there are no required fields
591
+ assert ! ( !optional_field. is_required( ) ) ;
592
+ assert ! ( !repeated_field. is_required( ) ) ;
593
+ }
0 commit comments