Skip to content

Commit 11c169c

Browse files
committed
Refactored buildUrl
1 parent dff6ff7 commit 11c169c

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed

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

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -149,50 +149,19 @@ private static void MakeServers(IList<OpenApiServer> servers, ParsingContext con
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-
int? port = null;
157-
if (host.Contains(":"))
158-
{
159-
var pieces = host.Split(':');
160-
host = pieces.First();
161-
port = int.Parse(pieces.Last());
162-
}
163-
var uriBuilder = new UriBuilder(scheme, host)
164-
{
165-
Path = basePath
166-
};
167-
168-
if (port != null)
169-
{
170-
uriBuilder.Port = port.Value;
171-
}
172-
173152
var server = new OpenApiServer
174153
{
175-
Url = uriBuilder.ToString()
154+
Url = BuildUrl(scheme, host, basePath)
176155
};
177156

178157
servers.Add(server);
179158
}
180159
}
181160
else
182161
{
183-
if (!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-
var uriBuilder = new UriBuilder()
188-
{
189-
Scheme = null,
190-
Host = host,
191-
Path = basePath
192-
};
193162
var server = new OpenApiServer
194163
{
195-
Url = uriBuilder.ToString()
164+
Url = BuildUrl(null, host, basePath)
196165
};
197166

198167
servers.Add(server);
@@ -209,6 +178,37 @@ private static void MakeServers(IList<OpenApiServer> servers, ParsingContext con
209178
}
210179
}
211180

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+
212212
public static OpenApiDocument LoadOpenApi(RootNode rootNode)
213213
{
214214
var openApidoc = new OpenApiDocument();

0 commit comments

Comments
 (0)