Skip to content

Commit 3724eeb

Browse files
committed
Implement PR feedback
1 parent 70768e1 commit 3724eeb

File tree

5 files changed

+30
-120
lines changed

5 files changed

+30
-120
lines changed

src/Microsoft.OpenApi.Readers/OpenApiYamlReader.cs

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public OpenApiDocument Parse(string input, out OpenApiDiagnostic diagnostic, Ope
3131
/// <inheritdoc/>
3232
public OpenApiDocument Read(string url, out OpenApiDiagnostic diagnostic, OpenApiReaderSettings settings = null)
3333
{
34-
var stream = GetStream(url);
34+
var stream = GetStream(url).GetAwaiter().GetResult();
3535
return Read(stream, out diagnostic, settings);
3636
}
3737

@@ -50,39 +50,7 @@ public OpenApiDocument Read(TextReader input, out OpenApiDiagnostic diagnostic,
5050
/// <inheritdoc/>
5151
public async Task<ReadResult> ReadAsync(string url, OpenApiReaderSettings settings = null, CancellationToken cancellationToken = default)
5252
{
53-
Stream stream;
54-
if (url.StartsWith("http", StringComparison.OrdinalIgnoreCase) || url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
55-
{
56-
try
57-
{
58-
stream = await _httpClient.GetStreamAsync(new Uri(url));
59-
}
60-
catch (HttpRequestException ex)
61-
{
62-
throw new InvalidOperationException($"Could not download the file at {url}", ex);
63-
}
64-
}
65-
else
66-
{
67-
try
68-
{
69-
var fileInput = new FileInfo(url);
70-
stream = fileInput.OpenRead();
71-
}
72-
catch (Exception ex) when (
73-
ex is
74-
FileNotFoundException or
75-
PathTooLongException or
76-
DirectoryNotFoundException or
77-
IOException or
78-
UnauthorizedAccessException or
79-
SecurityException or
80-
NotSupportedException)
81-
{
82-
throw new InvalidOperationException($"Could not open the file at {url}", ex);
83-
}
84-
}
85-
53+
var stream = GetStream(url).Result;
8654
return await ReadAsync(stream, settings, cancellationToken);
8755
}
8856

@@ -116,7 +84,7 @@ public T Read<T>(string url,
11684
OpenApiReaderSettings settings = null) where T : IOpenApiElement
11785
{
11886
settings ??= new OpenApiReaderSettings();
119-
var stream = GetStream(url);
87+
var stream = GetStream(url).GetAwaiter().GetResult();
12088
return Read<T>(stream, version, out diagnostic, settings);
12189
}
12290

@@ -165,14 +133,14 @@ public T Parse<T>(string input,
165133
return Read<T>(reader, version, out diagnostic, settings);
166134
}
167135

168-
private Stream GetStream(string url)
136+
private async Task<Stream> GetStream(string url)
169137
{
170138
Stream stream;
171139
if (url.StartsWith("http", StringComparison.OrdinalIgnoreCase) || url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
172140
{
173141
try
174142
{
175-
stream = _httpClient.GetStreamAsync(new Uri(url)).GetAwaiter().GetResult();
143+
stream = await _httpClient.GetStreamAsync(new Uri(url));
176144
}
177145
catch (HttpRequestException ex)
178146
{

src/Microsoft.OpenApi/Interfaces/IOpenApiReader.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the MIT license.
33

44
using System.IO;
5-
using System.Text.Json.Nodes;
65
using System.Threading;
76
using System.Threading.Tasks;
87
using Microsoft.OpenApi.Models;
@@ -82,10 +81,10 @@ public interface IOpenApiReader
8281
/// Reads the input string and parses it into an Open API document.
8382
/// </summary>
8483
/// <typeparam name="T"></typeparam>
85-
/// <param name="input"></param>
86-
/// <param name="version"></param>
87-
/// <param name="diagnostic"></param>
88-
/// <param name="settings"></param>
84+
/// <param name="input">Stream containing OpenAPI description to parse.</param>
85+
/// <param name="version">Version of the OpenAPI specification that the fragment conforms to.</param>
86+
/// <param name="diagnostic">Returns diagnostic object containing errors detected during parsing</param>
87+
/// <param name="settings">The OpenApiReader settings.</param>
8988
/// <returns></returns>
9089
T Parse<T>(string input, OpenApiSpecVersion version, out OpenApiDiagnostic diagnostic, OpenApiReaderSettings settings = null) where T : IOpenApiElement;
9190

@@ -110,7 +109,7 @@ public interface IOpenApiReader
110109
T Read<T>(TextReader input, OpenApiSpecVersion version, out OpenApiDiagnostic diagnostic, OpenApiReaderSettings settings = null) where T : IOpenApiElement;
111110

112111
/// <summary>
113-
/// Reads the stream input and parses the fragment of an OpenAPI description into an Open API Element.
112+
/// Reads the string input and parses the fragment of an OpenAPI description into an Open API Element.
114113
/// </summary>
115114
/// <param name="url">Url pointing to the document.</param>
116115
/// <param name="version">Version of the OpenAPI specification that the fragment conforms to.</param>

src/Microsoft.OpenApi/Models/OpenApiModelFactory.cs

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
using System;
55
using System.IO;
6+
using System.Linq;
67
using System.Net.Http;
7-
using System.Runtime;
88
using System.Threading.Tasks;
99
using Microsoft.OpenApi.Interfaces;
1010
using Microsoft.OpenApi.Reader;
@@ -167,16 +167,14 @@ public static T Load<T>(TextReader input, OpenApiSpecVersion version, out OpenAp
167167

168168
private static string GetContentType(string url)
169169
{
170-
var response = _httpClient.GetAsync(url).GetAwaiter().GetResult();
171-
var contentType = response.Content.Headers.ContentType.MediaType;
172-
if (contentType.EndsWith(OpenApiConstants.Json, StringComparison.OrdinalIgnoreCase))
173-
{
174-
return OpenApiConstants.Json;
175-
}
176-
else if (contentType.EndsWith(OpenApiConstants.Yaml, StringComparison.OrdinalIgnoreCase))
170+
if (!string.IsNullOrEmpty(url))
177171
{
178-
return OpenApiConstants.Yaml;
172+
var response = _httpClient.GetAsync(url).GetAwaiter().GetResult();
173+
var mediaType = response.Content.Headers.ContentType.MediaType;
174+
var contentType = mediaType.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).First();
175+
return contentType.Split('/').LastOrDefault();
179176
}
177+
180178
return null;
181179
}
182180

@@ -185,34 +183,16 @@ private static string GetFormat(string url)
185183
if (!string.IsNullOrEmpty(url))
186184
{
187185
if (url.StartsWith("http", StringComparison.OrdinalIgnoreCase) || url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
188-
{
189-
if (url.EndsWith(OpenApiConstants.Json, StringComparison.OrdinalIgnoreCase)
190-
|| GetContentType(url).Equals(OpenApiConstants.Json, StringComparison.OrdinalIgnoreCase))
191-
{
192-
return OpenApiConstants.Json;
193-
}
194-
else if (url.EndsWith(OpenApiConstants.Yaml, StringComparison.OrdinalIgnoreCase)
195-
|| url.EndsWith(OpenApiConstants.Yml, StringComparison.OrdinalIgnoreCase)
196-
|| GetContentType(url).Equals(OpenApiConstants.Yml, StringComparison.OrdinalIgnoreCase))
197-
{
198-
return OpenApiConstants.Yaml;
199-
}
186+
{
187+
// URL examples ---> https://example.com/path/to/file.json, https://example.com/path/to/file.yaml
188+
var path = new Uri(url);
189+
var urlSuffix = path.Segments[path.Segments.Length - 1].Split('.').LastOrDefault();
190+
191+
return !string.IsNullOrEmpty(urlSuffix) ? urlSuffix : GetContentType(url);
200192
}
201193
else
202194
{
203-
if (url.EndsWith(OpenApiConstants.Json, StringComparison.OrdinalIgnoreCase))
204-
{
205-
return OpenApiConstants.Json;
206-
}
207-
else if (url.EndsWith(OpenApiConstants.Yaml, StringComparison.OrdinalIgnoreCase)
208-
|| url.EndsWith(OpenApiConstants.Yml, StringComparison.OrdinalIgnoreCase))
209-
{
210-
return OpenApiConstants.Yaml;
211-
}
212-
else
213-
{
214-
throw new ArgumentException("Unsupported file format");
215-
}
195+
return Path.GetExtension(url).Split('.').LastOrDefault();
216196
}
217197
}
218198
return null;

src/Microsoft.OpenApi/Reader/OpenApiJsonReader.cs

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ public class OpenApiJsonReader : IOpenApiReader
3838
/// <exception cref="ArgumentException"></exception>
3939
public OpenApiDocument Read(string url, out OpenApiDiagnostic diagnostic, OpenApiReaderSettings settings = null)
4040
{
41-
settings ??= new OpenApiReaderSettings();
42-
var stream = GetStream(url);
41+
var stream = GetStream(url).GetAwaiter().GetResult();
4342
return Read(stream, out diagnostic, settings);
4443
}
4544

@@ -100,40 +99,7 @@ public OpenApiDocument Read(TextReader input, out OpenApiDiagnostic diagnostic,
10099
/// <exception cref="ArgumentException"></exception>
101100
public async Task<ReadResult> ReadAsync(string url, OpenApiReaderSettings settings = null, CancellationToken cancellationToken = default)
102101
{
103-
settings ??= new OpenApiReaderSettings();
104-
Stream stream;
105-
if (url.StartsWith("http", StringComparison.OrdinalIgnoreCase) || url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
106-
{
107-
try
108-
{
109-
stream = await _httpClient.GetStreamAsync(new Uri(url));
110-
}
111-
catch (HttpRequestException ex)
112-
{
113-
throw new InvalidOperationException($"Could not download the file at {url}", ex);
114-
}
115-
}
116-
else
117-
{
118-
try
119-
{
120-
var fileInput = new FileInfo(url);
121-
stream = fileInput.OpenRead();
122-
}
123-
catch (Exception ex) when (
124-
ex is
125-
FileNotFoundException or
126-
PathTooLongException or
127-
DirectoryNotFoundException or
128-
IOException or
129-
UnauthorizedAccessException or
130-
SecurityException or
131-
NotSupportedException)
132-
{
133-
throw new InvalidOperationException($"Could not open the file at {url}", ex);
134-
}
135-
}
136-
102+
var stream = await GetStream(url);
137103
return await ReadAsync(stream, settings, cancellationToken);
138104
}
139105

@@ -246,7 +212,7 @@ public T Read<T>(string url,
246212
OpenApiReaderSettings settings = null) where T : IOpenApiElement
247213
{
248214
settings ??= new OpenApiReaderSettings();
249-
var stream = GetStream(url);
215+
var stream = GetStream(url).GetAwaiter().GetResult();
250216
return Read<T>(stream, version, out diagnostic, settings);
251217
}
252218

@@ -453,14 +419,14 @@ private async Task LoadExternalRefs(OpenApiDocument document, CancellationToken
453419
await workspaceLoader.LoadAsync(new OpenApiReference() { ExternalResource = "/" }, document, OpenApiConstants.Json, null, cancellationToken);
454420
}
455421

456-
private Stream GetStream(string url)
422+
private async Task<Stream> GetStream(string url)
457423
{
458424
Stream stream;
459425
if (url.StartsWith("http", StringComparison.OrdinalIgnoreCase) || url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
460426
{
461427
try
462428
{
463-
stream = _httpClient.GetStreamAsync(new Uri(url)).GetAwaiter().GetResult();
429+
stream = await _httpClient.GetStreamAsync(new Uri(url));
464430
}
465431
catch (HttpRequestException ex)
466432
{

src/Microsoft.OpenApi/Reader/OpenApiReaderRegistry.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@ public static class OpenApiReaderRegistry
2121
/// <param name="reader">The reader instance.</param>
2222
public static void RegisterReader(string format, IOpenApiReader reader)
2323
{
24-
if (!_readers.ContainsKey(format))
25-
{
26-
_readers[format] = reader;
27-
}
24+
_readers[format] = reader;
2825
}
2926

3027
/// <summary>

0 commit comments

Comments
 (0)