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