Skip to content

Commit af0b5fc

Browse files
committed
More workspace fixes
1 parent e5893b6 commit af0b5fc

File tree

11 files changed

+78
-11
lines changed

11 files changed

+78
-11
lines changed

src/Microsoft.OpenApi.Readers/OpenApiReaderError.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ public OpenApiReaderError(OpenApiException exception) : base(exception)
2727

2828
}
2929

30+
public OpenApiReaderError(ParsingContext context, string message) : base(context.GetLocation(), message)
31+
{
32+
33+
}
34+
35+
3036
/// <summary>
3137
/// Create error object from YAML SyntaxErrorException
3238
/// </summary>

src/Microsoft.OpenApi.Readers/ParseNodes/PropertyNode.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public void ParseField<T>(
7373
else
7474
{
7575
Diagnostic.Errors.Add(
76-
new OpenApiError("", $"{Name} is not a valid property at {Context.GetLocation()}"));
76+
new OpenApiReaderError(Context, $"{Name} is not a valid property"));
7777
}
7878
}
7979
}

src/Microsoft.OpenApi.Readers/Services/OpenApiRemoteReferenceCollector.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace Microsoft.OpenApi.Readers.Services
1313
internal class OpenApiRemoteReferenceCollector : OpenApiVisitorBase
1414
{
1515
private OpenApiDocument _document;
16-
private Dictionary<string, OpenApiReference> _references;
16+
private Dictionary<string, OpenApiReference> _references = new Dictionary<string, OpenApiReference>();
1717
public OpenApiRemoteReferenceCollector(OpenApiDocument document)
1818
{
1919
_document = document;

src/Microsoft.OpenApi.Readers/V2/OpenApiSecurityRequirementDeserializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public static OpenApiSecurityRequirement LoadSecurityRequirement(ParseNode node)
3434
else
3535
{
3636
node.Diagnostic.Errors.Add(
37-
new OpenApiError(node.Context.GetLocation(),
37+
new OpenApiReaderError(node.Context,
3838
$"Scheme {property.Name} is not found"));
3939
}
4040
}

src/Microsoft.OpenApi.Readers/V3/OpenApiSecurityRequirementDeserializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public static OpenApiSecurityRequirement LoadSecurityRequirement(ParseNode node)
3434
else
3535
{
3636
node.Diagnostic.Errors.Add(
37-
new OpenApiError(node.Context.GetLocation(), $"Scheme {property.Name} is not found"));
37+
new OpenApiReaderError(node.Context, $"Scheme {property.Name} is not found"));
3838
}
3939
}
4040

src/Microsoft.OpenApi/Extensions/OpenApiElementExtensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,5 @@ public static IEnumerable<OpenApiError> Validate(this IOpenApiElement element, V
2727
walker.Walk(element);
2828
return validator.Errors;
2929
}
30-
3130
}
3231
}

src/Microsoft.OpenApi/Models/OpenApiError.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Microsoft.OpenApi.Models
88
/// <summary>
99
/// Error related to the Open API Document.
1010
/// </summary>
11-
public class OpenApiError
11+
public abstract class OpenApiError
1212
{
1313
/// <summary>
1414
/// Initializes the <see cref="OpenApiError"/> class using the message and pointer from the given exception.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
using Microsoft.OpenApi.Exceptions;
10+
using Microsoft.OpenApi.Models;
11+
12+
namespace Microsoft.OpenApi.Services
13+
{
14+
public class OpenApiReferenceError : OpenApiError
15+
{
16+
private OpenApiReference _reference;
17+
/// <summary>
18+
/// Initializes the <see cref="OpenApiError"/> class using the message and pointer from the given exception.
19+
/// </summary>
20+
public OpenApiReferenceError(OpenApiException exception) : base(exception.Pointer, exception.Message)
21+
{
22+
}
23+
24+
public OpenApiReferenceError(OpenApiReference reference, string message) : base("", message)
25+
{
26+
_reference = reference;
27+
}
28+
}
29+
}

src/Microsoft.OpenApi/Services/OpenApiReferenceResolver.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,13 +217,27 @@ private void ResolveTags(IList<OpenApiTag> tags)
217217
}
218218
catch (OpenApiException ex)
219219
{
220-
_errors.Add(new OpenApiError(ex));
220+
_errors.Add(new OpenApiReferenceError(ex));
221221
return null;
222222
}
223223
}
224224
else if (_resolveRemoteReferences == true)
225225
{
226-
return _currentDocument.Workspace.ResolveReference(reference) as T;
226+
if (_currentDocument.Workspace == null)
227+
{
228+
_errors.Add(new OpenApiReferenceError(reference,"Cannot resolve external references for documents not in workspaces."));
229+
// Leave as unresolved reference
230+
return new T()
231+
{
232+
UnresolvedReference = true,
233+
Reference = reference
234+
};
235+
}
236+
var target = _currentDocument.Workspace.ResolveReference(reference);
237+
238+
// TODO: If it is a document fragment, then we should resolve it within the current context
239+
240+
return target as T;
227241
}
228242
else
229243
{

src/Microsoft.OpenApi/Services/OpenApiWorkspace.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@ public IEnumerable<OpenApiDocument> Documents {
3535

3636
public bool Contains(string location)
3737
{
38-
return true;
38+
return _documents.ContainsKey(location);
3939
}
4040

4141
public void AddDocument(string location, OpenApiDocument document)
4242
{
43+
document.Workspace = this;
4344
_documents.Add(location, document);
4445
}
4546

@@ -48,6 +49,11 @@ public void AddFragment(string location, IOpenApiFragment fragment)
4849

4950
}
5051

52+
public void AddArtifact<T>(string location, T artifact)
53+
{
54+
55+
}
56+
5157
public IOpenApiReferenceable ResolveReference(OpenApiReference reference)
5258
{
5359
if (!_documents.TryGetValue(reference.ExternalResource,out var doc))

0 commit comments

Comments
 (0)