Skip to content

Commit 163e655

Browse files
committed
Fix MakeServers for V2
1 parent 469a55a commit 163e655

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

src/Microsoft.OpenApi.Readers/OpenApiReaderSettings.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,9 @@ public class OpenApiReaderSettings
4949
/// </summary>
5050
public ValidationRuleSet RuleSet { get; set; } = ValidationRuleSet.GetDefaultRuleSet();
5151

52+
/// <summary>
53+
/// URL where relative references should be resolved from if the description does not contain Server definitions
54+
/// </summary>
55+
public Uri BaseUrl { get; internal set; } = new Uri("https://example.org/");
5256
}
5357
}

src/Microsoft.OpenApi.Readers/OpenApiStreamReader.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ public OpenApiDocument Read(Stream input, out OpenApiDiagnostic diagnostic)
5757

5858
context = new ParsingContext
5959
{
60-
ExtensionParsers = _settings.ExtensionParsers
60+
ExtensionParsers = _settings.ExtensionParsers,
61+
BaseUrl = _settings.BaseUrl
6162
};
6263

6364
OpenApiDocument document = null;

src/Microsoft.OpenApi.Readers/ParsingContext.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public class ParsingContext
2828
internal Dictionary<string, Func<IOpenApiAny, IOpenApiExtension>> ExtensionParsers { get; set; } = new Dictionary<string, Func<IOpenApiAny, IOpenApiExtension>>();
2929
internal RootNode RootNode { get; set; }
3030
internal List<OpenApiTag> Tags { get; private set; } = new List<OpenApiTag>();
31+
internal Uri BaseUrl { get; set; }
3132

3233
/// <summary>
3334
/// Initiates the parsing process. Not thread safe and should only be called once on a parsing context

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,21 +119,35 @@ internal static partial class OpenApiV2Deserializer
119119
{s => s.StartsWith("x-"), (o, p, n) => o.AddExtension(p, n.CreateAny())}
120120
};
121121

122-
private static void MakeServers(IList<OpenApiServer> servers, ParsingContext context)
122+
private static void MakeServers(IList<OpenApiServer> servers, ParsingContext context, Uri defaultUrl)
123123
{
124124
var host = context.GetFromTempStorage<string>("host");
125125
var basePath = context.GetFromTempStorage<string>("basePath");
126126
var schemes = context.GetFromTempStorage<List<string>>("schemes");
127127

128+
// If nothing is provided, don't create a server
129+
if (host == null && basePath == null && schemes == null)
130+
{
131+
return;
132+
}
133+
134+
// Fill in missing information based on the defaultUrl
135+
host = host ?? defaultUrl.GetComponents(UriComponents.NormalizedHost, UriFormat.SafeUnescaped);
136+
basePath = basePath ?? defaultUrl.GetComponents(UriComponents.Path, UriFormat.SafeUnescaped);
137+
schemes = schemes ?? new List<string> { defaultUrl.GetComponents(UriComponents.Scheme, UriFormat.SafeUnescaped) };
138+
139+
// Create the Server objects
128140
if (schemes != null)
129141
{
130142
foreach (var scheme in schemes)
131143
{
132-
var server = new OpenApiServer();
133-
server.Url = scheme + "://" + (host ?? "example.org/") + (basePath ?? "/");
144+
var server = new OpenApiServer
145+
{
146+
Url = $"{scheme}://{host}{basePath}"
147+
};
134148
servers.Add(server);
135149
}
136-
}
150+
}
137151
}
138152

139153
public static OpenApiDocument LoadOpenApi(RootNode rootNode)
@@ -151,7 +165,7 @@ public static OpenApiDocument LoadOpenApi(RootNode rootNode)
151165
openApidoc.Servers = new List<OpenApiServer>();
152166
}
153167

154-
MakeServers(openApidoc.Servers, openApiNode.Context);
168+
MakeServers(openApidoc.Servers, openApiNode.Context, rootNode.Context.BaseUrl);
155169

156170
FixRequestBodyReferences(openApidoc);
157171
return openApidoc;

0 commit comments

Comments
 (0)