Skip to content

Commit ababfa3

Browse files
committed
Fix for port in host
1 parent 9424e24 commit ababfa3

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,23 @@ private static void MakeServers(IList<OpenApiServer> servers, ParsingContext con
153153
{
154154
host = "//" + host; // The double slash prefix creates a relative url where the scheme is defined by the BaseUrl
155155
}
156-
156+
int? port = null;
157+
if (host.Contains(":"))
158+
{
159+
var pieces = host.Split(':');
160+
host = pieces.First();
161+
port = int.Parse(pieces.Last());
162+
}
157163
var uriBuilder = new UriBuilder(scheme, host)
158164
{
159165
Path = basePath
160166
};
161167

168+
if (port != null)
169+
{
170+
uriBuilder.Port = port.Value;
171+
}
172+
162173
var server = new OpenApiServer
163174
{
164175
Url = uriBuilder.ToString()

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,5 +235,27 @@ public void MultipleServers()
235235
Assert.Equal("https://dev.bing.com/api", doc.Servers.Last().Url);
236236
}
237237

238+
[Fact]
239+
public void LocalHostWithCustomHost()
240+
{
241+
var input = @"
242+
swagger: 2.0
243+
info:
244+
title: test
245+
version: 1.0.0
246+
host: localhost:23232
247+
paths: {}
248+
";
249+
var reader = new OpenApiStringReader(new OpenApiReaderSettings()
250+
{
251+
BaseUrl = new Uri("https://bing.com")
252+
});
253+
254+
var doc = reader.Read(input, out var diagnostic);
255+
256+
var server = doc.Servers.First();
257+
Assert.Equal(1, doc.Servers.Count);
258+
Assert.Equal("https://localhost:23232", server.Url);
259+
}
238260
}
239261
}

0 commit comments

Comments
 (0)