Skip to content

Commit 0f1ee46

Browse files
committed
Added smoke tests
1 parent b68b226 commit 0f1ee46

File tree

7 files changed

+209
-19
lines changed

7 files changed

+209
-19
lines changed

Microsoft.OpenApi.sln

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 15
4-
VisualStudioVersion = 15.0.26730.3
4+
VisualStudioVersion = 15.0.27130.2027
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.OpenApi", "src\Microsoft.OpenApi\Microsoft.OpenApi.csproj", "{A8E50143-69B2-472A-9D45-3F9A05D13202}"
77
EndProject
@@ -15,14 +15,16 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.OpenApi.Workbench
1515
EndProject
1616
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.OpenApi.Readers", "src\Microsoft.OpenApi.Readers\Microsoft.OpenApi.Readers.csproj", "{79933258-0126-4382-8755-D50820ECC483}"
1717
EndProject
18-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.OpenApi.Tests", "test\Microsoft.OpenApi.Tests\Microsoft.OpenApi.Tests.csproj", "{AD83F991-DBF3-4251-8613-9CC54C826964}"
18+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.OpenApi.Tests", "test\Microsoft.OpenApi.Tests\Microsoft.OpenApi.Tests.csproj", "{AD83F991-DBF3-4251-8613-9CC54C826964}"
1919
EndProject
20-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.OpenApi.Readers.Tests", "test\Microsoft.OpenApi.Readers.Tests\Microsoft.OpenApi.Readers.Tests.csproj", "{1ED3C2C1-E1E7-4925-B4E6-2D969C3F5237}"
20+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.OpenApi.Readers.Tests", "test\Microsoft.OpenApi.Readers.Tests\Microsoft.OpenApi.Readers.Tests.csproj", "{1ED3C2C1-E1E7-4925-B4E6-2D969C3F5237}"
2121
EndProject
2222
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{E546B92F-20A8-49C3-8323-4B25BB78F3E1}"
2323
EndProject
2424
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{6357D7FD-2DE4-4900-ADB9-ABC37052040A}"
2525
EndProject
26+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.OpenApi.SmokeTests", "test\Microsoft.OpenApi.SmokeTests\Microsoft.OpenApi.SmokeTests.csproj", "{AD79B61D-88CF-497C-9ED5-41AE3867C5AC}"
27+
EndProject
2628
Global
2729
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2830
Debug|Any CPU = Debug|Any CPU
@@ -49,6 +51,10 @@ Global
4951
{1ED3C2C1-E1E7-4925-B4E6-2D969C3F5237}.Debug|Any CPU.Build.0 = Debug|Any CPU
5052
{1ED3C2C1-E1E7-4925-B4E6-2D969C3F5237}.Release|Any CPU.ActiveCfg = Release|Any CPU
5153
{1ED3C2C1-E1E7-4925-B4E6-2D969C3F5237}.Release|Any CPU.Build.0 = Release|Any CPU
54+
{AD79B61D-88CF-497C-9ED5-41AE3867C5AC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
55+
{AD79B61D-88CF-497C-9ED5-41AE3867C5AC}.Debug|Any CPU.Build.0 = Debug|Any CPU
56+
{AD79B61D-88CF-497C-9ED5-41AE3867C5AC}.Release|Any CPU.ActiveCfg = Release|Any CPU
57+
{AD79B61D-88CF-497C-9ED5-41AE3867C5AC}.Release|Any CPU.Build.0 = Release|Any CPU
5258
EndGlobalSection
5359
GlobalSection(SolutionProperties) = preSolution
5460
HideSolutionNode = FALSE
@@ -59,6 +65,7 @@ Global
5965
{79933258-0126-4382-8755-D50820ECC483} = {E546B92F-20A8-49C3-8323-4B25BB78F3E1}
6066
{AD83F991-DBF3-4251-8613-9CC54C826964} = {6357D7FD-2DE4-4900-ADB9-ABC37052040A}
6167
{1ED3C2C1-E1E7-4925-B4E6-2D969C3F5237} = {6357D7FD-2DE4-4900-ADB9-ABC37052040A}
68+
{AD79B61D-88CF-497C-9ED5-41AE3867C5AC} = {6357D7FD-2DE4-4900-ADB9-ABC37052040A}
6269
EndGlobalSection
6370
GlobalSection(ExtensibilityGlobals) = postSolution
6471
SolutionGuid = {9F171EFC-0DB5-4B10-ABFA-AF48D52CC565}

src/Microsoft.OpenApi.Readers/OpenApiStreamReader.cs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.IO;
66
using System.Linq;
7+
using Microsoft.OpenApi.Exceptions;
78
using Microsoft.OpenApi.Extensions;
89
using Microsoft.OpenApi.Models;
910
using Microsoft.OpenApi.Readers.Interface;
@@ -58,21 +59,31 @@ public OpenApiDocument Read(Stream input, out OpenApiDiagnostic diagnostic)
5859
ExtensionParsers = _settings.ExtensionParsers
5960
};
6061

61-
// Parse the OpenAPI Document
62-
var document = context.Parse(yamlDocument, diagnostic);
62+
OpenApiDocument document = null;
6363

64-
// Resolve References if requested
65-
switch (_settings.ReferenceResolution)
64+
try
65+
{
66+
// Parse the OpenAPI Document
67+
document = context.Parse(yamlDocument, diagnostic);
68+
69+
// Resolve References if requested
70+
switch (_settings.ReferenceResolution)
71+
{
72+
case ReferenceResolutionSetting.ResolveAllReferences:
73+
throw new ArgumentException(Properties.SRResource.CannotResolveRemoteReferencesSynchronously);
74+
case ReferenceResolutionSetting.ResolveLocalReferences:
75+
var resolver = new OpenApiReferenceResolver(document);
76+
var walker = new OpenApiWalker(resolver);
77+
walker.Walk(document);
78+
break;
79+
case ReferenceResolutionSetting.DoNotResolveReferences:
80+
break;
81+
}
82+
83+
}
84+
catch (OpenApiException ex)
6685
{
67-
case ReferenceResolutionSetting.ResolveAllReferences:
68-
throw new ArgumentException(Properties.SRResource.CannotResolveRemoteReferencesSynchronously);
69-
case ReferenceResolutionSetting.ResolveLocalReferences:
70-
var resolver = new OpenApiReferenceResolver(document);
71-
var walker = new OpenApiWalker(resolver);
72-
walker.Walk(document);
73-
break;
74-
case ReferenceResolutionSetting.DoNotResolveReferences:
75-
break;
86+
diagnostic.Errors.Add(new OpenApiError(ex));
7687
}
7788

7889
// Validate the document

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ public override Dictionary<string, T> CreateMapWithReference<T>(
9595
key = n.Key.GetScalarValue(),
9696
value = map(new MapNode(Context, Diagnostic, (YamlMappingNode)n.Value))
9797
};
98+
if (entry.value == null)
99+
{
100+
return null; // Body Parameters shouldn't be converted to Parameters
101+
}
98102
entry.value.Reference = new OpenApiReference()
99103
{
100104
Type = referenceType,
@@ -103,7 +107,7 @@ public override Dictionary<string, T> CreateMapWithReference<T>(
103107
return entry;
104108
}
105109
);
106-
return nodes.ToDictionary(k => k.key, v => v.value);
110+
return nodes.Where(n => n!= null).ToDictionary(k => k.key, v => v.value);
107111
}
108112

109113
public override Dictionary<string, T> CreateSimpleMap<T>(Func<ValueNode, T> map)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ internal static partial class OpenApiV2Deserializer
6565
o.Components.Parameters = n.CreateMapWithReference(
6666
ReferenceType.Parameter,
6767
LoadParameter);
68+
69+
//TODO: Convert reusable body parameters into Requestbody parameters
6870
}
6971
},
7072
{

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,32 @@ internal static partial class OpenApiV2Deserializer
5656
"authorizationUrl",
5757
(o, n) =>
5858
{
59+
var value = n.GetScalarValue();
60+
if (!String.IsNullOrEmpty(value))
61+
{
5962
_flow.AuthorizationUrl = new Uri(n.GetScalarValue());
63+
}
6064
}
6165
},
6266
{
6367
"tokenUrl",
6468
(o, n) =>
6569
{
66-
_flow.TokenUrl = new Uri(n.GetScalarValue());
70+
var value = n.GetScalarValue();
71+
if (!String.IsNullOrEmpty(value))
72+
{
73+
_flow.TokenUrl = new Uri(n.GetScalarValue());
74+
}
6775
}
6876
},
6977
{
7078
"scopes", (o, n) =>
7179
{
72-
_flow.Scopes = n.CreateSimpleMap(LoadString);
80+
var value = n.GetScalarValue();
81+
if (!String.IsNullOrEmpty(value))
82+
{
83+
_flow.Scopes = n.CreateSimpleMap(LoadString);
84+
}
7385
}
7486
}
7587
};
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
using Microsoft.OpenApi.Readers;
2+
using Newtonsoft.Json;
3+
using Newtonsoft.Json.Linq;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Diagnostics;
7+
using System.Net;
8+
using System.Net.Http;
9+
using System.Threading.Tasks;
10+
using Xunit;
11+
using Xunit.Abstractions;
12+
13+
namespace Microsoft.OpenApi.SmokeTests
14+
{
15+
[Collection("DefaultSettings")]
16+
public class ApisGuruTests
17+
{
18+
private static HttpClient _httpClient = new HttpClient(new HttpClientHandler() {
19+
AutomaticDecompression = DecompressionMethods.GZip
20+
});
21+
22+
private readonly ITestOutputHelper output;
23+
24+
public ApisGuruTests(ITestOutputHelper output)
25+
{
26+
27+
this.output = output;
28+
}
29+
30+
static ApisGuruTests()
31+
{
32+
_httpClient.DefaultRequestHeaders.AcceptEncoding.Add(new System.Net.Http.Headers.StringWithQualityHeaderValue("gzip"));
33+
_httpClient.DefaultRequestHeaders.UserAgent.Add(new System.Net.Http.Headers.ProductInfoHeaderValue("OpenApi.Net.Tests", "1.0"));
34+
35+
}
36+
37+
public static IEnumerable<object[]> GetSchemas()
38+
{
39+
40+
var listJsonStr = _httpClient
41+
.GetStringAsync("https://api.apis.guru/v2/list.json")
42+
.GetAwaiter().GetResult();
43+
44+
var json = JObject.Parse(listJsonStr);
45+
foreach (var item in json.Properties())
46+
{
47+
var versions = GetProp(item.Value, "versions") as JObject;
48+
if (versions == null)
49+
continue;
50+
foreach (var prop in versions.Properties())
51+
{
52+
var urlToJson = GetProp(prop.Value, "swaggerUrl")?.ToObject<string>();
53+
if (urlToJson != null)
54+
yield return new object[] { urlToJson };
55+
56+
var utlToYaml = GetProp(prop.Value, "swaggerYamlUrl")?.ToObject<string>();
57+
if (utlToYaml != null)
58+
yield return new object[] { utlToYaml };
59+
}
60+
}
61+
62+
JToken GetProp(JToken obj, string prop)
63+
{
64+
if (!(obj is JObject jObj))
65+
return null;
66+
if (!jObj.TryGetValue(prop, out var jToken))
67+
return null;
68+
return jToken;
69+
}
70+
}
71+
72+
[Theory(DisplayName = "APIs.guru")]
73+
[MemberData(nameof(GetSchemas))]
74+
public async Task EnsureThatICouldParse(string url)
75+
{
76+
var stopwatch = new Stopwatch();
77+
78+
var response = await _httpClient.GetAsync(url);
79+
if (!response.IsSuccessStatusCode)
80+
{
81+
output.WriteLine($"Couldn't load {url}");
82+
return;
83+
}
84+
await response.Content.LoadIntoBufferAsync();
85+
var stream = await response.Content.ReadAsStreamAsync();
86+
87+
stopwatch.Start();
88+
89+
var openApiDocument = new OpenApiStreamReader().Read(stream, out var diagnostic);
90+
91+
output.WriteLine(String.Join("\n", diagnostic.Errors));
92+
Assert.Equal(OpenApiSpecVersion.OpenApi2_0, diagnostic.SpecificationVersion);
93+
Assert.Equal(0, diagnostic.Errors.Count);
94+
95+
Assert.NotNull(openApiDocument);
96+
stopwatch.Stop();
97+
output.WriteLine($"Parsing {url} took {stopwatch.ElapsedMilliseconds} ms.");
98+
}
99+
100+
[Theory(DisplayName = "APIs.guru")]
101+
[MemberData(nameof(GetSchemas))]
102+
public async Task EnsureAllErrorsAreHandled(string url)
103+
{
104+
var stopwatch = new Stopwatch();
105+
106+
var response = await _httpClient.GetAsync(url);
107+
if (!response.IsSuccessStatusCode)
108+
{
109+
output.WriteLine($"Couldn't load {url}");
110+
return;
111+
}
112+
await response.Content.LoadIntoBufferAsync();
113+
var stream = await response.Content.ReadAsStreamAsync();
114+
115+
stopwatch.Start();
116+
117+
var openApiDocument = new OpenApiStreamReader().Read(stream, out var diagnostic);
118+
119+
output.WriteLine(String.Join("\n", diagnostic.Errors));
120+
Assert.Equal(OpenApiSpecVersion.OpenApi2_0, diagnostic.SpecificationVersion);
121+
122+
Assert.NotNull(openApiDocument);
123+
stopwatch.Stop();
124+
output.WriteLine($"Parsing {url} took {stopwatch.ElapsedMilliseconds} ms and has {diagnostic.Errors.Count} errors.");
125+
}
126+
}
127+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>net46;net461</TargetFrameworks>
5+
</PropertyGroup>
6+
7+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
8+
<DefineConstants>TRACE;DEBUG;net46</DefineConstants>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
13+
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
14+
<PackageReference Include="xunit" Version="2.3.1" />
15+
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<ProjectReference Include="..\..\src\Microsoft.OpenApi.Readers\Microsoft.OpenApi.Readers.csproj" />
20+
<ProjectReference Include="..\..\src\Microsoft.OpenApi\Microsoft.OpenApi.csproj" />
21+
</ItemGroup>
22+
23+
<ItemGroup>
24+
<Reference Include="System.Net.Http" />
25+
</ItemGroup>
26+
27+
</Project>

0 commit comments

Comments
 (0)