Skip to content

Commit 5d92b29

Browse files
authored
Merge pull request #289 from Microsoft/dm/vnext/hostswithports
Handle parsing port in host when reading V2
2 parents ce04d42 + db1996f commit 5d92b29

File tree

2 files changed

+56
-23
lines changed

2 files changed

+56
-23
lines changed

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

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -145,43 +145,23 @@ private static void MakeServers(IList<OpenApiServer> servers, ParsingContext con
145145
}
146146

147147
// Create the Server objects
148-
if (schemes != null)
148+
if (schemes != null && schemes.Count > 0)
149149
{
150150
foreach (var scheme in schemes)
151151
{
152-
if (String.IsNullOrEmpty(scheme))
153-
{
154-
host = "//" + host; // The double slash prefix creates a relative url where the scheme is defined by the BaseUrl
155-
}
156-
157-
var uriBuilder = new UriBuilder(scheme, host)
158-
{
159-
Path = basePath
160-
};
161-
162152
var server = new OpenApiServer
163153
{
164-
Url = uriBuilder.ToString()
154+
Url = BuildUrl(scheme, host, basePath)
165155
};
166156

167157
servers.Add(server);
168158
}
169159
}
170160
else
171161
{
172-
if (!String.IsNullOrEmpty(host))
173-
{
174-
host = "//" + host; // The double slash prefix creates a relative url where the scheme is defined by the BaseUrl
175-
}
176-
var uriBuilder = new UriBuilder()
177-
{
178-
Scheme = null,
179-
Host = host,
180-
Path = basePath
181-
};
182162
var server = new OpenApiServer
183163
{
184-
Url = uriBuilder.ToString()
164+
Url = BuildUrl(null, host, basePath)
185165
};
186166

187167
servers.Add(server);
@@ -198,6 +178,37 @@ private static void MakeServers(IList<OpenApiServer> servers, ParsingContext con
198178
}
199179
}
200180

181+
private static string BuildUrl(string scheme, string host, string basePath)
182+
{
183+
if (String.IsNullOrEmpty(scheme) && !String.IsNullOrEmpty(host))
184+
{
185+
host = "//" + host; // The double slash prefix creates a relative url where the scheme is defined by the BaseUrl
186+
}
187+
188+
int? port = null;
189+
190+
if (!String.IsNullOrEmpty(host) && host.Contains(":"))
191+
{
192+
var pieces = host.Split(':');
193+
host = pieces.First();
194+
port = int.Parse(pieces.Last());
195+
}
196+
197+
var uriBuilder = new UriBuilder()
198+
{
199+
Scheme = scheme,
200+
Host = host,
201+
Path = basePath
202+
};
203+
204+
if (port != null)
205+
{
206+
uriBuilder.Port = port.Value;
207+
}
208+
209+
return uriBuilder.ToString();
210+
}
211+
201212
public static OpenApiDocument LoadOpenApi(RootNode rootNode)
202213
{
203214
var openApidoc = new OpenApiDocument();

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)