@@ -665,52 +665,70 @@ public bool AddComponent<T>(string id, T componentToRegister)
665
665
Utils . CheckArgumentNull ( componentToRegister ) ;
666
666
Utils . CheckArgumentNullOrEmpty ( id ) ;
667
667
Components ??= new ( ) ;
668
+
669
+ static bool AddToDictionary < TValue > ( Dictionary < string , TValue > dict , string key , TValue value )
670
+ {
671
+ #if NET5_0_OR_GREATER
672
+ return dict . TryAdd ( key , value ) ;
673
+ #else
674
+ if ( ! dict . ContainsKey ( key ) )
675
+ {
676
+ dict . Add ( key , value ) ;
677
+ return true ;
678
+ }
679
+ return false ;
680
+ #endif
681
+ }
682
+
683
+ bool added = false ;
668
684
switch ( componentToRegister )
669
685
{
670
686
case IOpenApiSchema openApiSchema :
671
687
Components . Schemas ??= [ ] ;
672
- Components . Schemas . Add ( id , openApiSchema ) ;
688
+ added = AddToDictionary ( Components . Schemas , id , openApiSchema ) ;
673
689
break ;
674
690
case IOpenApiParameter openApiParameter :
675
691
Components . Parameters ??= [ ] ;
676
- Components . Parameters . Add ( id , openApiParameter ) ;
692
+ added = AddToDictionary ( Components . Parameters , id , openApiParameter ) ;
677
693
break ;
678
694
case IOpenApiResponse openApiResponse :
679
695
Components . Responses ??= [ ] ;
680
- Components . Responses . Add ( id , openApiResponse ) ;
696
+ added = AddToDictionary ( Components . Responses , id , openApiResponse ) ;
681
697
break ;
682
698
case IOpenApiRequestBody openApiRequestBody :
683
699
Components . RequestBodies ??= [ ] ;
684
- Components . RequestBodies . Add ( id , openApiRequestBody ) ;
700
+ added = AddToDictionary ( Components . RequestBodies , id , openApiRequestBody ) ;
685
701
break ;
686
702
case IOpenApiLink openApiLink :
687
703
Components . Links ??= [ ] ;
688
- Components . Links . Add ( id , openApiLink ) ;
704
+ added = AddToDictionary ( Components . Links , id , openApiLink ) ;
689
705
break ;
690
706
case IOpenApiCallback openApiCallback :
691
707
Components . Callbacks ??= [ ] ;
692
- Components . Callbacks . Add ( id , openApiCallback ) ;
708
+ added = AddToDictionary ( Components . Callbacks , id , openApiCallback ) ;
693
709
break ;
694
710
case IOpenApiPathItem openApiPathItem :
695
711
Components . PathItems ??= [ ] ;
696
- Components . PathItems . Add ( id , openApiPathItem ) ;
712
+ added = AddToDictionary ( Components . PathItems , id , openApiPathItem ) ;
697
713
break ;
698
714
case IOpenApiExample openApiExample :
699
715
Components . Examples ??= [ ] ;
700
- Components . Examples . Add ( id , openApiExample ) ;
716
+ added = AddToDictionary ( Components . Examples , id , openApiExample ) ;
701
717
break ;
702
718
case IOpenApiHeader openApiHeader :
703
719
Components . Headers ??= [ ] ;
704
- Components . Headers . Add ( id , openApiHeader ) ;
720
+ added = AddToDictionary ( Components . Headers , id , openApiHeader ) ;
705
721
break ;
706
722
case IOpenApiSecurityScheme openApiSecurityScheme :
707
723
Components . SecuritySchemes ??= [ ] ;
708
- Components . SecuritySchemes . Add ( id , openApiSecurityScheme ) ;
724
+ added = AddToDictionary ( Components . SecuritySchemes , id , openApiSecurityScheme ) ;
709
725
break ;
710
726
default :
711
727
throw new ArgumentException ( $ "Component type { componentToRegister ! . GetType ( ) . Name } is not supported.") ;
712
728
}
713
- return Workspace ? . RegisterComponentForDocument ( this , componentToRegister , id ) ?? false ;
729
+
730
+ // Register only if it was actually added to the collection
731
+ return added && ( Workspace ? . RegisterComponentForDocument ( this , componentToRegister , id ) ?? false ) ;
714
732
}
715
733
}
716
734
0 commit comments