Skip to content

Commit ecc9ea0

Browse files
committed
Add tests for MakeServers
1 parent 163e655 commit ecc9ea0

File tree

6 files changed

+215
-15
lines changed

6 files changed

+215
-15
lines changed

src/Microsoft.OpenApi.Readers/Exceptions/OpenApiUnsupportedSpecVersionException.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Microsoft.OpenApi.Readers.Exceptions
1212
[Serializable]
1313
public class OpenApiUnsupportedSpecVersionException : OpenApiReaderException
1414
{
15-
const string messagePattern = "OpenAPI specification version {0} is not supported.";
15+
const string messagePattern = "OpenAPI specification version '{0}' is not supported.";
1616

1717
/// <summary>
1818
/// Initializes the <see cref="OpenApiUnsupportedSpecVersionException"/> class with a specification version.
@@ -21,11 +21,6 @@ public class OpenApiUnsupportedSpecVersionException : OpenApiReaderException
2121
public OpenApiUnsupportedSpecVersionException(string specificationVersion)
2222
: base(string.Format(CultureInfo.InvariantCulture, messagePattern, specificationVersion))
2323
{
24-
if (string.IsNullOrWhiteSpace(specificationVersion))
25-
{
26-
throw new ArgumentException("Value cannot be null or white space.", nameof(specificationVersion));
27-
}
28-
2924
this.SpecificationVersion = specificationVersion;
3025
}
3126

@@ -38,11 +33,6 @@ public OpenApiUnsupportedSpecVersionException(string specificationVersion)
3833
public OpenApiUnsupportedSpecVersionException(string specificationVersion, Exception innerException)
3934
: base(string.Format(CultureInfo.InvariantCulture, messagePattern, specificationVersion), innerException)
4035
{
41-
if (string.IsNullOrWhiteSpace(specificationVersion))
42-
{
43-
throw new ArgumentException("Value cannot be null or white space.", nameof(specificationVersion));
44-
}
45-
4636
this.SpecificationVersion = specificationVersion;
4737
}
4838

src/Microsoft.OpenApi.Readers/Microsoft.OpenApi.Readers.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<Company>Microsoft</Company>
1111
<Title>Microsoft.OpenApi.Readers</Title>
1212
<PackageId>Microsoft.OpenApi.Readers</PackageId>
13-
<Version>1.1.0-preview.1</Version>
13+
<Version>1.1.0-preview.4</Version>
1414
<Description>OpenAPI.NET Readers for JSON and YAML documents</Description>
1515
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright>
1616
<PackageTags>OpenAPI .NET</PackageTags>

src/Microsoft.OpenApi.Readers/OpenApiReaderSettings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,6 @@ public class OpenApiReaderSettings
5252
/// <summary>
5353
/// URL where relative references should be resolved from if the description does not contain Server definitions
5454
/// </summary>
55-
public Uri BaseUrl { get; internal set; } = new Uri("https://example.org/");
55+
public Uri BaseUrl { get; set; } = new Uri("https://example.org/");
5656
}
5757
}

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,15 +136,28 @@ private static void MakeServers(IList<OpenApiServer> servers, ParsingContext con
136136
basePath = basePath ?? defaultUrl.GetComponents(UriComponents.Path, UriFormat.SafeUnescaped);
137137
schemes = schemes ?? new List<string> { defaultUrl.GetComponents(UriComponents.Scheme, UriFormat.SafeUnescaped) };
138138

139+
139140
// Create the Server objects
140141
if (schemes != null)
141142
{
142143
foreach (var scheme in schemes)
143144
{
145+
var ub = new UriBuilder(scheme, host)
146+
{
147+
Path = basePath
148+
};
149+
144150
var server = new OpenApiServer
145151
{
146-
Url = $"{scheme}://{host}{basePath}"
152+
Url = ub.ToString()
147153
};
154+
155+
// Server Urls are always appended to Paths and Paths must start with /
156+
// so removing the slash prevents a double slash.
157+
if (server.Url.EndsWith("/"))
158+
{
159+
server.Url = server.Url.Substring(0, server.Url.Length - 1);
160+
}
148161
servers.Add(server);
149162
}
150163
}

src/Microsoft.OpenApi/Microsoft.OpenApi.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<Company>Microsoft</Company>
1111
<Title>Microsoft.OpenApi</Title>
1212
<PackageId>Microsoft.OpenApi</PackageId>
13-
<Version>1.1.0-preview.1</Version>
13+
<Version>1.1.0-preview.2</Version>
1414
<Description>.NET models with JSON and YAML writers for OpenAPI specification</Description>
1515
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright>
1616
<PackageTags>OpenAPI .NET</PackageTags>
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Xunit;
7+
8+
namespace Microsoft.OpenApi.Readers.Tests.V2Tests
9+
{
10+
public class OpenApiServerTests
11+
{
12+
[Fact]
13+
public void NoServer()
14+
{
15+
var input = @"
16+
swagger: 2.0
17+
info:
18+
title: test
19+
version: 1.0.0
20+
paths: {}
21+
";
22+
var reader = new OpenApiStringReader(new OpenApiReaderSettings() {
23+
});
24+
25+
var doc = reader.Read(input, out var diagnostic);
26+
27+
Assert.Empty(doc.Servers);
28+
}
29+
30+
[Fact]
31+
public void JustScheme()
32+
{
33+
var input = @"
34+
swagger: 2.0
35+
info:
36+
title: test
37+
version: 1.0.0
38+
schemes:
39+
- http
40+
paths: {}
41+
";
42+
var reader = new OpenApiStringReader(new OpenApiReaderSettings()
43+
{
44+
});
45+
46+
var doc = reader.Read(input, out var diagnostic);
47+
48+
var server = doc.Servers.First();
49+
Assert.Equal(1,doc.Servers.Count);
50+
Assert.Equal("http://example.org", server.Url);
51+
}
52+
53+
[Fact]
54+
public void JustSchemeWithCustomHost()
55+
{
56+
var input = @"
57+
swagger: 2.0
58+
info:
59+
title: test
60+
version: 1.0.0
61+
schemes:
62+
- http
63+
paths: {}
64+
";
65+
var reader = new OpenApiStringReader(new OpenApiReaderSettings()
66+
{
67+
BaseUrl = new Uri("https://bing.com/foo")
68+
});
69+
70+
var doc = reader.Read(input, out var diagnostic);
71+
72+
var server = doc.Servers.First();
73+
Assert.Equal(1, doc.Servers.Count);
74+
Assert.Equal("http://bing.com/foo", server.Url);
75+
}
76+
77+
[Fact]
78+
public void JustSchemeWithCustomHostWithEmptyPath()
79+
{
80+
var input = @"
81+
swagger: 2.0
82+
info:
83+
title: test
84+
version: 1.0.0
85+
schemes:
86+
- http
87+
paths: {}
88+
";
89+
var reader = new OpenApiStringReader(new OpenApiReaderSettings()
90+
{
91+
BaseUrl = new Uri("https://bing.com")
92+
});
93+
94+
var doc = reader.Read(input, out var diagnostic);
95+
96+
var server = doc.Servers.First();
97+
Assert.Equal(1, doc.Servers.Count);
98+
Assert.Equal("http://bing.com", server.Url);
99+
}
100+
101+
[Fact]
102+
public void JustBasePathWithCustomHost()
103+
{
104+
var input = @"
105+
swagger: 2.0
106+
info:
107+
title: test
108+
version: 1.0.0
109+
basePath: /api
110+
paths: {}
111+
";
112+
var reader = new OpenApiStringReader(new OpenApiReaderSettings()
113+
{
114+
BaseUrl = new Uri("https://bing.com")
115+
});
116+
117+
var doc = reader.Read(input, out var diagnostic);
118+
119+
var server = doc.Servers.First();
120+
Assert.Equal(1, doc.Servers.Count);
121+
Assert.Equal("https://bing.com/api", server.Url);
122+
}
123+
124+
[Fact]
125+
public void JustHostWithCustomHost()
126+
{
127+
var input = @"
128+
swagger: 2.0
129+
info:
130+
title: test
131+
version: 1.0.0
132+
host: www.example.com
133+
paths: {}
134+
";
135+
var reader = new OpenApiStringReader(new OpenApiReaderSettings()
136+
{
137+
BaseUrl = new Uri("https://bing.com")
138+
});
139+
140+
var doc = reader.Read(input, out var diagnostic);
141+
142+
var server = doc.Servers.First();
143+
Assert.Equal(1, doc.Servers.Count);
144+
Assert.Equal("https://www.example.com", server.Url);
145+
}
146+
147+
[Fact]
148+
public void JustHostWithCustomHostWithApi()
149+
{
150+
var input = @"
151+
swagger: 2.0
152+
info:
153+
title: test
154+
version: 1.0.0
155+
host: prod.bing.com
156+
paths: {}
157+
";
158+
var reader = new OpenApiStringReader(new OpenApiReaderSettings()
159+
{
160+
BaseUrl = new Uri("https://dev.bing.com/api")
161+
});
162+
163+
var doc = reader.Read(input, out var diagnostic);
164+
165+
var server = doc.Servers.First();
166+
Assert.Equal(1, doc.Servers.Count);
167+
Assert.Equal("https://prod.bing.com/api", server.Url);
168+
}
169+
170+
[Fact]
171+
public void MultipleServers()
172+
{
173+
var input = @"
174+
swagger: 2.0
175+
info:
176+
title: test
177+
version: 1.0.0
178+
schemes:
179+
- http
180+
- https
181+
paths: {}
182+
";
183+
var reader = new OpenApiStringReader(new OpenApiReaderSettings()
184+
{
185+
BaseUrl = new Uri("https://dev.bing.com/api")
186+
});
187+
188+
var doc = reader.Read(input, out var diagnostic);
189+
190+
var server = doc.Servers.First();
191+
Assert.Equal(2, doc.Servers.Count);
192+
Assert.Equal("http://dev.bing.com/api", server.Url);
193+
Assert.Equal("https://dev.bing.com/api", doc.Servers.Last().Url);
194+
}
195+
196+
}
197+
}

0 commit comments

Comments
 (0)