in src/Microsoft.OpenApi.Readers/V2/OpenApiDocumentDeserializer.cs [124:188]
private static void MakeServers(IList<OpenApiServer> servers, ParsingContext context, RootNode rootNode)
{
var host = context.GetFromTempStorage<string>("host");
var basePath = context.GetFromTempStorage<string>("basePath");
var schemes = context.GetFromTempStorage<List<string>>("schemes");
Uri defaultUrl = rootNode.Context.BaseUrl;
// If nothing is provided, don't create a server
if (host == null && basePath == null && schemes == null)
{
return;
}
//Validate host
if (host != null && !IsHostValid(host))
{
rootNode.Context.Diagnostic.Errors.Add(new OpenApiError(rootNode.Context.GetLocation(), "Invalid host"));
return;
}
// Fill in missing information based on the defaultUrl
if (defaultUrl != null)
{
host = host ?? defaultUrl.GetComponents(UriComponents.NormalizedHost, UriFormat.SafeUnescaped);
basePath = basePath ?? defaultUrl.GetComponents(UriComponents.Path, UriFormat.SafeUnescaped);
schemes = schemes ?? new List<string> { defaultUrl.GetComponents(UriComponents.Scheme, UriFormat.SafeUnescaped) };
}
else if (String.IsNullOrEmpty(host) && String.IsNullOrEmpty(basePath))
{
return; // Can't make a server object out of just a Scheme
}
// Create the Server objects
if (schemes != null && schemes.Count > 0)
{
foreach (var scheme in schemes)
{
var server = new OpenApiServer
{
Url = BuildUrl(scheme, host, basePath)
};
servers.Add(server);
}
}
else
{
var server = new OpenApiServer
{
Url = BuildUrl(null, host, basePath)
};
servers.Add(server);
}
foreach (var server in servers)
{
// Server Urls are always appended to Paths and Paths must start with /
// so removing the slash prevents a double slash.
if (server.Url.EndsWith("/"))
{
server.Url = server.Url.Substring(0, server.Url.Length - 1);
}
}
}