Skip to content

Commit 664a2f4

Browse files
Mahdi GolestanMahdi Golestan
authored andcommitted
Improve server URL handling and add unit tests
Refactor `MakeServers` method for better null checks on `host`, `basePath`, `schemes`, and `defaultUrl`. Enhance `BuildUrl` to remove default ports (80 for HTTP, 443 for HTTPS) from generated URLs. Add unit tests in `OpenApiServerTests.cs` to verify correct URL generation when standard ports are used.
1 parent fe5ceda commit 664a2f4

File tree

2 files changed

+105
-2
lines changed

2 files changed

+105
-2
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,13 @@ private static void MakeServers(IList<OpenApiServer> servers, ParsingContext con
146146
}
147147

148148
// If nothing is provided and there's no defaultUrl, don't create a server
149-
if (host == null && basePath == null && schemes == null && defaultUrl == null)
149+
if (string.IsNullOrEmpty(host) && string.IsNullOrEmpty(basePath) && (schemes == null || schemes.Count == 0) && defaultUrl == null)
150150
{
151151
return;
152152
}
153153

154154
//Validate host
155-
if (host != null && !IsHostValid(host))
155+
if (!string.IsNullOrEmpty(host) && !IsHostValid(host))
156156
{
157157
rootNode.Context.Diagnostic.Errors.Add(new(rootNode.Context.GetLocation(), "Invalid host"));
158158
return;
@@ -236,6 +236,13 @@ private static string BuildUrl(string scheme, string host, string basePath)
236236
uriBuilder.Port = port.Value;
237237
}
238238

239+
// Remove default ports to clean up the URL
240+
if ((uriBuilder.Scheme == "https" && uriBuilder.Port == 443) ||
241+
(uriBuilder.Scheme == "http" && uriBuilder.Port == 80))
242+
{
243+
uriBuilder.Port = -1; // Setting to -1 removes the port from the URL
244+
}
245+
239246
return uriBuilder.ToString();
240247
}
241248

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

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,5 +392,101 @@ public void BaseUrlWithNonStandardPortShouldPreservePort()
392392
Assert.Single(doc.Servers);
393393
Assert.Equal("https://api.example.com:9443/v1/openapi.yaml", server.Url);
394394
}
395+
396+
[Fact]
397+
public void BaseUrlWithStandardHttpsPortShouldRemovePort()
398+
{
399+
var input =
400+
"""
401+
swagger: 2.0
402+
info:
403+
title: test
404+
version: 1.0.0
405+
paths: {}
406+
""";
407+
var reader = new OpenApiStringReader(new()
408+
{
409+
BaseUrl = new("https://foo.bar:443/api")
410+
});
411+
412+
var doc = reader.Read(input, out var diagnostic);
413+
414+
var server = doc.Servers.First();
415+
Assert.Single(doc.Servers);
416+
Assert.Equal("https://foo.bar/api", server.Url);
417+
}
418+
419+
[Fact]
420+
public void BaseUrlWithStandardHttpPortShouldRemovePort()
421+
{
422+
var input =
423+
"""
424+
swagger: 2.0
425+
info:
426+
title: test
427+
version: 1.0.0
428+
paths: {}
429+
""";
430+
var reader = new OpenApiStringReader(new()
431+
{
432+
BaseUrl = new("http://foo.bar:80/api")
433+
});
434+
435+
var doc = reader.Read(input, out var diagnostic);
436+
437+
var server = doc.Servers.First();
438+
Assert.Single(doc.Servers);
439+
Assert.Equal("http://foo.bar/api", server.Url);
440+
}
441+
442+
[Fact]
443+
public void HostWithStandardHttpsPortShouldRemovePort()
444+
{
445+
var input =
446+
"""
447+
swagger: 2.0
448+
info:
449+
title: test
450+
version: 1.0.0
451+
host: foo.bar:443
452+
schemes:
453+
- https
454+
paths: {}
455+
""";
456+
var reader = new OpenApiStringReader(new()
457+
{
458+
});
459+
460+
var doc = reader.Read(input, out var diagnostic);
461+
462+
var server = doc.Servers.First();
463+
Assert.Single(doc.Servers);
464+
Assert.Equal("https://foo.bar", server.Url);
465+
}
466+
467+
[Fact]
468+
public void HostWithStandardHttpPortShouldRemovePort()
469+
{
470+
var input =
471+
"""
472+
swagger: 2.0
473+
info:
474+
title: test
475+
version: 1.0.0
476+
host: foo.bar:80
477+
schemes:
478+
- http
479+
paths: {}
480+
""";
481+
var reader = new OpenApiStringReader(new()
482+
{
483+
});
484+
485+
var doc = reader.Read(input, out var diagnostic);
486+
487+
var server = doc.Servers.First();
488+
Assert.Single(doc.Servers);
489+
Assert.Equal("http://foo.bar", server.Url);
490+
}
395491
}
396492
}

0 commit comments

Comments
 (0)