Skip to content

Commit fe5ceda

Browse files
Mahdi GolestanMahdi Golestan
authored andcommitted
Enhance server creation logic and add unit tests
Updated the `MakeServers` method to include a check for `defaultUrl` when creating servers. The method now returns early if all relevant parameters are null, and the host assignment from `defaultUrl` now preserves the port. Added new unit tests in `OpenApiServerTests.cs` to ensure that base URLs with ports are correctly handled. Tests cover scenarios for base URLs with a port, with a port and path, and with a non-standard port.
1 parent afbc27d commit fe5ceda

File tree

2 files changed

+72
-3
lines changed

2 files changed

+72
-3
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ 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 (host == null && basePath == null && schemes == null && defaultUrl == null)
150150
{
151151
return;
152152
}
@@ -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
}

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

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,5 +323,74 @@ 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+
}
326395
}
327396
}

0 commit comments

Comments
 (0)