Skip to content

Commit 1b286e5

Browse files
committed
Update models
1 parent 4e480ed commit 1b286e5

File tree

2 files changed

+41
-49
lines changed

2 files changed

+41
-49
lines changed

src/Microsoft.OpenApi/Models/OpenApiDocument.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
using System.Threading;
1212
using System.Threading.Tasks;
1313
using Json.Schema;
14-
using Microsoft.OpenApi.Exceptions;
1514
using Microsoft.OpenApi.Interfaces;
1615
using Microsoft.OpenApi.Reader;
1716
using Microsoft.OpenApi.Services;
@@ -503,7 +502,7 @@ public JsonSchema ResolveJsonSchemaReference(Uri referenceUri)
503502
? (JsonSchema)Workspace.ResolveReference<IBaseDocument>(reference.Id, reference.Type, Components) // local ref
504503
: Workspace.ResolveReference<JsonSchema>(reference); // external ref
505504

506-
return resolvedSchema ?? throw new OpenApiException(string.Format(Properties.SRResource.InvalidReferenceId, reference.Id));
505+
return resolvedSchema;
507506
}
508507

509508
/// <summary>
@@ -553,11 +552,11 @@ internal IOpenApiReferenceable ResolveReference(OpenApiReference reference, bool
553552
// Todo: Verify if we need to check to see if this external reference is actually targeted at this document.
554553
if (useExternal)
555554
{
556-
if (this.Workspace == null)
555+
if (Workspace == null)
557556
{
558557
throw new ArgumentException(Properties.SRResource.WorkspaceRequredForExternalReferenceResolution);
559558
}
560-
return this.Workspace.ResolveReference<IOpenApiReferenceable>(reference);
559+
return Workspace.ResolveReference<IOpenApiReferenceable>(reference);
561560
}
562561

563562
if (!reference.Type.HasValue)
@@ -580,8 +579,7 @@ internal IOpenApiReferenceable ResolveReference(OpenApiReference reference, bool
580579
return null;
581580
}
582581

583-
return Workspace.ResolveReference<IOpenApiReferenceable>(reference.Id, reference.Type, Components)
584-
?? throw new OpenApiException(string.Format(Properties.SRResource.InvalidReferenceId, reference.Id));
582+
return Workspace.ResolveReference<IOpenApiReferenceable>(reference.Id, reference.Type, Components);
585583
}
586584

587585
/// <summary>

src/Microsoft.OpenApi/Services/OpenApiWorkspace.cs

Lines changed: 37 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System;
55
using System.Collections.Generic;
66
using System.IO;
7-
using System.Linq;
87
using Json.Schema;
98
using Microsoft.OpenApi.Exceptions;
109
using Microsoft.OpenApi.Extensions;
@@ -18,10 +17,10 @@ namespace Microsoft.OpenApi.Services
1817
/// </summary>
1918
public class OpenApiWorkspace
2019
{
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();
2524

2625
/// <summary>
2726
/// A list of OpenApiDocuments contained in the workspace
@@ -30,7 +29,7 @@ public IEnumerable<OpenApiDocument> Documents
3029
{
3130
get
3231
{
33-
return _documents.Values;
32+
return _documentsRegistry.Values;
3433
}
3534
}
3635

@@ -71,11 +70,6 @@ public OpenApiWorkspace()
7170
/// </summary>
7271
public OpenApiWorkspace(OpenApiWorkspace workspace) { }
7372

74-
/// <summary>
75-
///
76-
/// </summary>
77-
public IDictionary<Uri, OpenApiComponents> ComponentsRegistry { get; } = new Dictionary<Uri, OpenApiComponents>();
78-
7973
/// <summary>
8074
/// Verify if workspace contains a document based on its URL.
8175
/// </summary>
@@ -84,7 +78,7 @@ public OpenApiWorkspace(OpenApiWorkspace workspace) { }
8478
public bool Contains(string location)
8579
{
8680
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);
8882
}
8983

9084
/// <summary>
@@ -97,9 +91,9 @@ public void AddDocument(string location, OpenApiDocument document)
9791
document.Workspace = this;
9892
var locationUrl = ToLocationUrl(location);
9993

100-
if (!_documents.ContainsKey(locationUrl))
94+
if (!_documentsRegistry.ContainsKey(locationUrl))
10195
{
102-
_documents.Add(locationUrl, document);
96+
_documentsRegistry.Add(locationUrl, document);
10397
}
10498
}
10599

@@ -113,7 +107,7 @@ public void AddDocument(string location, OpenApiDocument document)
113107
/// </remarks>
114108
public void AddFragment(string location, IOpenApiReferenceable fragment)
115109
{
116-
_fragments.Add(ToLocationUrl(location), fragment);
110+
_fragmentsRegistry.Add(ToLocationUrl(location), fragment);
117111
}
118112

119113
/// <summary>
@@ -124,20 +118,20 @@ public void AddFragment(string location, IOpenApiReferenceable fragment)
124118
public void AddSchemaFragment(string location, JsonSchema fragment)
125119
{
126120
var locationUri = ToLocationUrl(location);
127-
if (!_schemaFragments.ContainsKey(locationUri))
121+
if (!_schemaFragmentsRegistry.ContainsKey(locationUri))
128122
{
129-
_schemaFragments.Add(locationUri, fragment);
123+
_schemaFragmentsRegistry.Add(locationUri, fragment);
130124
}
131125
}
132126

133127
/// <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.
135129
/// </summary>
136130
/// <param name="location"></param>
137131
/// <param name="artifact"></param>
138132
public void AddArtifact(string location, Stream artifact)
139133
{
140-
_artifacts.Add(ToLocationUrl(location), artifact);
134+
_artifactsRegistry.Add(ToLocationUrl(location), artifact);
141135
}
142136

143137
/// <summary>
@@ -149,21 +143,20 @@ public void AddArtifact(string location, Stream artifact)
149143
public T ResolveReference<T>(OpenApiReference reference)
150144
{
151145
var uri = new Uri(BaseUrl, reference.ExternalResource);
152-
if (_documents.TryGetValue(uri, out var doc))
146+
if (_documentsRegistry.TryGetValue(uri, out var doc))
153147
{
154148
return ResolveReference<T>(reference.Id, reference.Type, doc.Components);
155149
}
156-
else if (_fragments.TryGetValue(uri, out var fragment))
150+
else if (_fragmentsRegistry.TryGetValue(uri, out var fragment))
157151
{
158152
var jsonPointer = new JsonPointer($"/{reference.Id ?? string.Empty}");
159153
return (T)fragment.ResolveReference(jsonPointer);
160154
}
161-
else if (_schemaFragments.TryGetValue(uri, out var schemaFragment))
155+
else if (_schemaFragmentsRegistry.TryGetValue(uri, out var schemaFragment))
162156
{
163157
return (T)(schemaFragment as IBaseDocument);
164158
}
165159
return default;
166-
167160
}
168161

169162
/// <summary>
@@ -180,20 +173,27 @@ public T ResolveReference<T>(string referenceId, ReferenceType? referenceType, O
180173
if (string.IsNullOrEmpty(referenceId)) return default;
181174
if (components == null) return default;
182175

183-
return referenceType switch
176+
try
184177
{
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+
}
197197
}
198198

199199
/// <summary>
@@ -203,18 +203,12 @@ public T ResolveReference<T>(string referenceId, ReferenceType? referenceType, O
203203
/// <returns></returns>
204204
public Stream GetArtifact(string location)
205205
{
206-
return _artifacts[ToLocationUrl(location)];
206+
return _artifactsRegistry[ToLocationUrl(location)];
207207
}
208208

209209
private Uri ToLocationUrl(string location)
210210
{
211211
return new(BaseUrl, location);
212212
}
213-
214-
private static JsonSchema FetchSchemaFromRegistry(Uri reference)
215-
{
216-
var resolvedSchema = (JsonSchema)SchemaRegistry.Global.Get(reference);
217-
return resolvedSchema;
218-
}
219213
}
220214
}

0 commit comments

Comments
 (0)