@@ -666,56 +666,69 @@ public bool AddComponent<T>(string id, T componentToRegister)
666666 Utils . CheckArgumentNullOrEmpty ( id ) ;
667667 Components ??= new ( ) ;
668668
669- static Dictionary < string , TValue > AddToDictionary < TValue > ( Dictionary < string , TValue > ? dict , string key , TValue value )
669+ static bool AddToDictionary < TValue > ( Dictionary < string , TValue > dict , string key , TValue value )
670670 {
671- dict ??= new Dictionary < string , TValue > ( ) ;
672671#if NET5_0_OR_GREATER
673- dict . TryAdd ( key , value ) ;
672+ return dict . TryAdd ( key , value ) ;
674673#else
675674 if ( ! dict . ContainsKey ( key ) )
676675 {
677676 dict . Add ( key , value ) ;
677+ return true ;
678678 }
679+ return false ;
679680#endif
680- return dict ;
681681 }
682682
683+ bool added = false ;
683684 switch ( componentToRegister )
684685 {
685686 case IOpenApiSchema openApiSchema :
686- Components . Schemas = AddToDictionary ( Components . Schemas , id , openApiSchema ) ;
687+ Components . Schemas ??= [ ] ;
688+ added = AddToDictionary ( Components . Schemas , id , openApiSchema ) ;
687689 break ;
688690 case IOpenApiParameter openApiParameter :
689- Components . Parameters = AddToDictionary ( Components . Parameters , id , openApiParameter ) ;
691+ Components . Parameters ??= [ ] ;
692+ added = AddToDictionary ( Components . Parameters , id , openApiParameter ) ;
690693 break ;
691694 case IOpenApiResponse openApiResponse :
692- Components . Responses = AddToDictionary ( Components . Responses , id , openApiResponse ) ;
695+ Components . Responses ??= [ ] ;
696+ added = AddToDictionary ( Components . Responses , id , openApiResponse ) ;
693697 break ;
694698 case IOpenApiRequestBody openApiRequestBody :
695- Components . RequestBodies = AddToDictionary ( Components . RequestBodies , id , openApiRequestBody ) ;
699+ Components . RequestBodies ??= [ ] ;
700+ added = AddToDictionary ( Components . RequestBodies , id , openApiRequestBody ) ;
696701 break ;
697702 case IOpenApiLink openApiLink :
698- Components . Links = AddToDictionary ( Components . Links , id , openApiLink ) ;
703+ Components . Links ??= [ ] ;
704+ added = AddToDictionary ( Components . Links , id , openApiLink ) ;
699705 break ;
700706 case IOpenApiCallback openApiCallback :
701- Components . Callbacks = AddToDictionary ( Components . Callbacks , id , openApiCallback ) ;
707+ Components . Callbacks ??= [ ] ;
708+ added = AddToDictionary ( Components . Callbacks , id , openApiCallback ) ;
702709 break ;
703710 case IOpenApiPathItem openApiPathItem :
704- Components . PathItems = AddToDictionary ( Components . PathItems , id , openApiPathItem ) ;
711+ Components . PathItems ??= [ ] ;
712+ added = AddToDictionary ( Components . PathItems , id , openApiPathItem ) ;
705713 break ;
706714 case IOpenApiExample openApiExample :
707- Components . Examples = AddToDictionary ( Components . Examples , id , openApiExample ) ;
715+ Components . Examples ??= [ ] ;
716+ added = AddToDictionary ( Components . Examples , id , openApiExample ) ;
708717 break ;
709718 case IOpenApiHeader openApiHeader :
710- Components . Headers = AddToDictionary ( Components . Headers , id , openApiHeader ) ;
719+ Components . Headers ??= [ ] ;
720+ added = AddToDictionary ( Components . Headers , id , openApiHeader ) ;
711721 break ;
712722 case IOpenApiSecurityScheme openApiSecurityScheme :
713- Components . SecuritySchemes = AddToDictionary ( Components . SecuritySchemes , id , openApiSecurityScheme ) ;
723+ Components . SecuritySchemes ??= [ ] ;
724+ added = AddToDictionary ( Components . SecuritySchemes , id , openApiSecurityScheme ) ;
714725 break ;
715726 default :
716727 throw new ArgumentException ( $ "Component type { componentToRegister ! . GetType ( ) . Name } is not supported.") ;
717728 }
718- 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 ) ;
719732 }
720733 }
721734
0 commit comments