@@ -54,6 +54,7 @@ public int ComponentsCount()
54
54
{
55
55
return _IOpenApiReferenceableRegistry . Count + _artifactsRegistry . Count ;
56
56
}
57
+ private const string ComponentSegmentSeparator = "/" ;
57
58
58
59
/// <summary>
59
60
/// Registers a document's components into the workspace
@@ -63,89 +64,130 @@ public void RegisterComponents(OpenApiDocument document)
63
64
{
64
65
if ( document ? . Components == null ) return ;
65
66
66
- string baseUri = document . BaseUri + OpenApiConstants . ComponentsSegment ;
67
+ string baseUri = getBaseUri ( document ) ;
67
68
string location ;
68
69
69
70
// Register Schema
70
71
foreach ( var item in document . Components . Schemas )
71
72
{
72
- location = item . Value . Id ?? baseUri + ReferenceType . Schema . GetDisplayName ( ) + "/" + item . Key ;
73
+ location = item . Value . Id ?? baseUri + ReferenceType . Schema . GetDisplayName ( ) + ComponentSegmentSeparator + item . Key ;
73
74
74
75
RegisterComponent ( location , item . Value ) ;
75
76
}
76
77
77
78
// Register Parameters
78
79
foreach ( var item in document . Components . Parameters )
79
80
{
80
- location = baseUri + ReferenceType . Parameter . GetDisplayName ( ) + "/" + item . Key ;
81
+ location = baseUri + ReferenceType . Parameter . GetDisplayName ( ) + ComponentSegmentSeparator + item . Key ;
81
82
RegisterComponent ( location , item . Value ) ;
82
83
}
83
84
84
85
// Register Responses
85
86
foreach ( var item in document . Components . Responses )
86
87
{
87
- location = baseUri + ReferenceType . Response . GetDisplayName ( ) + "/" + item . Key ;
88
+ location = baseUri + ReferenceType . Response . GetDisplayName ( ) + ComponentSegmentSeparator + item . Key ;
88
89
RegisterComponent ( location , item . Value ) ;
89
90
}
90
91
91
92
// Register RequestBodies
92
93
foreach ( var item in document . Components . RequestBodies )
93
94
{
94
- location = baseUri + ReferenceType . RequestBody . GetDisplayName ( ) + "/" + item . Key ;
95
+ location = baseUri + ReferenceType . RequestBody . GetDisplayName ( ) + ComponentSegmentSeparator + item . Key ;
95
96
RegisterComponent ( location , item . Value ) ;
96
97
}
97
98
98
99
// Register Links
99
100
foreach ( var item in document . Components . Links )
100
101
{
101
- location = baseUri + ReferenceType . Link . GetDisplayName ( ) + "/" + item . Key ;
102
+ location = baseUri + ReferenceType . Link . GetDisplayName ( ) + ComponentSegmentSeparator + item . Key ;
102
103
RegisterComponent ( location , item . Value ) ;
103
104
}
104
105
105
106
// Register Callbacks
106
107
foreach ( var item in document . Components . Callbacks )
107
108
{
108
- location = baseUri + ReferenceType . Callback . GetDisplayName ( ) + "/" + item . Key ;
109
+ location = baseUri + ReferenceType . Callback . GetDisplayName ( ) + ComponentSegmentSeparator + item . Key ;
109
110
RegisterComponent ( location , item . Value ) ;
110
111
}
111
112
112
113
// Register PathItems
113
114
foreach ( var item in document . Components . PathItems )
114
115
{
115
- location = baseUri + ReferenceType . PathItem . GetDisplayName ( ) + "/" + item . Key ;
116
+ location = baseUri + ReferenceType . PathItem . GetDisplayName ( ) + ComponentSegmentSeparator + item . Key ;
116
117
RegisterComponent ( location , item . Value ) ;
117
118
}
118
119
119
120
// Register Examples
120
121
foreach ( var item in document . Components . Examples )
121
122
{
122
- location = baseUri + ReferenceType . Example . GetDisplayName ( ) + "/" + item . Key ;
123
+ location = baseUri + ReferenceType . Example . GetDisplayName ( ) + ComponentSegmentSeparator + item . Key ;
123
124
RegisterComponent ( location , item . Value ) ;
124
125
}
125
126
126
127
// Register Headers
127
128
foreach ( var item in document . Components . Headers )
128
129
{
129
- location = baseUri + ReferenceType . Header . GetDisplayName ( ) + "/" + item . Key ;
130
+ location = baseUri + ReferenceType . Header . GetDisplayName ( ) + ComponentSegmentSeparator + item . Key ;
130
131
RegisterComponent ( location , item . Value ) ;
131
132
}
132
133
133
134
// Register SecuritySchemes
134
135
foreach ( var item in document . Components . SecuritySchemes )
135
136
{
136
- location = baseUri + ReferenceType . SecurityScheme . GetDisplayName ( ) + "/" + item . Key ;
137
+ location = baseUri + ReferenceType . SecurityScheme . GetDisplayName ( ) + ComponentSegmentSeparator + item . Key ;
137
138
RegisterComponent ( location , item . Value ) ;
138
139
}
139
140
}
140
141
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
+ }
141
183
142
184
/// <summary>
143
185
/// Registers a component in the component registry.
144
186
/// </summary>
145
187
/// <param name="location"></param>
146
188
/// <param name="component"></param>
147
189
/// <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 )
149
191
{
150
192
var uri = ToLocationUrl ( location ) ;
151
193
if ( component is IOpenApiReferenceable referenceable )
0 commit comments