-
Notifications
You must be signed in to change notification settings - Fork 250
/
Copy pathOpenApiWorkspaceStreamTests.cs
99 lines (86 loc) · 3.37 KB
/
OpenApiWorkspaceStreamTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Reader;
using Xunit;
namespace Microsoft.OpenApi.Readers.Tests.OpenApiWorkspaceTests
{
public class OpenApiWorkspaceStreamTests
{
// Use OpenApiWorkspace to load a document and a referenced document
[Fact]
public async Task LoadingDocumentWithResolveAllReferencesShouldLoadDocumentIntoWorkspaceAsync()
{
// Create a reader that will resolve all references
var settings = new OpenApiReaderSettings
{
LoadExternalRefs = true,
CustomExternalLoader = new MockLoader(),
BaseUrl = new("file://c:\\")
};
settings.AddYamlReader();
var stream = new MemoryStream();
var doc = """
openapi: 3.0.0
info:
title: foo
version: 1.0.0
paths: {}
""";
var wr = new StreamWriter(stream);
await wr.WriteAsync(doc);
await wr.FlushAsync();
stream.Position = 0;
var result = await OpenApiDocument.LoadAsync(stream, OpenApiConstants.Yaml, settings: settings);
Assert.NotNull(result.Document.Workspace);
}
[Fact]
public async Task LoadDocumentWithExternalReferenceShouldLoadBothDocumentsIntoWorkspaceAsync()
{
// Create a reader that will resolve all references
var settings = new OpenApiReaderSettings
{
LoadExternalRefs = true,
CustomExternalLoader = new ResourceLoader(),
BaseUrl = new("file://c:\\"),
};
settings.AddYamlReader();
ReadResult result;
result = await OpenApiDocument.LoadAsync("V3Tests/Samples/OpenApiWorkspace/TodoMain.yaml", settings);
var externalDocBaseUri = result.Document.Workspace.GetDocumentId("./TodoComponents.yaml");
var schemasPath = "/components/schemas/";
var parametersPath = "/components/parameters/";
Assert.NotNull(externalDocBaseUri);
Assert.True(result.Document.Workspace.Contains(externalDocBaseUri + schemasPath + "todo"));
Assert.True(result.Document.Workspace.Contains(externalDocBaseUri + schemasPath + "entity"));
Assert.True(result.Document.Workspace.Contains(externalDocBaseUri + parametersPath + "filter"));
}
}
public class MockLoader : IStreamLoader
{
public Stream Load(Uri uri)
{
return null;
}
public Task<Stream> LoadAsync(Uri uri, CancellationToken cancellationToken = default)
{
return Task.FromResult<Stream>(null);
}
}
public class ResourceLoader : IStreamLoader
{
public Stream Load(Uri uri)
{
return null;
}
public Task<Stream> LoadAsync(Uri uri, CancellationToken cancellationToken = default)
{
var path = new Uri(new("http://example.org/V3Tests/Samples/OpenApiWorkspace/"), uri).AbsolutePath;
path = path[1..]; // remove leading slash
return Task.FromResult(Resources.GetStream(path));
}
}
}