@@ -665,47 +665,52 @@ public bool AddComponent<T>(string id, T componentToRegister)
665665 Utils . CheckArgumentNull ( componentToRegister ) ;
666666 Utils . CheckArgumentNullOrEmpty ( id ) ;
667667 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+
668683 switch ( componentToRegister )
669684 {
670685 case IOpenApiSchema openApiSchema :
671- Components . Schemas ??= [ ] ;
672- Components . Schemas . Add ( id , openApiSchema ) ;
686+ Components . Schemas = AddToDictionary ( Components . Schemas , id , openApiSchema ) ;
673687 break ;
674688 case IOpenApiParameter openApiParameter :
675- Components . Parameters ??= [ ] ;
676- Components . Parameters . Add ( id , openApiParameter ) ;
689+ Components . Parameters = AddToDictionary ( Components . Parameters , id , openApiParameter ) ;
677690 break ;
678691 case IOpenApiResponse openApiResponse :
679- Components . Responses ??= [ ] ;
680- Components . Responses . Add ( id , openApiResponse ) ;
692+ Components . Responses = AddToDictionary ( Components . Responses , id , openApiResponse ) ;
681693 break ;
682694 case IOpenApiRequestBody openApiRequestBody :
683- Components . RequestBodies ??= [ ] ;
684- Components . RequestBodies . Add ( id , openApiRequestBody ) ;
695+ Components . RequestBodies = AddToDictionary ( Components . RequestBodies , id , openApiRequestBody ) ;
685696 break ;
686697 case IOpenApiLink openApiLink :
687- Components . Links ??= [ ] ;
688- Components . Links . Add ( id , openApiLink ) ;
698+ Components . Links = AddToDictionary ( Components . Links , id , openApiLink ) ;
689699 break ;
690700 case IOpenApiCallback openApiCallback :
691- Components . Callbacks ??= [ ] ;
692- Components . Callbacks . Add ( id , openApiCallback ) ;
701+ Components . Callbacks = AddToDictionary ( Components . Callbacks , id , openApiCallback ) ;
693702 break ;
694703 case IOpenApiPathItem openApiPathItem :
695- Components . PathItems ??= [ ] ;
696- Components . PathItems . Add ( id , openApiPathItem ) ;
704+ Components . PathItems = AddToDictionary ( Components . PathItems , id , openApiPathItem ) ;
697705 break ;
698706 case IOpenApiExample openApiExample :
699- Components . Examples ??= [ ] ;
700- Components . Examples . Add ( id , openApiExample ) ;
707+ Components . Examples = AddToDictionary ( Components . Examples , id , openApiExample ) ;
701708 break ;
702709 case IOpenApiHeader openApiHeader :
703- Components . Headers ??= [ ] ;
704- Components . Headers . Add ( id , openApiHeader ) ;
710+ Components . Headers = AddToDictionary ( Components . Headers , id , openApiHeader ) ;
705711 break ;
706712 case IOpenApiSecurityScheme openApiSecurityScheme :
707- Components . SecuritySchemes ??= [ ] ;
708- Components . SecuritySchemes . Add ( id , openApiSecurityScheme ) ;
713+ Components . SecuritySchemes = AddToDictionary ( Components . SecuritySchemes , id , openApiSecurityScheme ) ;
709714 break ;
710715 default :
711716 throw new ArgumentException ( $ "Component type { componentToRegister ! . GetType ( ) . Name } is not supported.") ;
0 commit comments