diff --git a/src/Microsoft.OpenApi/Reader/V2/OpenApiDocumentDeserializer.cs b/src/Microsoft.OpenApi/Reader/V2/OpenApiDocumentDeserializer.cs index fc981bf7e..5ee8a85ec 100644 --- a/src/Microsoft.OpenApi/Reader/V2/OpenApiDocumentDeserializer.cs +++ b/src/Microsoft.OpenApi/Reader/V2/OpenApiDocumentDeserializer.cs @@ -124,14 +124,14 @@ private static void MakeServers(IList 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; @@ -140,7 +140,7 @@ private static void MakeServers(IList 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)]; } @@ -150,7 +150,7 @@ private static void MakeServers(IList servers, ParsingContext con } // Create the Server objects - if (schemes is {Count: > 0}) + if (schemes is { Count: > 0 }) { foreach (var scheme in schemes) { @@ -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 @@ -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(); } diff --git a/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiServerTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiServerTests.cs index f5f6078a2..c2a8b7115 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiServerTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiServerTests.cs @@ -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); + } } }