@@ -572,6 +572,67 @@ public static ReadResult Parse(string input,
572572 {
573573 return OpenApiModelFactory . Parse ( input , format , settings ) ;
574574 }
575+ /// <summary>
576+ /// Adds a component to the components object of the current document and registers it to the underlying workspace.
577+ /// </summary>
578+ /// <param name="componentToRegister">The component to add</param>
579+ /// <param name="id">The id for the component</param>
580+ /// <typeparam name="T">The type of the component</typeparam>
581+ /// <returns>Whether the component was added to the components.</returns>
582+ /// <exception cref="ArgumentNullException">Thrown when the component is null.</exception>
583+ /// <exception cref="ArgumentException">Thrown when the id is null or empty.</exception>
584+ public bool AddComponent < T > ( string id , T componentToRegister )
585+ {
586+ Utils . CheckArgumentNull ( componentToRegister ) ;
587+ Utils . CheckArgumentNullOrEmpty ( id ) ;
588+ Components ??= new ( ) ;
589+ switch ( componentToRegister )
590+ {
591+ case OpenApiSchema openApiSchema :
592+ Components . Schemas ??= new Dictionary < string , OpenApiSchema > ( ) ;
593+ Components . Schemas . Add ( id , openApiSchema ) ;
594+ break ;
595+ case OpenApiParameter openApiParameter :
596+ Components . Parameters ??= new Dictionary < string , OpenApiParameter > ( ) ;
597+ Components . Parameters . Add ( id , openApiParameter ) ;
598+ break ;
599+ case OpenApiResponse openApiResponse :
600+ Components . Responses ??= new Dictionary < string , OpenApiResponse > ( ) ;
601+ Components . Responses . Add ( id , openApiResponse ) ;
602+ break ;
603+ case OpenApiRequestBody openApiRequestBody :
604+ Components . RequestBodies ??= new Dictionary < string , OpenApiRequestBody > ( ) ;
605+ Components . RequestBodies . Add ( id , openApiRequestBody ) ;
606+ break ;
607+ case OpenApiLink openApiLink :
608+ Components . Links ??= new Dictionary < string , OpenApiLink > ( ) ;
609+ Components . Links . Add ( id , openApiLink ) ;
610+ break ;
611+ case OpenApiCallback openApiCallback :
612+ Components . Callbacks ??= new Dictionary < string , OpenApiCallback > ( ) ;
613+ Components . Callbacks . Add ( id , openApiCallback ) ;
614+ break ;
615+ case OpenApiPathItem openApiPathItem :
616+ Components . PathItems ??= new Dictionary < string , OpenApiPathItem > ( ) ;
617+ Components . PathItems . Add ( id , openApiPathItem ) ;
618+ break ;
619+ case OpenApiExample openApiExample :
620+ Components . Examples ??= new Dictionary < string , OpenApiExample > ( ) ;
621+ Components . Examples . Add ( id , openApiExample ) ;
622+ break ;
623+ case OpenApiHeader openApiHeader :
624+ Components . Headers ??= new Dictionary < string , OpenApiHeader > ( ) ;
625+ Components . Headers . Add ( id , openApiHeader ) ;
626+ break ;
627+ case OpenApiSecurityScheme openApiSecurityScheme :
628+ Components . SecuritySchemes ??= new Dictionary < string , OpenApiSecurityScheme > ( ) ;
629+ Components . SecuritySchemes . Add ( id , openApiSecurityScheme ) ;
630+ break ;
631+ default :
632+ throw new ArgumentException ( $ "Component type { componentToRegister ! . GetType ( ) . Name } is not supported.") ;
633+ }
634+ return Workspace ? . RegisterComponentForDocument ( this , componentToRegister , id ) ?? false ;
635+ }
575636 }
576637
577638 internal class FindSchemaReferences : OpenApiVisitorBase
0 commit comments