Skip to content

Commit 4cbd66d

Browse files
committed
code refactor
1 parent 989c6cf commit 4cbd66d

File tree

5 files changed

+43
-9
lines changed

5 files changed

+43
-9
lines changed

src/Microsoft.OpenApi/Services/OpenApiWorkspace.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,22 @@ namespace Microsoft.OpenApi.Services
1414
/// </summary>
1515
public class OpenApiWorkspace
1616
{
17+
private Dictionary<Uri, OpenApiDocument> _documents = new();
1718
private readonly Dictionary<string, Uri> _documentsIdRegistry = new();
1819
private readonly Dictionary<Uri, Stream> _artifactsRegistry = new();
1920
private readonly Dictionary<Uri, IOpenApiReferenceable> _IOpenApiReferenceableRegistry = new();
2021

22+
/// <summary>
23+
/// A list of OpenApiDocuments contained in the workspace
24+
/// </summary>
25+
public IEnumerable<OpenApiDocument> Documents
26+
{
27+
get
28+
{
29+
return _documents.Values;
30+
}
31+
}
32+
2133
/// <summary>
2234
/// The base location from where all relative references are resolved
2335
/// </summary>
@@ -96,6 +108,17 @@ public void AddDocumentId(string key, Uri value)
96108
}
97109
}
98110

111+
/// <summary>
112+
/// Add an OpenApiDocument to the workspace.
113+
/// </summary>
114+
/// <param name="location"></param>
115+
/// <param name="document"></param>
116+
public void AddDocument(string location, OpenApiDocument document)
117+
{
118+
document.Workspace = this;
119+
_documents.Add(ToLocationUrl(location), document);
120+
}
121+
99122
/// <summary>
100123
/// Retrieves the document id given a key.
101124
/// </summary>

test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiDocumentTests.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ public async Task ExternalDocumentDereferenceToOpenApiDocumentUsingJsonPointerWo
500500
};
501501

502502
// Act
503-
var result = await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "docWithExternalRef.yaml"), settings);
503+
var result = await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "externalRefByJsonPointer.yaml"), settings);
504504
var responseSchema = result.OpenApiDocument.Paths["/resource"].Operations[OperationType.Get].Responses["200"].Content["application/json"].Schema;
505505

506506
// Assert
@@ -521,10 +521,11 @@ public async Task ParseExternalDocumentDereferenceToOpenApiDocumentByIdWorks()
521521
};
522522

523523
// Act
524-
var result = await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "docWithExternalRef.yaml"), settings);
525-
var externalDoc = await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "externalResource.yaml"), settings);
524+
var result = await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "externalRefById.yaml"), settings);
525+
var doc2 = OpenApiDocument.Load(Path.Combine(SampleFolderPath, "externalResource.yaml")).OpenApiDocument;
526526

527527
var requestBodySchema = result.OpenApiDocument.Paths["/resource"].Operations[OperationType.Get].Parameters.First().Schema;
528+
result.OpenApiDocument.Workspace.RegisterComponents(doc2);
528529

529530
// Assert
530531
requestBodySchema.Properties.Count.Should().Be(2); // reference has been resolved
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
openapi: 3.1.0
2+
info:
3+
title: ReferenceById
4+
version: 1.0.0
5+
paths:
6+
/resource:
7+
get:
8+
parameters:
9+
- name: id
10+
in: query
11+
required: true
12+
schema:
13+
$ref: 'https://example.com/schemas/user.json'
14+
components: {}

test/Microsoft.OpenApi.Readers.Tests/V31Tests/Samples/OpenApiDocument/docWithExternalRef.yaml renamed to test/Microsoft.OpenApi.Readers.Tests/V31Tests/Samples/OpenApiDocument/externalRefByJsonPointer.yaml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,6 @@ info:
55
paths:
66
/resource:
77
get:
8-
parameters:
9-
- name: id
10-
in: query
11-
required: true
12-
schema:
13-
$ref: 'https://example.com/schemas/user.json'
148
responses:
159
'200':
1610
description: OK

test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1507,6 +1507,8 @@ namespace Microsoft.OpenApi.Services
15071507
public OpenApiWorkspace(Microsoft.OpenApi.Services.OpenApiWorkspace workspace) { }
15081508
public OpenApiWorkspace(System.Uri baseUrl) { }
15091509
public System.Uri BaseUrl { get; }
1510+
public System.Collections.Generic.IEnumerable<Microsoft.OpenApi.Models.OpenApiDocument> Documents { get; }
1511+
public void AddDocument(string location, Microsoft.OpenApi.Models.OpenApiDocument document) { }
15101512
public void AddDocumentId(string key, System.Uri value) { }
15111513
public int ComponentsCount() { }
15121514
public bool Contains(string location) { }

0 commit comments

Comments
 (0)