Skip to content

Commit a15c50e

Browse files
authored
Merge pull request #2525 from Mahdigln/fix/issue-1294-preserve-baseurl-port
fix: Improve server creation and URL handling logic to maintain port
2 parents 7c65a49 + e332b57 commit a15c50e

File tree

2 files changed

+176
-4
lines changed

2 files changed

+176
-4
lines changed

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,14 @@ private static void MakeServers(IList<OpenApiServer> servers, ParsingContext con
145145
basePath = "/";
146146
}
147147

148-
// If nothing is provided, don't create a server
149-
if (host == null && basePath == null && schemes == null)
148+
// If nothing is provided and there's no defaultUrl, don't create a server
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;
@@ -161,7 +161,7 @@ private static void MakeServers(IList<OpenApiServer> servers, ParsingContext con
161161
// Fill in missing information based on the defaultUrl
162162
if (defaultUrl != null)
163163
{
164-
host = host ?? defaultUrl.GetComponents(UriComponents.NormalizedHost, UriFormat.SafeUnescaped);
164+
host = host ?? defaultUrl.GetComponents(UriComponents.Host | UriComponents.Port, UriFormat.SafeUnescaped);
165165
basePath = basePath ?? defaultUrl.GetComponents(UriComponents.Path, UriFormat.SafeUnescaped);
166166
schemes = schemes ?? new List<string> { defaultUrl.GetComponents(UriComponents.Scheme, UriFormat.SafeUnescaped) };
167167
}
@@ -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 (("https".Equals(uriBuilder.Scheme, StringComparison.OrdinalIgnoreCase) && uriBuilder.Port == 443) ||
241+
("http".Equals(uriBuilder.Scheme, StringComparison.OrdinalIgnoreCase) && 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: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,5 +323,170 @@ public void InvalidHostShouldYieldError()
323323
SpecificationVersion = OpenApiSpecVersion.OpenApi2_0
324324
});
325325
}
326+
327+
[Fact]
328+
public void BaseUrlWithPortShouldPreservePort()
329+
{
330+
var input =
331+
"""
332+
swagger: 2.0
333+
info:
334+
title: test
335+
version: 1.0.0
336+
paths: {}
337+
""";
338+
var reader = new OpenApiStringReader(new()
339+
{
340+
BaseUrl = new("http://demo.testfire.net:8080")
341+
});
342+
343+
var doc = reader.Read(input, out var diagnostic);
344+
345+
var server = doc.Servers.First();
346+
Assert.Single(doc.Servers);
347+
Assert.Equal("http://demo.testfire.net:8080", server.Url);
348+
}
349+
350+
[Fact]
351+
public void BaseUrlWithPortAndPathShouldPreservePort()
352+
{
353+
var input =
354+
"""
355+
swagger: 2.0
356+
info:
357+
title: test
358+
version: 1.0.0
359+
paths: {}
360+
""";
361+
var reader = new OpenApiStringReader(new()
362+
{
363+
BaseUrl = new("http://demo.testfire.net:8080/swagger/properties.json")
364+
});
365+
366+
var doc = reader.Read(input, out var diagnostic);
367+
368+
var server = doc.Servers.First();
369+
Assert.Single(doc.Servers);
370+
Assert.Equal("http://demo.testfire.net:8080/swagger/properties.json", server.Url);
371+
}
372+
373+
[Fact]
374+
public void BaseUrlWithNonStandardPortShouldPreservePort()
375+
{
376+
var input =
377+
"""
378+
swagger: 2.0
379+
info:
380+
title: test
381+
version: 1.0.0
382+
paths: {}
383+
""";
384+
var reader = new OpenApiStringReader(new()
385+
{
386+
BaseUrl = new("https://api.example.com:9443/v1/openapi.yaml")
387+
});
388+
389+
var doc = reader.Read(input, out var diagnostic);
390+
391+
var server = doc.Servers.First();
392+
Assert.Single(doc.Servers);
393+
Assert.Equal("https://api.example.com:9443/v1/openapi.yaml", server.Url);
394+
}
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+
}
326491
}
327492
}

0 commit comments

Comments
 (0)