@@ -665,47 +665,52 @@ 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 Dictionary < string , TValue > AddToDictionary < TValue > ( Dictionary < string , TValue > ? dict , string key , TValue value )
670
+ {
671
+ dict ??= new Dictionary < string , TValue > ( ) ;
672
+ #if NET5_0_OR_GREATER
673
+ dict . TryAdd ( key , value ) ;
674
+ #else
675
+ if ( ! dict . ContainsKey ( key ) )
676
+ {
677
+ dict . Add ( key , value ) ;
678
+ }
679
+ #endif
680
+ return dict ;
681
+ }
682
+
668
683
switch ( componentToRegister )
669
684
{
670
685
case IOpenApiSchema openApiSchema :
671
- Components . Schemas ??= [ ] ;
672
- Components . Schemas . Add ( id , openApiSchema ) ;
686
+ Components . Schemas = AddToDictionary ( Components . Schemas , id , openApiSchema ) ;
673
687
break ;
674
688
case IOpenApiParameter openApiParameter :
675
- Components . Parameters ??= [ ] ;
676
- Components . Parameters . Add ( id , openApiParameter ) ;
689
+ Components . Parameters = AddToDictionary ( Components . Parameters , id , openApiParameter ) ;
677
690
break ;
678
691
case IOpenApiResponse openApiResponse :
679
- Components . Responses ??= [ ] ;
680
- Components . Responses . Add ( id , openApiResponse ) ;
692
+ Components . Responses = AddToDictionary ( Components . Responses , id , openApiResponse ) ;
681
693
break ;
682
694
case IOpenApiRequestBody openApiRequestBody :
683
- Components . RequestBodies ??= [ ] ;
684
- Components . RequestBodies . Add ( id , openApiRequestBody ) ;
695
+ Components . RequestBodies = AddToDictionary ( Components . RequestBodies , id , openApiRequestBody ) ;
685
696
break ;
686
697
case IOpenApiLink openApiLink :
687
- Components . Links ??= [ ] ;
688
- Components . Links . Add ( id , openApiLink ) ;
698
+ Components . Links = AddToDictionary ( Components . Links , id , openApiLink ) ;
689
699
break ;
690
700
case IOpenApiCallback openApiCallback :
691
- Components . Callbacks ??= [ ] ;
692
- Components . Callbacks . Add ( id , openApiCallback ) ;
701
+ Components . Callbacks = AddToDictionary ( Components . Callbacks , id , openApiCallback ) ;
693
702
break ;
694
703
case IOpenApiPathItem openApiPathItem :
695
- Components . PathItems ??= [ ] ;
696
- Components . PathItems . Add ( id , openApiPathItem ) ;
704
+ Components . PathItems = AddToDictionary ( Components . PathItems , id , openApiPathItem ) ;
697
705
break ;
698
706
case IOpenApiExample openApiExample :
699
- Components . Examples ??= [ ] ;
700
- Components . Examples . Add ( id , openApiExample ) ;
707
+ Components . Examples = AddToDictionary ( Components . Examples , id , openApiExample ) ;
701
708
break ;
702
709
case IOpenApiHeader openApiHeader :
703
- Components . Headers ??= [ ] ;
704
- Components . Headers . Add ( id , openApiHeader ) ;
710
+ Components . Headers = AddToDictionary ( Components . Headers , id , openApiHeader ) ;
705
711
break ;
706
712
case IOpenApiSecurityScheme openApiSecurityScheme :
707
- Components . SecuritySchemes ??= [ ] ;
708
- Components . SecuritySchemes . Add ( id , openApiSecurityScheme ) ;
713
+ Components . SecuritySchemes = AddToDictionary ( Components . SecuritySchemes , id , openApiSecurityScheme ) ;
709
714
break ;
710
715
default :
711
716
throw new ArgumentException ( $ "Component type { componentToRegister ! . GetType ( ) . Name } is not supported.") ;
0 commit comments