Skip to content

Commit d21f229

Browse files
authored
Merge pull request #1213 from microsoft/bugfix/optional-base-path
- fixes a bug where the base path would be forcibly set to the description
2 parents 6f3e634 + acf6741 commit d21f229

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,12 @@ private static void MakeServers(IList<OpenApiServer> servers, ParsingContext con
139139
var schemes = context.GetFromTempStorage<List<string>>("schemes");
140140
Uri defaultUrl = rootNode.Context.BaseUrl;
141141

142+
// so we don't default to the document path when a host is provided
143+
if (string.IsNullOrEmpty(basePath) && !string.IsNullOrEmpty(host))
144+
{
145+
basePath = "/";
146+
}
147+
142148
// If nothing is provided, don't create a server
143149
if (host == null && basePath == null && schemes == null)
144150
{

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

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,31 @@ public void JustHostNoDefault()
7474
Assert.Equal("//www.foo.com", server.Url);
7575
}
7676

77+
[Fact]
78+
public void NoBasePath()
79+
{
80+
var input = @"
81+
swagger: 2.0
82+
info:
83+
title: test
84+
version: 1.0.0
85+
host: www.foo.com
86+
schemes:
87+
- http
88+
paths: {}
89+
";
90+
var reader = new OpenApiStringReader(new OpenApiReaderSettings()
91+
{
92+
BaseUrl = new Uri("https://www.foo.com/spec.yaml")
93+
});
94+
95+
var doc = reader.Read(input, out var diagnostic);
96+
97+
var server = doc.Servers.First();
98+
Assert.Equal(1, doc.Servers.Count);
99+
Assert.Equal("http://www.foo.com", server.Url);
100+
}
101+
77102
[Fact]
78103
public void JustBasePathNoDefault()
79104
{
@@ -203,14 +228,14 @@ public void JustHostWithCustomHostWithApi()
203228
";
204229
var reader = new OpenApiStringReader(new OpenApiReaderSettings()
205230
{
206-
BaseUrl = new Uri("https://dev.bing.com/api")
231+
BaseUrl = new Uri("https://dev.bing.com/api/description.yaml")
207232
});
208233

209234
var doc = reader.Read(input, out var diagnostic);
210235

211236
var server = doc.Servers.First();
212237
Assert.Equal(1, doc.Servers.Count);
213-
Assert.Equal("https://prod.bing.com/api", server.Url);
238+
Assert.Equal("https://prod.bing.com", server.Url);
214239
}
215240

216241
[Fact]

0 commit comments

Comments
 (0)