Skip to content

Commit

Permalink
Merge pull request #138 from alexandery/master
Browse files Browse the repository at this point in the history
@alexandery Thanks so much for implementing this!
  • Loading branch information
rbeauchamp authored Apr 24, 2017
2 parents 4defe99 + 1779f81 commit 0d96a0c
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<Import Project="$(NuProjPath)\NuProj.props" Condition="Exists('$(NuProjPath)\NuProj.props')" />
<PropertyGroup Label="Configuration">
<Id>Swashbuckle.OData</Id>
<Version>3.2.0</Version>
<Version>3.2.1</Version>
<Title>Swashbuckle.OData</Title>
<Authors>Richard Beauchamp and the Swashbuckle.OData contributors</Authors>
<Owners>Richard Beauchamp</Owners>
Expand Down
3 changes: 1 addition & 2 deletions Swashbuckle.OData/Descriptions/AttributeRouteStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using System.Web.OData.Routing;
using System.Web.OData.Routing.Conventions;
using System.Web.OData.Routing.Template;
using Flurl;
using Microsoft.Extensions.DependencyInjection;

namespace Swashbuckle.OData.Descriptions
Expand Down Expand Up @@ -55,7 +54,7 @@ private static ODataActionDescriptor GetODataActionDescriptorFromAttributeRoute(
var odataRouteAttribute = actionDescriptor.GetCustomAttributes<ODataRouteAttribute>()?.FirstOrDefault();

Contract.Assume(odataRouteAttribute != null);
var pathTemplate = HttpUtility.UrlDecode(oDataRoute.GetRoutePrefix().AppendPathSegment(GetODataPathTemplate(odataRoutePrefixAttribute?.Prefix, odataRouteAttribute.PathTemplate)));
var pathTemplate = HttpUtility.UrlDecode(oDataRoute.GetRoutePrefix().AppendUriSegment(GetODataPathTemplate(odataRoutePrefixAttribute?.Prefix, odataRouteAttribute.PathTemplate)));
Contract.Assume(pathTemplate != null);

return new ODataActionDescriptor(actionDescriptor, oDataRoute, pathTemplate, CreateHttpRequestMessage(actionDescriptor, oDataRoute, httpConfig));
Expand Down
3 changes: 1 addition & 2 deletions Swashbuckle.OData/Descriptions/OperationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using Flurl;
using Swashbuckle.Swagger;

namespace Swashbuckle.OData.Descriptions
Expand Down Expand Up @@ -30,7 +29,7 @@ public static string GenerateSampleODataUri(this Operation operation, string ser

return new UriTemplate(pathTemplate).BindByName(prefix, parameters).ToString();
}
return serviceRoot.AppendPathSegment(pathTemplate);
return serviceRoot.AppendUriSegment(pathTemplate);
}

public static IList<Parameter> Parameters(this Operation operation)
Expand Down
3 changes: 1 addition & 2 deletions Swashbuckle.OData/Descriptions/SwaggerRoute.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Diagnostics.Contracts;
using System.Web;
using System.Web.OData.Routing;
using Flurl;
using Swashbuckle.Swagger;

namespace Swashbuckle.OData.Descriptions
Expand Down Expand Up @@ -44,7 +43,7 @@ public string PrefixedTemplate
get
{
Contract.Ensures(!string.IsNullOrWhiteSpace(Contract.Result<string>()));
return HttpUtility.UrlDecode(ODataRoute.GetRoutePrefix().AppendPathSegment(_template));
return HttpUtility.UrlDecode(ODataRoute.GetRoutePrefix().AppendUriSegment(_template));
}
}

Expand Down
58 changes: 58 additions & 0 deletions Swashbuckle.OData/Descriptions/UriExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Globalization;

namespace Swashbuckle.OData.Descriptions
{
internal static class UriExtensions
{
/// <summary>
/// Appends a segment to provided URI
/// </summary>
/// <param name="uri">URI to append a segment to</param>
/// <param name="segment">A segment to append</param>
/// <returns>Adjusted URI</returns>
public static string AppendUriSegment(this string uri, string segment)
{
if (segment == null)
throw new ArgumentNullException(nameof(segment));

// Disassemble provided URI into major parts
var parts = uri.Split('?');
string uriQueryAndFragment = (parts.Length == 2) ? parts[1] : "";
string uriPath = parts[0];

// define URL separator character
char pathSeparator = '/';

// make sure the segment value gets properly encoded
var encodedSegment = EncodeUri(segment.Trim(pathSeparator).ToString(CultureInfo.InvariantCulture)).Replace("?", "%3F");

// add segment to the base URI path
uriPath = $"{uriPath}{pathSeparator}{encodedSegment}";

return ConstructUri(uriPath, uriQueryAndFragment);
}

private static string EncodeUri(string urlPart)
{
var unescaped = Uri.UnescapeDataString(urlPart);
return Uri.EscapeUriString(unescaped);
}

/// <summary>
/// Constructs URI from provided parts
/// </summary>
/// <param name="path">URI path</param>
/// <param name="query">URI query string</param>
/// <param name="fragment">URI fragment</param>
/// <returns>URI value</returns>
private static string ConstructUri(string path, string query)
{
var sb = new System.Text.StringBuilder(path);
if (!string.IsNullOrWhiteSpace(query))
sb.Append("?").Append(query);
return sb.ToString();
}

}
}
2 changes: 1 addition & 1 deletion Swashbuckle.OData/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@

[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("3.2.0")]
[assembly: AssemblyInformationalVersion("3.2.1")]
5 changes: 1 addition & 4 deletions Swashbuckle.OData/Swashbuckle.OData.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,6 @@
<AssemblyOriginatorKeyFile>Swashbuckle.Odata.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="Flurl, Version=2.0.0.0, Culture=neutral, PublicKeyToken=1308302a96879dfb, processorArchitecture=MSIL">
<HintPath>..\packages\Flurl.Signed.2.0.0\lib\portable-net40+win+wpa81+wp80+MonoAndroid10+MonoTouch10\Flurl.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Extensions.DependencyInjection, Version=1.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Extensions.DependencyInjection.1.0.0\lib\netstandard1.1\Microsoft.Extensions.DependencyInjection.dll</HintPath>
<Private>True</Private>
Expand Down Expand Up @@ -187,6 +183,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="CollectionExtentions.cs" />
<Compile Include="Descriptions\UriExtensions.cs" />
<Compile Include="ODataListResponse.cs" />
<Compile Include="ODataSwaggerDocsConfig.cs" />
<Compile Include="Descriptions\ApiDescriptionExtensions.cs" />
Expand Down
1 change: 0 additions & 1 deletion Swashbuckle.OData/packages.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Flurl.Signed" version="2.0.0" allowedVersions="[2.0.0,)" targetFramework="net452" />
<package id="Microsoft.AspNet.OData" version="6.0.0" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.Client" allowedVersions="(,6.0.0)" version="5.2.3" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.Core" allowedVersions="(,6.0.0)" version="5.2.3" targetFramework="net452" />
Expand Down

0 comments on commit 0d96a0c

Please sign in to comment.