Skip to content

Commit 18babce

Browse files
#319 - changes as per review comments
1 parent f6a3836 commit 18babce

File tree

2 files changed

+10
-17
lines changed

2 files changed

+10
-17
lines changed

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

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ private static void MakeServers(IList<OpenApiServer> servers, ParsingContext con
139139
//Validate host
140140
if (host != null && !IsHostValid(host))
141141
{
142-
rootNode.Diagnostic.Errors.Add(new OpenApiError(new OpenApiException("Invalid host")));
142+
rootNode.Diagnostic.Errors.Add(new OpenApiError(rootNode.Context.GetLocation(), "Invalid host"));
143143
return;
144144
}
145145

@@ -255,22 +255,15 @@ private static void FixRequestBodyReferences(OpenApiDocument doc)
255255

256256
private static bool IsHostValid(string host)
257257
{
258-
try
259-
{
260-
//Check if the host contains ://
261-
if (host.Contains(Uri.SchemeDelimiter))
262-
{
263-
return false;
264-
}
265-
266-
//Check if the host (excluding port number) is a valid dns/ip address.
267-
var hostPart = host.Split(':').First();
268-
return Uri.CheckHostName(hostPart) != UriHostNameType.Unknown;
269-
}
270-
catch (Exception)
258+
//Check if the host contains ://
259+
if (host.Contains(Uri.SchemeDelimiter))
271260
{
272261
return false;
273262
}
263+
264+
//Check if the host (excluding port number) is a valid dns/ip address.
265+
var hostPart = host.Split(':').First();
266+
return Uri.CheckHostName(hostPart) != UriHostNameType.Unknown;
274267
}
275268
}
276269

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ public void LocalHostWithCustomHost()
262262
}
263263

264264
[Fact]
265-
public void InvalidHostShouldYieldDiagnostic()
265+
public void InvalidHostShouldYieldError()
266266
{
267267
var input = @"
268268
swagger: 2.0
@@ -278,13 +278,13 @@ public void InvalidHostShouldYieldDiagnostic()
278278
});
279279

280280
var doc = reader.Read(input, out var diagnostic);
281-
Assert.Equal(0, doc.Servers.Count);
281+
doc.Servers.Count.Should().Be(0);
282282
diagnostic.ShouldBeEquivalentTo(
283283
new OpenApiDiagnostic
284284
{
285285
Errors =
286286
{
287-
new OpenApiError(new OpenApiException("Invalid host"))
287+
new OpenApiError("#/", "Invalid host")
288288
},
289289
SpecificationVersion = OpenApiSpecVersion.OpenApi2_0
290290
});

0 commit comments

Comments
 (0)