4
4
using System ;
5
5
using System . Collections . Generic ;
6
6
using System . IO ;
7
- using System . Linq ;
8
7
using Json . Schema ;
9
8
using Microsoft . OpenApi . Exceptions ;
10
9
using Microsoft . OpenApi . Extensions ;
@@ -18,10 +17,10 @@ namespace Microsoft.OpenApi.Services
18
17
/// </summary>
19
18
public class OpenApiWorkspace
20
19
{
21
- private readonly Dictionary < Uri , OpenApiDocument > _documents = new ( ) ;
22
- private readonly Dictionary < Uri , IOpenApiReferenceable > _fragments = new ( ) ;
23
- private readonly Dictionary < Uri , JsonSchema > _schemaFragments = new ( ) ;
24
- private readonly Dictionary < Uri , Stream > _artifacts = new ( ) ;
20
+ private readonly Dictionary < Uri , OpenApiDocument > _documentsRegistry = new ( ) ;
21
+ private readonly Dictionary < Uri , IOpenApiReferenceable > _fragmentsRegistry = new ( ) ;
22
+ private readonly Dictionary < Uri , JsonSchema > _schemaFragmentsRegistry = new ( ) ;
23
+ private readonly Dictionary < Uri , Stream > _artifactsRegistry = new ( ) ;
25
24
26
25
/// <summary>
27
26
/// A list of OpenApiDocuments contained in the workspace
@@ -30,7 +29,7 @@ public IEnumerable<OpenApiDocument> Documents
30
29
{
31
30
get
32
31
{
33
- return _documents . Values ;
32
+ return _documentsRegistry . Values ;
34
33
}
35
34
}
36
35
@@ -71,11 +70,6 @@ public OpenApiWorkspace()
71
70
/// </summary>
72
71
public OpenApiWorkspace ( OpenApiWorkspace workspace ) { }
73
72
74
- /// <summary>
75
- ///
76
- /// </summary>
77
- public IDictionary < Uri , OpenApiComponents > ComponentsRegistry { get ; } = new Dictionary < Uri , OpenApiComponents > ( ) ;
78
-
79
73
/// <summary>
80
74
/// Verify if workspace contains a document based on its URL.
81
75
/// </summary>
@@ -84,7 +78,7 @@ public OpenApiWorkspace(OpenApiWorkspace workspace) { }
84
78
public bool Contains ( string location )
85
79
{
86
80
var key = ToLocationUrl ( location ) ;
87
- return _documents . ContainsKey ( key ) || _fragments . ContainsKey ( key ) || _artifacts . ContainsKey ( key ) || _schemaFragments . ContainsKey ( key ) ;
81
+ return _documentsRegistry . ContainsKey ( key ) || _fragmentsRegistry . ContainsKey ( key ) || _artifactsRegistry . ContainsKey ( key ) || _schemaFragmentsRegistry . ContainsKey ( key ) ;
88
82
}
89
83
90
84
/// <summary>
@@ -97,9 +91,9 @@ public void AddDocument(string location, OpenApiDocument document)
97
91
document . Workspace = this ;
98
92
var locationUrl = ToLocationUrl ( location ) ;
99
93
100
- if ( ! _documents . ContainsKey ( locationUrl ) )
94
+ if ( ! _documentsRegistry . ContainsKey ( locationUrl ) )
101
95
{
102
- _documents . Add ( locationUrl , document ) ;
96
+ _documentsRegistry . Add ( locationUrl , document ) ;
103
97
}
104
98
}
105
99
@@ -113,7 +107,7 @@ public void AddDocument(string location, OpenApiDocument document)
113
107
/// </remarks>
114
108
public void AddFragment ( string location , IOpenApiReferenceable fragment )
115
109
{
116
- _fragments . Add ( ToLocationUrl ( location ) , fragment ) ;
110
+ _fragmentsRegistry . Add ( ToLocationUrl ( location ) , fragment ) ;
117
111
}
118
112
119
113
/// <summary>
@@ -124,20 +118,20 @@ public void AddFragment(string location, IOpenApiReferenceable fragment)
124
118
public void AddSchemaFragment ( string location , JsonSchema fragment )
125
119
{
126
120
var locationUri = ToLocationUrl ( location ) ;
127
- if ( ! _schemaFragments . ContainsKey ( locationUri ) )
121
+ if ( ! _schemaFragmentsRegistry . ContainsKey ( locationUri ) )
128
122
{
129
- _schemaFragments . Add ( locationUri , fragment ) ;
123
+ _schemaFragmentsRegistry . Add ( locationUri , fragment ) ;
130
124
}
131
125
}
132
126
133
127
/// <summary>
134
- /// Add a stream based artificat to the workspace. Useful for images, examples, alternative schemas.
128
+ /// Add a stream based artifact to the workspace. Useful for images, examples, alternative schemas.
135
129
/// </summary>
136
130
/// <param name="location"></param>
137
131
/// <param name="artifact"></param>
138
132
public void AddArtifact ( string location , Stream artifact )
139
133
{
140
- _artifacts . Add ( ToLocationUrl ( location ) , artifact ) ;
134
+ _artifactsRegistry . Add ( ToLocationUrl ( location ) , artifact ) ;
141
135
}
142
136
143
137
/// <summary>
@@ -149,21 +143,20 @@ public void AddArtifact(string location, Stream artifact)
149
143
public T ResolveReference < T > ( OpenApiReference reference )
150
144
{
151
145
var uri = new Uri ( BaseUrl , reference . ExternalResource ) ;
152
- if ( _documents . TryGetValue ( uri , out var doc ) )
146
+ if ( _documentsRegistry . TryGetValue ( uri , out var doc ) )
153
147
{
154
148
return ResolveReference < T > ( reference . Id , reference . Type , doc . Components ) ;
155
149
}
156
- else if ( _fragments . TryGetValue ( uri , out var fragment ) )
150
+ else if ( _fragmentsRegistry . TryGetValue ( uri , out var fragment ) )
157
151
{
158
152
var jsonPointer = new JsonPointer ( $ "/{ reference . Id ?? string . Empty } ") ;
159
153
return ( T ) fragment . ResolveReference ( jsonPointer ) ;
160
154
}
161
- else if ( _schemaFragments . TryGetValue ( uri , out var schemaFragment ) )
155
+ else if ( _schemaFragmentsRegistry . TryGetValue ( uri , out var schemaFragment ) )
162
156
{
163
157
return ( T ) ( schemaFragment as IBaseDocument ) ;
164
158
}
165
159
return default ;
166
-
167
160
}
168
161
169
162
/// <summary>
@@ -180,20 +173,27 @@ public T ResolveReference<T>(string referenceId, ReferenceType? referenceType, O
180
173
if ( string . IsNullOrEmpty ( referenceId ) ) return default ;
181
174
if ( components == null ) return default ;
182
175
183
- return referenceType switch
176
+ try
184
177
{
185
- ReferenceType . PathItem => ( T ) ( IOpenApiReferenceable ) components . PathItems [ referenceId ] ,
186
- ReferenceType . Response => ( T ) ( IOpenApiReferenceable ) components . Responses [ referenceId ] ,
187
- ReferenceType . Parameter => ( T ) ( IOpenApiReferenceable ) components . Parameters [ referenceId ] ,
188
- ReferenceType . Example => ( T ) ( IOpenApiReferenceable ) components . Examples [ referenceId ] ,
189
- ReferenceType . RequestBody => ( T ) ( IOpenApiReferenceable ) components . RequestBodies [ referenceId ] ,
190
- ReferenceType . Header => ( T ) ( IOpenApiReferenceable ) components . Headers [ referenceId ] ,
191
- ReferenceType . SecurityScheme => ( T ) ( IOpenApiReferenceable ) components . SecuritySchemes [ referenceId ] ,
192
- ReferenceType . Link => ( T ) ( IOpenApiReferenceable ) components . Links [ referenceId ] ,
193
- ReferenceType . Callback => ( T ) ( IOpenApiReferenceable ) components . Callbacks [ referenceId ] ,
194
- ReferenceType . Schema => ( T ) ( IBaseDocument ) components . Schemas [ referenceId ] ,
195
- _ => throw new OpenApiException ( Properties . SRResource . InvalidReferenceType ) ,
196
- } ;
178
+ return referenceType switch
179
+ {
180
+ ReferenceType . PathItem => ( T ) ( IOpenApiReferenceable ) components . PathItems [ referenceId ] ,
181
+ ReferenceType . Response => ( T ) ( IOpenApiReferenceable ) components . Responses [ referenceId ] ,
182
+ ReferenceType . Parameter => ( T ) ( IOpenApiReferenceable ) components . Parameters [ referenceId ] ,
183
+ ReferenceType . Example => ( T ) ( IOpenApiReferenceable ) components . Examples [ referenceId ] ,
184
+ ReferenceType . RequestBody => ( T ) ( IOpenApiReferenceable ) components . RequestBodies [ referenceId ] ,
185
+ ReferenceType . Header => ( T ) ( IOpenApiReferenceable ) components . Headers [ referenceId ] ,
186
+ ReferenceType . SecurityScheme => ( T ) ( IOpenApiReferenceable ) components . SecuritySchemes [ referenceId ] ,
187
+ ReferenceType . Link => ( T ) ( IOpenApiReferenceable ) components . Links [ referenceId ] ,
188
+ ReferenceType . Callback => ( T ) ( IOpenApiReferenceable ) components . Callbacks [ referenceId ] ,
189
+ ReferenceType . Schema => ( T ) ( IBaseDocument ) components . Schemas [ referenceId ] ,
190
+ _ => throw new OpenApiException ( Properties . SRResource . InvalidReferenceType )
191
+ } ;
192
+ }
193
+ catch ( KeyNotFoundException )
194
+ {
195
+ throw new OpenApiException ( string . Format ( Properties . SRResource . InvalidReferenceId , referenceId ) ) ;
196
+ }
197
197
}
198
198
199
199
/// <summary>
@@ -203,18 +203,12 @@ public T ResolveReference<T>(string referenceId, ReferenceType? referenceType, O
203
203
/// <returns></returns>
204
204
public Stream GetArtifact ( string location )
205
205
{
206
- return _artifacts [ ToLocationUrl ( location ) ] ;
206
+ return _artifactsRegistry [ ToLocationUrl ( location ) ] ;
207
207
}
208
208
209
209
private Uri ToLocationUrl ( string location )
210
210
{
211
211
return new ( BaseUrl , location ) ;
212
212
}
213
-
214
- private static JsonSchema FetchSchemaFromRegistry ( Uri reference )
215
- {
216
- var resolvedSchema = ( JsonSchema ) SchemaRegistry . Global . Get ( reference ) ;
217
- return resolvedSchema ;
218
- }
219
213
}
220
214
}
0 commit comments