Skip to content
Merged
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
21 changes: 14 additions & 7 deletions src/Microsoft.OpenApi/Reader/V2/OpenApiDocumentDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,14 @@ 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 (string.IsNullOrEmpty(host) && string.IsNullOrEmpty(basePath) && (schemes == null || schemes.Count == 0) && defaultUrl == null)
{
return;
}

//Validate host
if (host != null && !IsHostValid(host))
if (!string.IsNullOrEmpty(host) && !IsHostValid(host!))
{
rootNode.Context.Diagnostic.Errors.Add(new(rootNode.Context.GetLocation(), "Invalid host"));
return;
Expand All @@ -140,7 +140,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 ?? [defaultUrl.GetComponents(UriComponents.Scheme, UriFormat.SafeUnescaped)];
}
Expand All @@ -150,7 +150,7 @@ private static void MakeServers(IList<OpenApiServer> servers, ParsingContext con
}

// Create the Server objects
if (schemes is {Count: > 0})
if (schemes is { Count: > 0 })
{
foreach (var scheme in schemes)
{
Expand Down Expand Up @@ -202,8 +202,8 @@ private static string BuildUrl(string? scheme, string? host, string? basePath)
if (pieces is not null)
{
host = pieces[0];
port = int.Parse(pieces[pieces.Count() -1], CultureInfo.InvariantCulture);
}
port = int.Parse(pieces[pieces.Count() - 1], CultureInfo.InvariantCulture);
}
}

var uriBuilder = new UriBuilder
Expand All @@ -218,6 +218,13 @@ private static string BuildUrl(string? scheme, string? host, string? basePath)
uriBuilder.Port = port.Value;
}

// Remove default ports to clean up the URL
if (("https".Equals(uriBuilder.Scheme, StringComparison.OrdinalIgnoreCase) && uriBuilder.Port == 443) ||
("http".Equals(uriBuilder.Scheme, StringComparison.OrdinalIgnoreCase) && uriBuilder.Port == 80))
{
uriBuilder.Port = -1; // Setting to -1 removes the port from the URL
}

return uriBuilder.ToString();
}

Expand Down
172 changes: 172 additions & 0 deletions test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiServerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -318,5 +318,177 @@ public void InvalidHostShouldYieldError()
Format = OpenApiConstants.Yaml
}, result.Diagnostic);
}

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

var result = OpenApiDocument.Parse(input, "yaml", settings);

var server = result.Document.Servers.First();
Assert.Single(result.Document.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 settings = new OpenApiReaderSettings
{
BaseUrl = new("http://demo.testfire.net:8080/swagger/properties.json")
};
settings.AddYamlReader();

var result = OpenApiDocument.Parse(input, "yaml", settings);

var server = result.Document.Servers.First();
Assert.Single(result.Document.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 settings = new OpenApiReaderSettings
{
BaseUrl = new("https://api.example.com:9443/v1/openapi.yaml")
};
settings.AddYamlReader();

var result = OpenApiDocument.Parse(input, "yaml", settings);

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

[Fact]
public void BaseUrlWithStandardHttpsPortShouldRemovePort()
{
var input =
"""
swagger: 2.0
info:
title: test
version: 1.0.0
paths: {}
""";
var settings = new OpenApiReaderSettings
{
BaseUrl = new("https://foo.bar:443/api")
};
settings.AddYamlReader();

var result = OpenApiDocument.Parse(input, "yaml", settings);

var server = result.Document.Servers.First();
Assert.Single(result.Document.Servers);
Assert.Equal("https://foo.bar/api", server.Url);
}

[Fact]
public void BaseUrlWithStandardHttpPortShouldRemovePort()
{
var input =
"""
swagger: 2.0
info:
title: test
version: 1.0.0
paths: {}
""";
var settings = new OpenApiReaderSettings
{
BaseUrl = new("http://foo.bar:80/api")
};
settings.AddYamlReader();

var result = OpenApiDocument.Parse(input, "yaml", settings);

var server = result.Document.Servers.First();
Assert.Single(result.Document.Servers);
Assert.Equal("http://foo.bar/api", server.Url);
}

[Fact]
public void HostWithStandardHttpsPortShouldRemovePort()
{
var input =
"""
swagger: 2.0
info:
title: test
version: 1.0.0
host: foo.bar:443
schemes:
- https
paths: {}
""";
var settings = new OpenApiReaderSettings
{
};
settings.AddYamlReader();

var result = OpenApiDocument.Parse(input, "yaml", settings);

var server = result.Document.Servers.First();
Assert.Single(result.Document.Servers);
Assert.Equal("https://foo.bar", server.Url);
}

[Fact]
public void HostWithStandardHttpPortShouldRemovePort()
{
var input =
"""
swagger: 2.0
info:
title: test
version: 1.0.0
host: foo.bar:80
schemes:
- http
paths: {}
""";
var settings = new OpenApiReaderSettings
{
};
settings.AddYamlReader();

var result = OpenApiDocument.Parse(input, "yaml", settings);

var server = result.Document.Servers.First();
Assert.Single(result.Document.Servers);
Assert.Equal("http://foo.bar", server.Url);
}
}
}