Skip to content

Commit ba31015

Browse files
committed
Fixed based on code review
1 parent 219da17 commit ba31015

File tree

5 files changed

+36
-43
lines changed

5 files changed

+36
-43
lines changed

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

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ public MapNode CheckMapNode(string nodeName)
3232
if (mapNode == null)
3333
{
3434
throw new OpenApiException($"{nodeName} must be a map/object");
35-
//Diagnostic.Errors.Add(
36-
// new OpenApiError("", $"{nodeName} must be a map/object at " + Context.GetLocation()));
3735
}
3836

3937
return mapNode;
@@ -70,54 +68,50 @@ public static ParseNode Create(ParsingContext context, OpenApiDiagnostic diagnos
7068

7169
public virtual List<T> CreateList<T>(Func<MapNode, T> map)
7270
{
73-
throw new OpenApiException("Cannot create list");
71+
throw new OpenApiException("Cannot create list from this type of node.");
7472
}
7573

7674
public virtual Dictionary<string, T> CreateMap<T>(Func<MapNode, T> map)
7775
{
78-
throw new OpenApiException("Cannot create map");
76+
throw new OpenApiException("Cannot create map from this type of node.");
7977
}
8078

8179
public virtual Dictionary<string, T> CreateMapWithReference<T>(
8280
ReferenceType referenceType,
8381
Func<MapNode, T> map)
8482
where T : class, IOpenApiReferenceable
8583
{
86-
throw new OpenApiException("Cannot create map from reference");
84+
throw new OpenApiException("Cannot create map from this reference.");
8785
}
8886

8987
public virtual List<T> CreateSimpleList<T>(Func<ValueNode, T> map)
9088
{
91-
throw new OpenApiException("Cannot create simple list");
89+
throw new OpenApiException("Cannot create simple list from this type of node.");
9290
}
9391

9492
public virtual Dictionary<string, T> CreateSimpleMap<T>(Func<ValueNode, T> map)
9593
{
96-
throw new OpenApiException("Cannot create simple map");
94+
throw new OpenApiException("Cannot create simple map from this type of node.");
9795
}
9896

99-
/// <summary>
100-
/// Create a <see cref="IOpenApiAny"/>
101-
/// </summary>
102-
/// <returns></returns>
10397
public virtual IOpenApiAny CreateAny()
10498
{
105-
throw new NotSupportedException();
99+
throw new OpenApiException("Cannot create an Any object this type of node.");
106100
}
107101

108102
public virtual string GetRaw()
109103
{
110-
throw new OpenApiException("Cannot get raw value");
104+
throw new OpenApiException("Cannot get raw value from this type of node.");
111105
}
112106

113107
public virtual string GetScalarValue()
114108
{
115-
throw new OpenApiException("Cannot get scalar value");
109+
throw new OpenApiException("Cannot create a scalar value from this type of node.");
116110
}
117111

118112
public virtual List<IOpenApiAny> CreateListOfAny()
119113
{
120-
throw new OpenApiException("Cannot create list of any from here");
114+
throw new OpenApiException("Cannot create a list from this type of node.");
121115
}
122116

123117
}

src/Microsoft.OpenApi.Readers/Services/OpenApiReferenceResolver.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,15 +190,16 @@ private void ResolveTags(IList<OpenApiTag> tags)
190190
try
191191
{
192192
return _currentDocument.ResolveReference(reference) as T;
193-
} catch(OpenApiException ex)
193+
}
194+
catch (OpenApiException ex)
194195
{
195196
_errors.Add(new OpenApiError(ex));
196197
return null;
197198
}
198199
}
199200
else if (_resolveRemoteReferences == true)
200201
{
201-
// TODO: Resolve Remote reference
202+
// TODO: Resolve Remote reference (Targeted for 1.1 release)
202203
return new T()
203204
{
204205
UnresolvedReference = true,

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ internal static partial class OpenApiV2Deserializer
7272
o.Components.RequestBodies = n.CreateMapWithReference(ReferenceType.RequestBody, p =>
7373
{
7474
var parameter = LoadParameter(p, evenBody: true);
75-
if (parameter.In == null) {
75+
if (parameter.In == null)
76+
{
7677
return CreateRequestBody(n.Context,parameter);
7778
}
7879
return null;

src/Microsoft.OpenApi/Models/OpenApiDocument.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ public IOpenApiReferenceable ResolveReference(OpenApiReference reference)
307307
switch (reference.Type)
308308
{
309309
case ReferenceType.Schema:
310-
return this.Components.Schemas[reference.Id];
310+
return this.Components.Schemas[reference.Id];
311311

312312
case ReferenceType.Response:
313313
return this.Components.Responses[reference.Id];

test/Microsoft.OpenApi.SmokeTests/ApiGurus.cs

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
using Microsoft.OpenApi.Readers;
2-
using Newtonsoft.Json;
3-
using Newtonsoft.Json.Linq;
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
44
using System;
55
using System.Collections.Generic;
66
using System.Diagnostics;
77
using System.Net;
88
using System.Net.Http;
99
using System.Threading.Tasks;
10+
using Microsoft.OpenApi.Readers;
11+
using Newtonsoft.Json.Linq;
1012
using Xunit;
1113
using Xunit.Abstractions;
1214

@@ -15,28 +17,26 @@ namespace Microsoft.OpenApi.SmokeTests
1517
[Collection("DefaultSettings")]
1618
public class ApisGuruTests
1719
{
18-
private static HttpClient _httpClient = new HttpClient(new HttpClientHandler() {
19-
AutomaticDecompression = DecompressionMethods.GZip
20-
});
21-
22-
private readonly ITestOutputHelper output;
20+
private static HttpClient _httpClient;
21+
private readonly ITestOutputHelper _output;
2322

2423
public ApisGuruTests(ITestOutputHelper output)
2524
{
26-
27-
this.output = output;
25+
_output = output;
2826
}
2927

3028
static ApisGuruTests()
3129
{
30+
_httpClient = new HttpClient(new HttpClientHandler()
31+
{
32+
AutomaticDecompression = DecompressionMethods.GZip
33+
});
3234
_httpClient.DefaultRequestHeaders.AcceptEncoding.Add(new System.Net.Http.Headers.StringWithQualityHeaderValue("gzip"));
3335
_httpClient.DefaultRequestHeaders.UserAgent.Add(new System.Net.Http.Headers.ProductInfoHeaderValue("OpenApi.Net.Tests", "1.0"));
34-
3536
}
3637

3738
public static IEnumerable<object[]> GetSchemas()
3839
{
39-
4040
var listJsonStr = _httpClient
4141
.GetStringAsync("https://api.apis.guru/v2/list.json")
4242
.GetAwaiter().GetResult();
@@ -69,39 +69,36 @@ JToken GetProp(JToken obj, string prop)
6969
}
7070
}
7171

72-
7372
[Theory(DisplayName = "APIs.guru")]
7473
[MemberData(nameof(GetSchemas))]
7574
public async Task EnsureThatICouldParse(string url)
76-
{
77-
var stopwatch = new Stopwatch();
78-
75+
{
7976
var response = await _httpClient.GetAsync(url);
8077
if (!response.IsSuccessStatusCode)
8178
{
82-
output.WriteLine($"Couldn't load {url}");
79+
_output.WriteLine($"Couldn't load {url}");
8380
return;
8481
}
82+
8583
await response.Content.LoadIntoBufferAsync();
8684
var stream = await response.Content.ReadAsStreamAsync();
8785

86+
var stopwatch = new Stopwatch();
8887
stopwatch.Start();
8988

90-
var reader = new OpenApiStreamReader(new OpenApiReaderSettings() {
91-
});
89+
var reader = new OpenApiStreamReader();
9290
var openApiDocument = reader.Read(stream, out var diagnostic);
9391

9492
if (diagnostic.Errors.Count > 0)
9593
{
96-
output.WriteLine($"Errors parsing {url}");
97-
output.WriteLine(String.Join("\n", diagnostic.Errors));
94+
_output.WriteLine($"Errors parsing {url}");
95+
_output.WriteLine(String.Join("\n", diagnostic.Errors));
9896
// Assert.True(false); // Uncomment to identify descriptions with errors.
9997
}
10098

10199
Assert.NotNull(openApiDocument);
102100
stopwatch.Stop();
103-
output.WriteLine($"Parsing {url} took {stopwatch.ElapsedMilliseconds} ms.");
101+
_output.WriteLine($"Parsing {url} took {stopwatch.ElapsedMilliseconds} ms.");
104102
}
105-
106-
}
103+
}
107104
}

0 commit comments

Comments
 (0)