Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<Company>Microsoft</Company>
<Title>Microsoft.OpenApi.Readers</Title>
<PackageId>Microsoft.OpenApi.Readers</PackageId>
<Version>1.6.4-preview2</Version>
<Version>1.6.4-preview3</Version>
<Description>OpenAPI.NET Readers for JSON and YAML documents</Description>
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright>
<PackageTags>OpenAPI .NET</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ private static void MakeServers(IList<OpenApiServer> servers, ParsingContext con
var schemes = context.GetFromTempStorage<List<string>>("schemes");
Uri defaultUrl = rootNode.Context.BaseUrl;

// so we don't default to the document path when a host is provided
if (string.IsNullOrEmpty(basePath) && !string.IsNullOrEmpty(host))
{
basePath = "/";
}

// If nothing is provided, don't create a server
if (host == null && basePath == null && schemes == null)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0-windows</TargetFramework>
<OutputType>WinExe</OutputType>
Expand All @@ -10,7 +10,7 @@
<PackageReference Include="Microsoft.DotNet.UpgradeAssistant.Extensions.Default.Analyzers" Version="0.4.410601">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.Windows.Compatibility" Version="7.0.0" />
<PackageReference Include="Microsoft.Windows.Compatibility" Version="7.0.1" />
</ItemGroup>
<ItemGroup>
<Resource Include="Themes\Metro\HowToApplyTheme.txt" />
Expand Down
24 changes: 18 additions & 6 deletions src/Microsoft.OpenApi/Helpers/DictionaryCloneHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Microsoft.OpenApi.Helpers
/// <summary>
/// Helper class for deep cloning dictionaries.
/// </summary>
internal class DictionaryCloneHelper
internal static class DictionaryCloneHelper
{
/// <summary>
/// Deep clone key value pairs in a dictionary.
Expand All @@ -21,14 +21,26 @@ internal class DictionaryCloneHelper
internal static Dictionary<T, U> Clone<T, U>(IDictionary<T, U> dictionary)
{
if (dictionary is null) return null;

var clonedDictionary = new Dictionary<T, U>(dictionary.Keys.Count);
var clonedObjects = new Dictionary<object, object>();

foreach (var kvp in dictionary)
foreach (var keyValuePair in dictionary)
{
// Create instance of the specified type using the constructor matching the specified parameter types.
clonedDictionary[kvp.Key] = (U)Activator.CreateInstance(kvp.Value.GetType(), kvp.Value);
}

// If the object has already been cloned, use the cloned object instead of cloning it again
if (clonedObjects.TryGetValue(keyValuePair.Value, out var clonedValue))
{
clonedDictionary[keyValuePair.Key] = (U)clonedValue;
}
else
{
// Create instance of the specified type using the constructor matching the specified parameter types.
clonedDictionary[keyValuePair.Key] = (U)Activator.CreateInstance(keyValuePair.Value.GetType(), keyValuePair.Value);

// Add the cloned object to the dictionary of cloned objects
clonedObjects.Add(keyValuePair.Value, clonedDictionary[keyValuePair.Key]);
}
}

return clonedDictionary;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi/Microsoft.OpenApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<Company>Microsoft</Company>
<Title>Microsoft.OpenApi</Title>
<PackageId>Microsoft.OpenApi</PackageId>
<Version>1.6.4-preview2</Version>
<Version>1.6.4-preview3</Version>
<Description>.NET models with JSON and YAML writers for OpenAPI specification</Description>
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright>
<PackageTags>OpenAPI .NET</PackageTags>
Expand Down
29 changes: 27 additions & 2 deletions test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiServerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,31 @@ public void JustHostNoDefault()
Assert.Equal("//www.foo.com", server.Url);
}

[Fact]
public void NoBasePath()
{
var input = @"
swagger: 2.0
info:
title: test
version: 1.0.0
host: www.foo.com
schemes:
- http
paths: {}
";
var reader = new OpenApiStringReader(new OpenApiReaderSettings()
{
BaseUrl = new Uri("https://www.foo.com/spec.yaml")
});

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

var server = doc.Servers.First();
Assert.Equal(1, doc.Servers.Count);
Assert.Equal("http://www.foo.com", server.Url);
}

[Fact]
public void JustBasePathNoDefault()
{
Expand Down Expand Up @@ -203,14 +228,14 @@ public void JustHostWithCustomHostWithApi()
";
var reader = new OpenApiStringReader(new OpenApiReaderSettings()
{
BaseUrl = new Uri("https://dev.bing.com/api")
BaseUrl = new Uri("https://dev.bing.com/api/description.yaml")
});

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

var server = doc.Servers.First();
Assert.Equal(1, doc.Servers.Count);
Assert.Equal("https://prod.bing.com/api", server.Url);
Assert.Equal("https://prod.bing.com", server.Url);
}

[Fact]
Expand Down