Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ private static void MakeServers(IList<OpenApiServer> servers, ParsingContext con
basePath = "/";
}

// If nothing is provided, don't create a server
if (host == null && basePath == null && schemes == null)
// If nothing is provided and there's no defaultUrl, don't create a server
if (host == null && basePath == null && schemes == null && defaultUrl == null)
{
return;
}
Expand All @@ -161,7 +161,7 @@ private static void MakeServers(IList<OpenApiServer> servers, ParsingContext con
// Fill in missing information based on the defaultUrl
if (defaultUrl != null)
{
host = host ?? defaultUrl.GetComponents(UriComponents.NormalizedHost, UriFormat.SafeUnescaped);
host = host ?? defaultUrl.GetComponents(UriComponents.Host | UriComponents.Port, UriFormat.SafeUnescaped);
basePath = basePath ?? defaultUrl.GetComponents(UriComponents.Path, UriFormat.SafeUnescaped);
schemes = schemes ?? new List<string> { defaultUrl.GetComponents(UriComponents.Scheme, UriFormat.SafeUnescaped) };
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,5 +323,74 @@ public void InvalidHostShouldYieldError()
SpecificationVersion = OpenApiSpecVersion.OpenApi2_0
});
}

[Fact]
public void BaseUrlWithPortShouldPreservePort()
{
var input =
"""
swagger: 2.0
info:
title: test
version: 1.0.0
paths: {}
""";
var reader = new OpenApiStringReader(new()
{
BaseUrl = new("http://demo.testfire.net:8080")
});

var doc = reader.Read(input, out var diagnostic);

var server = doc.Servers.First();
Assert.Single(doc.Servers);
Assert.Equal("http://demo.testfire.net:8080", server.Url);
}

[Fact]
public void BaseUrlWithPortAndPathShouldPreservePort()
{
var input =
"""
swagger: 2.0
info:
title: test
version: 1.0.0
paths: {}
""";
var reader = new OpenApiStringReader(new()
{
BaseUrl = new("http://demo.testfire.net:8080/swagger/properties.json")
});

var doc = reader.Read(input, out var diagnostic);

var server = doc.Servers.First();
Assert.Single(doc.Servers);
Assert.Equal("http://demo.testfire.net:8080/swagger/properties.json", server.Url);
}

[Fact]
public void BaseUrlWithNonStandardPortShouldPreservePort()
{
var input =
"""
swagger: 2.0
info:
title: test
version: 1.0.0
paths: {}
""";
var reader = new OpenApiStringReader(new()
{
BaseUrl = new("https://api.example.com:9443/v1/openapi.yaml")
});

var doc = reader.Read(input, out var diagnostic);

var server = doc.Servers.First();
Assert.Single(doc.Servers);
Assert.Equal("https://api.example.com:9443/v1/openapi.yaml", server.Url);
}
}
}