@@ -54,6 +54,7 @@ public int ComponentsCount()
5454 {
5555 return _IOpenApiReferenceableRegistry . Count + _artifactsRegistry . Count ;
5656 }
57+ private const string ComponentSegmentSeparator = "/" ;
5758
5859 /// <summary>
5960 /// Registers a document's components into the workspace
@@ -63,89 +64,130 @@ public void RegisterComponents(OpenApiDocument document)
6364 {
6465 if ( document ? . Components == null ) return ;
6566
66- string baseUri = document . BaseUri + OpenApiConstants . ComponentsSegment ;
67+ string baseUri = getBaseUri ( document ) ;
6768 string location ;
6869
6970 // Register Schema
7071 foreach ( var item in document . Components . Schemas )
7172 {
72- location = item . Value . Id ?? baseUri + ReferenceType . Schema . GetDisplayName ( ) + "/" + item . Key ;
73+ location = item . Value . Id ?? baseUri + ReferenceType . Schema . GetDisplayName ( ) + ComponentSegmentSeparator + item . Key ;
7374
7475 RegisterComponent ( location , item . Value ) ;
7576 }
7677
7778 // Register Parameters
7879 foreach ( var item in document . Components . Parameters )
7980 {
80- location = baseUri + ReferenceType . Parameter . GetDisplayName ( ) + "/" + item . Key ;
81+ location = baseUri + ReferenceType . Parameter . GetDisplayName ( ) + ComponentSegmentSeparator + item . Key ;
8182 RegisterComponent ( location , item . Value ) ;
8283 }
8384
8485 // Register Responses
8586 foreach ( var item in document . Components . Responses )
8687 {
87- location = baseUri + ReferenceType . Response . GetDisplayName ( ) + "/" + item . Key ;
88+ location = baseUri + ReferenceType . Response . GetDisplayName ( ) + ComponentSegmentSeparator + item . Key ;
8889 RegisterComponent ( location , item . Value ) ;
8990 }
9091
9192 // Register RequestBodies
9293 foreach ( var item in document . Components . RequestBodies )
9394 {
94- location = baseUri + ReferenceType . RequestBody . GetDisplayName ( ) + "/" + item . Key ;
95+ location = baseUri + ReferenceType . RequestBody . GetDisplayName ( ) + ComponentSegmentSeparator + item . Key ;
9596 RegisterComponent ( location , item . Value ) ;
9697 }
9798
9899 // Register Links
99100 foreach ( var item in document . Components . Links )
100101 {
101- location = baseUri + ReferenceType . Link . GetDisplayName ( ) + "/" + item . Key ;
102+ location = baseUri + ReferenceType . Link . GetDisplayName ( ) + ComponentSegmentSeparator + item . Key ;
102103 RegisterComponent ( location , item . Value ) ;
103104 }
104105
105106 // Register Callbacks
106107 foreach ( var item in document . Components . Callbacks )
107108 {
108- location = baseUri + ReferenceType . Callback . GetDisplayName ( ) + "/" + item . Key ;
109+ location = baseUri + ReferenceType . Callback . GetDisplayName ( ) + ComponentSegmentSeparator + item . Key ;
109110 RegisterComponent ( location , item . Value ) ;
110111 }
111112
112113 // Register PathItems
113114 foreach ( var item in document . Components . PathItems )
114115 {
115- location = baseUri + ReferenceType . PathItem . GetDisplayName ( ) + "/" + item . Key ;
116+ location = baseUri + ReferenceType . PathItem . GetDisplayName ( ) + ComponentSegmentSeparator + item . Key ;
116117 RegisterComponent ( location , item . Value ) ;
117118 }
118119
119120 // Register Examples
120121 foreach ( var item in document . Components . Examples )
121122 {
122- location = baseUri + ReferenceType . Example . GetDisplayName ( ) + "/" + item . Key ;
123+ location = baseUri + ReferenceType . Example . GetDisplayName ( ) + ComponentSegmentSeparator + item . Key ;
123124 RegisterComponent ( location , item . Value ) ;
124125 }
125126
126127 // Register Headers
127128 foreach ( var item in document . Components . Headers )
128129 {
129- location = baseUri + ReferenceType . Header . GetDisplayName ( ) + "/" + item . Key ;
130+ location = baseUri + ReferenceType . Header . GetDisplayName ( ) + ComponentSegmentSeparator + item . Key ;
130131 RegisterComponent ( location , item . Value ) ;
131132 }
132133
133134 // Register SecuritySchemes
134135 foreach ( var item in document . Components . SecuritySchemes )
135136 {
136- location = baseUri + ReferenceType . SecurityScheme . GetDisplayName ( ) + "/" + item . Key ;
137+ location = baseUri + ReferenceType . SecurityScheme . GetDisplayName ( ) + ComponentSegmentSeparator + item . Key ;
137138 RegisterComponent ( location , item . Value ) ;
138139 }
139140 }
140141
142+ private string getBaseUri ( OpenApiDocument openApiDocument )
143+ {
144+ return openApiDocument . BaseUri + OpenApiConstants . ComponentsSegment ;
145+ }
146+
147+ /// <summary>
148+ /// Registers a component for a document in the workspace
149+ /// </summary>
150+ /// <param name="openApiDocument">The document to register the component for.</param>
151+ /// <param name="componentToRegister">The component to register.</param>
152+ /// <param name="id">The id of the component.</param>
153+ /// <typeparam name="T">The type of the component to register.</typeparam>
154+ /// <returns>true if the component is successfully registered; otherwise false.</returns>
155+ /// <exception cref="ArgumentNullException">openApiDocument is null</exception>
156+ /// <exception cref="ArgumentNullException">componentToRegister is null</exception>
157+ /// <exception cref="ArgumentNullException">id is null or empty</exception>
158+ public bool RegisterComponentForDocument < T > ( OpenApiDocument openApiDocument , T componentToRegister , string id )
159+ {
160+ Utils . CheckArgumentNull ( openApiDocument ) ;
161+ Utils . CheckArgumentNull ( componentToRegister ) ;
162+ Utils . CheckArgumentNullOrEmpty ( id ) ;
163+
164+ var baseUri = getBaseUri ( openApiDocument ) ;
165+
166+ var location = componentToRegister switch
167+ {
168+ OpenApiSchema => baseUri + ReferenceType . Schema . GetDisplayName ( ) + ComponentSegmentSeparator + id ,
169+ OpenApiParameter => baseUri + ReferenceType . Parameter . GetDisplayName ( ) + ComponentSegmentSeparator + id ,
170+ OpenApiResponse => baseUri + ReferenceType . Response . GetDisplayName ( ) + ComponentSegmentSeparator + id ,
171+ OpenApiRequestBody => baseUri + ReferenceType . RequestBody . GetDisplayName ( ) + ComponentSegmentSeparator + id ,
172+ OpenApiLink => baseUri + ReferenceType . Link . GetDisplayName ( ) + ComponentSegmentSeparator + id ,
173+ OpenApiCallback => baseUri + ReferenceType . Callback . GetDisplayName ( ) + ComponentSegmentSeparator + id ,
174+ OpenApiPathItem => baseUri + ReferenceType . PathItem . GetDisplayName ( ) + ComponentSegmentSeparator + id ,
175+ OpenApiExample => baseUri + ReferenceType . Example . GetDisplayName ( ) + ComponentSegmentSeparator + id ,
176+ OpenApiHeader => baseUri + ReferenceType . Header . GetDisplayName ( ) + ComponentSegmentSeparator + id ,
177+ OpenApiSecurityScheme => baseUri + ReferenceType . SecurityScheme . GetDisplayName ( ) + ComponentSegmentSeparator + id ,
178+ _ => throw new ArgumentException ( $ "Invalid component type { componentToRegister . GetType ( ) . Name } ") ,
179+ } ;
180+
181+ return RegisterComponent ( location , componentToRegister ) ;
182+ }
141183
142184 /// <summary>
143185 /// Registers a component in the component registry.
144186 /// </summary>
145187 /// <param name="location"></param>
146188 /// <param name="component"></param>
147189 /// <returns>true if the component is successfully registered; otherwise false.</returns>
148- public bool RegisterComponent < T > ( string location , T component )
190+ internal bool RegisterComponent < T > ( string location , T component )
149191 {
150192 var uri = ToLocationUrl ( location ) ;
151193 if ( component is IOpenApiReferenceable referenceable )
0 commit comments