Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Fix for port in host
  • Loading branch information
darrelmiller committed Jun 16, 2018
commit ababfa3704d9adc7786346d00f7914cb805d75a7
13 changes: 12 additions & 1 deletion src/Microsoft.OpenApi.Readers/V2/OpenApiDocumentDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,23 @@ private static void MakeServers(IList<OpenApiServer> servers, ParsingContext con
{
host = "//" + host; // The double slash prefix creates a relative url where the scheme is defined by the BaseUrl
}

int? port = null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

int? [](start = 20, length = 4)

nullable not needed -- int.Parse(pieces.Last()) throws an error if pieces.Last() is not parsable.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PerthCharern I think my logic is correct here. If the host does not contain ":" then we won't try to call int.Parse(pieces.Last()) and therefore port will be null. I don't want to update uriBuilder.Port if port isn't specified because the URL should omit port.
If the host does contain ":" and the part after the colon is not a number, I'm ok with it throwing a parsing exception.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense.

if (host.Contains(":"))
{
var pieces = host.Split(':');
host = pieces.First();
port = int.Parse(pieces.Last());
}
var uriBuilder = new UriBuilder(scheme, host)
{
Path = basePath
};

if (port != null)
{
uriBuilder.Port = port.Value;
}

var server = new OpenApiServer
{
Url = uriBuilder.ToString()
Expand Down
22 changes: 22 additions & 0 deletions test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiServerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,5 +235,27 @@ public void MultipleServers()
Assert.Equal("https://dev.bing.com/api", doc.Servers.Last().Url);
}

[Fact]
public void LocalHostWithCustomHost()
{
var input = @"
swagger: 2.0
info:
title: test
version: 1.0.0
host: localhost:23232
paths: {}
";
var reader = new OpenApiStringReader(new OpenApiReaderSettings()
{
BaseUrl = new Uri("https://bing.com")
});

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

var server = doc.Servers.First();
Assert.Equal(1, doc.Servers.Count);
Assert.Equal("https://localhost:23232", server.Url);
}
}
}