Skip to content

Commit db2e5c2

Browse files
authored
Merge branch 'master' into dm/context-refactor
2 parents 3529d07 + 9a0a57e commit db2e5c2

15 files changed

+235
-186
lines changed

test/Microsoft.OpenApi.Readers.Tests/Microsoft.OpenApi.Readers.Tests.csproj

Lines changed: 129 additions & 130 deletions
Large diffs are not rendered by default.

test/Microsoft.OpenApi.Readers.Tests/ReferenceService/TryLoadReferenceV2Tests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace Microsoft.OpenApi.Readers.Tests.ReferenceService
1515
[Collection("DefaultSettings")]
1616
public class TryLoadReferenceV2Tests
1717
{
18-
private const string SampleFolderPath = "ReferenceService/Samples";
18+
private const string SampleFolderPath = "ReferenceService/Samples/";
1919

2020
[Fact]
2121
public void LoadSchemaReference()
@@ -24,7 +24,7 @@ public void LoadSchemaReference()
2424
var context = new ParsingContext();
2525
var diagnostic = new OpenApiDiagnostic();
2626

27-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "multipleReferences.v2.yaml")))
27+
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "multipleReferences.v2.yaml")))
2828
{
2929
var yamlStream = new YamlStream();
3030
yamlStream.Load(new StreamReader(stream));
@@ -80,7 +80,7 @@ public void LoadParameterReference()
8080
var context = new ParsingContext();
8181
var diagnostic = new OpenApiDiagnostic();
8282

83-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "multipleReferences.v2.yaml")))
83+
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "multipleReferences.v2.yaml")))
8484
{
8585
var yamlStream = new YamlStream();
8686
yamlStream.Load(new StreamReader(stream));
@@ -124,7 +124,7 @@ public void LoadSecuritySchemeReference()
124124
var context = new ParsingContext();
125125
var diagnostic = new OpenApiDiagnostic();
126126

127-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "multipleReferences.v2.yaml")))
127+
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "multipleReferences.v2.yaml")))
128128
{
129129
var yamlStream = new YamlStream();
130130
yamlStream.Load(new StreamReader(stream));
@@ -161,7 +161,7 @@ public void LoadResponseReference()
161161
var context = new ParsingContext();
162162
var diagnostic = new OpenApiDiagnostic();
163163

164-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "multipleReferences.v2.yaml")))
164+
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "multipleReferences.v2.yaml")))
165165
{
166166
var yamlStream = new YamlStream();
167167
yamlStream.Load(new StreamReader(stream));
@@ -197,7 +197,7 @@ public void LoadResponseAndSchemaReference()
197197
var context = new ParsingContext();
198198
var diagnostic = new OpenApiDiagnostic();
199199

200-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "multipleReferences.v2.yaml")))
200+
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "multipleReferences.v2.yaml")))
201201
{
202202
var yamlStream = new YamlStream();
203203
yamlStream.Load(new StreamReader(stream));
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
using System.IO;
5+
6+
namespace Microsoft.OpenApi.Readers.Tests
7+
{
8+
internal static class Resources
9+
{
10+
/// <summary>
11+
/// Get the file contents.
12+
/// </summary>
13+
/// <param name="fileName">The file name with relative path. For example: "/V3Tests/Samples/OpenApiCallback/..".</param>
14+
/// <returns>The file contents.</returns>
15+
public static string GetString(string fileName)
16+
{
17+
using (Stream stream = GetStream(fileName))
18+
using (TextReader reader = new StreamReader(stream))
19+
{
20+
return reader.ReadToEnd();
21+
}
22+
}
23+
24+
/// <summary>
25+
/// Get the file stream.
26+
/// </summary>
27+
/// <param name="fileName">The file name with relative path. For example: "/V3Tests/Samples/OpenApiCallback/..".</param>
28+
/// <returns>The file stream.</returns>
29+
public static Stream GetStream(string fileName)
30+
{
31+
string path = GetPath(fileName);
32+
Stream stream = typeof(Resources).Assembly.GetManifestResourceStream(path);
33+
34+
if (stream == null)
35+
{
36+
string message = Error.Format("The embedded resource '{0}' was not found.", path);
37+
throw new FileNotFoundException(message, path);
38+
}
39+
40+
return stream;
41+
}
42+
43+
private static string GetPath(string fileName)
44+
{
45+
const string pathSeparator = ".";
46+
return typeof(Resources).Namespace + pathSeparator + fileName.Replace('/', '.');
47+
}
48+
}
49+
}

test/Microsoft.OpenApi.Readers.Tests/V2Tests/ComparisonTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ public class ComparisonTests
1818
//[InlineData("definitions")] //Currently broken due to V3 references not behaving the same as V2
1919
public void EquivalentV2AndV3DocumentsShouldProductEquivalentObjects(string fileName)
2020
{
21-
using (var streamV2 = File.OpenRead(Path.Combine(SampleFolderPath, $"{fileName}.v2.yaml")))
22-
using (var streamV3 = File.OpenRead(Path.Combine(SampleFolderPath, $"{fileName}.v3.yaml")))
21+
using (var streamV2 = Resources.GetStream(Path.Combine(SampleFolderPath, $"{fileName}.v2.yaml")))
22+
using (var streamV3 = Resources.GetStream(Path.Combine(SampleFolderPath, $"{fileName}.v3.yaml")))
2323
{
2424
var openApiDocV2 = new OpenApiStreamReader().Read(streamV2, out var contextV2);
2525
var openApiDocV3 = new OpenApiStreamReader().Read(streamV3, out var contextV3);

test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiParameterTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ namespace Microsoft.OpenApi.Readers.Tests.V2Tests
1313
[Collection("DefaultSettings")]
1414
public class OpenApiParameterTests
1515
{
16-
private const string SampleFolderPath = "V2Tests/Samples/OpenApiParameter";
16+
private const string SampleFolderPath = "V2Tests/Samples/OpenApiParameter/";
1717

1818
[Fact]
1919
public void ParseBodyParameterShouldSucceed()
2020
{
2121
// Arrange
2222
MapNode node;
23-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "bodyParameter.yaml")))
23+
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "bodyParameter.yaml")))
2424
{
2525
node = TestHelper.CreateYamlMapNode(stream);
2626
}
@@ -39,7 +39,7 @@ public void ParsePathParameterShouldSucceed()
3939
{
4040
// Arrange
4141
MapNode node;
42-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "pathParameter.yaml")))
42+
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "pathParameter.yaml")))
4343
{
4444
node = TestHelper.CreateYamlMapNode(stream);
4545
}
@@ -67,7 +67,7 @@ public void ParseQueryParameterShouldSucceed()
6767
{
6868
// Arrange
6969
MapNode node;
70-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "queryParameter.yaml")))
70+
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "queryParameter.yaml")))
7171
{
7272
node = TestHelper.CreateYamlMapNode(stream);
7373
}
@@ -101,7 +101,7 @@ public void ParseFormDataParameterShouldSucceed()
101101
{
102102
// Arrange
103103
MapNode node;
104-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "formDataParameter.yaml")))
104+
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "formDataParameter.yaml")))
105105
{
106106
node = TestHelper.CreateYamlMapNode(stream);
107107
}
@@ -120,7 +120,7 @@ public void ParseHeaderParameterShouldSucceed()
120120
{
121121
// Arrange
122122
MapNode node;
123-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "headerParameter.yaml")))
123+
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "headerParameter.yaml")))
124124
{
125125
node = TestHelper.CreateYamlMapNode(stream);
126126
}

test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiSecuritySchemeTests.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ namespace Microsoft.OpenApi.Readers.Tests.V2Tests
1616
[Collection("DefaultSettings")]
1717
public class OpenApiSecuritySchemeTests
1818
{
19-
private const string SampleFolderPath = "V2Tests/Samples/OpenApiSecurityScheme";
19+
private const string SampleFolderPath = "V2Tests/Samples/OpenApiSecurityScheme/";
2020

2121
[Fact]
2222
public void ParseHttpSecuritySchemeShouldSucceed()
2323
{
24-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "basicSecurityScheme.yaml")))
24+
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "basicSecurityScheme.yaml")))
2525
{
2626
var document = OpenApiStreamReader.LoadYamlDocument(stream);
2727

@@ -46,7 +46,7 @@ public void ParseHttpSecuritySchemeShouldSucceed()
4646
[Fact]
4747
public void ParseApiKeySecuritySchemeShouldSucceed()
4848
{
49-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "apiKeySecurityScheme.yaml")))
49+
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "apiKeySecurityScheme.yaml")))
5050
{
5151
var document = OpenApiStreamReader.LoadYamlDocument(stream);
5252
var context = new ParsingContext();
@@ -71,7 +71,7 @@ public void ParseApiKeySecuritySchemeShouldSucceed()
7171
[Fact]
7272
public void ParseOAuth2ImplicitSecuritySchemeShouldSucceed()
7373
{
74-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "oAuth2ImplicitSecurityScheme.yaml")))
74+
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "oauth2ImplicitSecurityScheme.yaml")))
7575
{
7676
var document = OpenApiStreamReader.LoadYamlDocument(stream);
7777
var context = new ParsingContext();
@@ -106,7 +106,7 @@ public void ParseOAuth2ImplicitSecuritySchemeShouldSucceed()
106106
[Fact]
107107
public void ParseOAuth2PasswordSecuritySchemeShouldSucceed()
108108
{
109-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "oAuth2PasswordSecurityScheme.yaml")))
109+
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "oauth2PasswordSecurityScheme.yaml")))
110110
{
111111
var document = OpenApiStreamReader.LoadYamlDocument(stream);
112112
var context = new ParsingContext();
@@ -141,7 +141,7 @@ public void ParseOAuth2PasswordSecuritySchemeShouldSucceed()
141141
[Fact]
142142
public void ParseOAuth2ApplicationSecuritySchemeShouldSucceed()
143143
{
144-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "oAuth2ApplicationSecurityScheme.yaml")))
144+
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "oauth2ApplicationSecurityScheme.yaml")))
145145
{
146146
var document = OpenApiStreamReader.LoadYamlDocument(stream);
147147
var context = new ParsingContext();
@@ -176,7 +176,7 @@ public void ParseOAuth2ApplicationSecuritySchemeShouldSucceed()
176176
[Fact]
177177
public void ParseOAuth2AccessCodeSecuritySchemeShouldSucceed()
178178
{
179-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "oAuth2AccessCodeSecurityScheme.yaml")))
179+
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "oauth2AccessCodeSecurityScheme.yaml")))
180180
{
181181
var document = OpenApiStreamReader.LoadYamlDocument(stream);
182182

test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiCallbackTests.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ namespace Microsoft.OpenApi.Readers.Tests.V3Tests
1616
[Collection("DefaultSettings")]
1717
public class OpenApiCallbackTests
1818
{
19-
private const string SampleFolderPath = "V3Tests/Samples/OpenApiCallback";
19+
private const string SampleFolderPath = "V3Tests/Samples/OpenApiCallback/";
2020

2121
[Fact]
2222
public void ParseBasicCallbackShouldSucceed()
2323
{
24-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "basicCallback.yaml")))
24+
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "basicCallback.yaml")))
2525
{
26+
// Arrange
2627
var yamlStream = new YamlStream();
2728
yamlStream.Load(new StreamReader(stream));
2829
var yamlNode = yamlStream.Documents.First().RootNode;
@@ -76,7 +77,7 @@ public void ParseBasicCallbackShouldSucceed()
7677
[Fact]
7778
public void ParseAdvancedCallbackWithReferenceShouldSucceed()
7879
{
79-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "advancedCallbackWithReference.yaml")))
80+
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "advancedCallbackWithReference.yaml")))
8081
{
8182
// Act
8283
var openApiDoc = new OpenApiStreamReader().Read(stream, out var context);

test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiDiscriminatorTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ namespace Microsoft.OpenApi.Readers.Tests.V3Tests
1515
[Collection("DefaultSettings")]
1616
public class OpenApiDiscriminatorTests
1717
{
18-
private const string SampleFolderPath = "V3Tests/Samples/OpenApiDiscriminator";
18+
private const string SampleFolderPath = "V3Tests/Samples/OpenApiDiscriminator/";
1919

2020
[Fact]
2121
public void ParseBasicDiscriminatorShouldSucceed()
2222
{
23-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "basicDiscriminator.yaml")))
23+
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "basicDiscriminator.yaml")))
2424
{
2525
var yamlStream = new YamlStream();
2626
yamlStream.Load(new StreamReader(stream));

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public void ParseDocumentFromInlineStringShouldSucceed()
5353
[Fact]
5454
public void ParseBasicDocumentWithMultipleServersShouldSucceed()
5555
{
56-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "basicDocumentWithMultipleServers.yaml")))
56+
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "basicDocumentWithMultipleServers.yaml")))
5757
{
5858
var openApiDoc = new OpenApiStreamReader().Read(stream, out var context);
5959

@@ -87,7 +87,7 @@ public void ParseBasicDocumentWithMultipleServersShouldSucceed()
8787
[Fact]
8888
public void ParseBrokenMinimalDocumentShouldYieldExpectedDiagnostic()
8989
{
90-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "brokenMinimalDocument.yaml")))
90+
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "brokenMinimalDocument.yaml")))
9191
{
9292
var openApiDoc = new OpenApiStreamReader().Read(stream, out var context);
9393

@@ -114,7 +114,7 @@ public void ParseBrokenMinimalDocumentShouldYieldExpectedDiagnostic()
114114
[Fact]
115115
public void ParseMinimalDocumentShouldSucceed()
116116
{
117-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "minimalDocument.yaml")))
117+
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "minimalDocument.yaml")))
118118
{
119119
var openApiDoc = new OpenApiStreamReader().Read(stream, out var context);
120120

@@ -137,7 +137,7 @@ public void ParseMinimalDocumentShouldSucceed()
137137
public void ParseStandardPetStoreDocumentShouldSucceed()
138138
{
139139
OpenApiDiagnostic context;
140-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "petStore.yaml")))
140+
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "petStore.yaml")))
141141
{
142142
var actual = new OpenApiStreamReader().Read(stream, out context);
143143

@@ -548,7 +548,7 @@ public void ParseStandardPetStoreDocumentShouldSucceed()
548548
public void ParseModifiedPetStoreDocumentWithTagAndSecurityShouldSucceed()
549549
{
550550
OpenApiDiagnostic context;
551-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "petStoreWithTagAndSecurity.yaml")))
551+
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "petStoreWithTagAndSecurity.yaml")))
552552
{
553553
var actual = new OpenApiStreamReader().Read(stream, out context);
554554

@@ -1050,7 +1050,7 @@ public void ParsePetStoreExpandedShouldSucceed()
10501050
{
10511051
OpenApiDiagnostic context;
10521052

1053-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "petStoreExpanded.yaml")))
1053+
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "petStoreExpanded.yaml")))
10541054
{
10551055
var actual = new OpenApiStreamReader().Read(stream, out context);
10561056

test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiEncodingTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ namespace Microsoft.OpenApi.Readers.Tests.V3Tests
1515
[Collection("DefaultSettings")]
1616
public class OpenApiEncodingTests
1717
{
18-
private const string SampleFolderPath = "V3Tests/Samples/OpenApiEncoding";
18+
private const string SampleFolderPath = "V3Tests/Samples/OpenApiEncoding/";
1919

2020
[Fact]
2121
public void ParseBasicEncodingShouldSucceed()
2222
{
23-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "basicEncoding.yaml")))
23+
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "basicEncoding.yaml")))
2424
{
2525
var yamlStream = new YamlStream();
2626
yamlStream.Load(new StreamReader(stream));
@@ -46,7 +46,7 @@ public void ParseBasicEncodingShouldSucceed()
4646
[Fact]
4747
public void ParseAdvancedEncodingShouldSucceed()
4848
{
49-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "advancedEncoding.yaml")))
49+
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "advancedEncoding.yaml")))
5050
{
5151
var yamlStream = new YamlStream();
5252
yamlStream.Load(new StreamReader(stream));

0 commit comments

Comments
 (0)