-
Notifications
You must be signed in to change notification settings - Fork 275
Expand file tree
/
Copy pathOpenApiWorkspaceStreamTests.cs
More file actions
161 lines (140 loc) · 6.21 KB
/
OpenApiWorkspaceStreamTests.cs
File metadata and controls
161 lines (140 loc) · 6.21 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Models.Interfaces;
using Microsoft.OpenApi.Models.References;
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"));
}
[Fact]
public async Task LoadDocumentWithExternalReferencesInSubDirectories()
{
var sampleFolderPath = $"V3Tests/Samples/OpenApiWorkspace/ExternalReferencesInSubDirectories";
var referenceBaseUri = "file://" + Path.GetFullPath(sampleFolderPath);
// Create a reader that will resolve all references also of documentes located in the non-root directory
var settings = new OpenApiReaderSettings()
{
LoadExternalRefs = true,
BaseUrl = new Uri("file://")
};
settings.AddYamlReader();
// Act
var result = await OpenApiDocument.LoadAsync($"{sampleFolderPath}/Root.yaml", settings);
var document = result.Document;
var workspace = result.Document.Workspace;
// Assert
Assert.True(workspace.Contains($"{Path.Combine(referenceBaseUri, "Directory", "PetsPage.yaml")}#/components/schemas/PetsPage"));
Assert.True(workspace.Contains($"{Path.Combine(referenceBaseUri, "Directory", "Pets.yaml")}#/components/schemas/Pets"));
Assert.True(workspace.Contains($"{Path.Combine(referenceBaseUri, "Directory", "Pets.yaml")}#/components/schemas/Pet"));
var operationResponseSchema = document.Paths["/pets"].Operations[HttpMethod.Get].Responses["200"].Content["application/json"].Schema;
Assert.IsType<OpenApiSchemaReference>(operationResponseSchema);
var petsSchema = operationResponseSchema.Properties["pets"];
Assert.IsType<OpenApiSchemaReference>(petsSchema);
Assert.Equal(JsonSchemaType.Array, petsSchema.Type);
var petSchema = petsSchema.Items;
var petSchemaReference = Assert.IsType<OpenApiSchemaReference>(petSchema);
var petSchemaTarget = petSchemaReference.RecursiveTarget;
Assert.NotNull(petSchemaTarget);
Assert.Equivalent(new OpenApiSchema
{
Required = new HashSet<string> { "id", "name" },
Properties = new Dictionary<string, IOpenApiSchema>
{
["id"] = new OpenApiSchema
{
Type = JsonSchemaType.Integer,
Format = "int64"
},
["name"] = new OpenApiSchema
{
Type = JsonSchemaType.String
},
["tag"] = new OpenApiSchema
{
Type = JsonSchemaType.String
}
}
}, petSchemaTarget);
}
}
public class MockLoader : IStreamLoader
{
public Stream Load(Uri uri)
{
return null;
}
public Task<Stream> LoadAsync(Uri baseUrl, 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 baseUrl, 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));
}
}
}