@@ -665,52 +665,70 @@ public bool AddComponent<T>(string id, T componentToRegister)
665665 Utils . CheckArgumentNull ( componentToRegister ) ;
666666 Utils . CheckArgumentNullOrEmpty ( id ) ;
667667 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 ;
668684 switch ( componentToRegister )
669685 {
670686 case IOpenApiSchema openApiSchema :
671687 Components . Schemas ??= [ ] ;
672- Components . Schemas . Add ( id , openApiSchema ) ;
688+ added = AddToDictionary ( Components . Schemas , id , openApiSchema ) ;
673689 break ;
674690 case IOpenApiParameter openApiParameter :
675691 Components . Parameters ??= [ ] ;
676- Components . Parameters . Add ( id , openApiParameter ) ;
692+ added = AddToDictionary ( Components . Parameters , id , openApiParameter ) ;
677693 break ;
678694 case IOpenApiResponse openApiResponse :
679695 Components . Responses ??= [ ] ;
680- Components . Responses . Add ( id , openApiResponse ) ;
696+ added = AddToDictionary ( Components . Responses , id , openApiResponse ) ;
681697 break ;
682698 case IOpenApiRequestBody openApiRequestBody :
683699 Components . RequestBodies ??= [ ] ;
684- Components . RequestBodies . Add ( id , openApiRequestBody ) ;
700+ added = AddToDictionary ( Components . RequestBodies , id , openApiRequestBody ) ;
685701 break ;
686702 case IOpenApiLink openApiLink :
687703 Components . Links ??= [ ] ;
688- Components . Links . Add ( id , openApiLink ) ;
704+ added = AddToDictionary ( Components . Links , id , openApiLink ) ;
689705 break ;
690706 case IOpenApiCallback openApiCallback :
691707 Components . Callbacks ??= [ ] ;
692- Components . Callbacks . Add ( id , openApiCallback ) ;
708+ added = AddToDictionary ( Components . Callbacks , id , openApiCallback ) ;
693709 break ;
694710 case IOpenApiPathItem openApiPathItem :
695711 Components . PathItems ??= [ ] ;
696- Components . PathItems . Add ( id , openApiPathItem ) ;
712+ added = AddToDictionary ( Components . PathItems , id , openApiPathItem ) ;
697713 break ;
698714 case IOpenApiExample openApiExample :
699715 Components . Examples ??= [ ] ;
700- Components . Examples . Add ( id , openApiExample ) ;
716+ added = AddToDictionary ( Components . Examples , id , openApiExample ) ;
701717 break ;
702718 case IOpenApiHeader openApiHeader :
703719 Components . Headers ??= [ ] ;
704- Components . Headers . Add ( id , openApiHeader ) ;
720+ added = AddToDictionary ( Components . Headers , id , openApiHeader ) ;
705721 break ;
706722 case IOpenApiSecurityScheme openApiSecurityScheme :
707723 Components . SecuritySchemes ??= [ ] ;
708- Components . SecuritySchemes . Add ( id , openApiSecurityScheme ) ;
724+ added = AddToDictionary ( Components . SecuritySchemes , id , openApiSecurityScheme ) ;
709725 break ;
710726 default :
711727 throw new ArgumentException ( $ "Component type { componentToRegister ! . GetType ( ) . Name } is not supported.") ;
712728 }
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 ) ;
714732 }
715733 }
716734
0 commit comments