-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from rbeauchamp/feature/31
Provide single quotes around swagger path parameters of type string
- Loading branch information
Showing
11 changed files
with
144 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
Swashbuckle.OData.Tests/Fixtures/StringTypeUrlParamTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using System.Web.Http.Dispatcher; | ||
using System.Web.OData.Builder; | ||
using System.Web.OData.Extensions; | ||
using FluentAssertions; | ||
using Microsoft.OData.Edm; | ||
using Microsoft.Owin.Hosting; | ||
using NUnit.Framework; | ||
using Owin; | ||
using Swashbuckle.Swagger; | ||
using SwashbuckleODataSample; | ||
using SwashbuckleODataSample.Models; | ||
|
||
namespace Swashbuckle.OData.Tests | ||
{ | ||
[TestFixture] | ||
public class StringTypeUrlParamTests | ||
{ | ||
[Test] | ||
public async Task It_wraps_string_type_url_params_with_single_quotes() | ||
{ | ||
using (WebApp.Start(HttpClientUtils.BaseAddress, appBuilder => Configuration(appBuilder, typeof(ProductsV1Controller)))) | ||
{ | ||
// Arrange | ||
var httpClient = HttpClientUtils.GetHttpClient(HttpClientUtils.BaseAddress, ODataConfig.ODataRoutePrefix); | ||
|
||
// Act | ||
var swaggerDocument = await httpClient.GetJsonAsync<SwaggerDocument>("swagger/docs/v1"); | ||
|
||
// Assert | ||
PathItem pathItem; | ||
swaggerDocument.paths.TryGetValue("/odata/v1/Products({Id})/Default.CalculateGeneralSalesTax(state='{state}')", out pathItem); | ||
pathItem.Should().NotBeNull(); | ||
pathItem.get.Should().NotBeNull(); | ||
} | ||
} | ||
|
||
private static void Configuration(IAppBuilder appBuilder, Type targetController) | ||
{ | ||
var config = appBuilder.GetStandardHttpConfig(targetController); | ||
|
||
var controllerSelector = new UnitTestODataVersionControllerSelector(config, targetController); | ||
config.Services.Replace(typeof(IHttpControllerSelector), controllerSelector); | ||
|
||
// Define a route to a controller class that contains functions | ||
config.MapODataServiceRoute("FunctionsODataRoute", "odata/v1", GetFunctionsEdmModel()); | ||
controllerSelector.RouteVersionSuffixMapping.Add("FunctionsODataRoute", "V1"); | ||
|
||
config.EnsureInitialized(); | ||
} | ||
|
||
private static IEdmModel GetFunctionsEdmModel() | ||
{ | ||
ODataModelBuilder builder = new ODataConventionModelBuilder(); | ||
|
||
builder.EntitySet<Product>("Products"); | ||
|
||
var productType = builder.EntityType<Product>(); | ||
|
||
// Function bound to a collection | ||
// Returns the most expensive product, a single entity | ||
productType.Collection | ||
.Function("MostExpensive") | ||
.Returns<double>(); | ||
|
||
// Function bound to a collection | ||
// Returns the top 10 product, a collection | ||
productType.Collection | ||
.Function("Top10") | ||
.ReturnsCollectionFromEntitySet<Product>("Products"); | ||
|
||
// Function bound to a single entity | ||
// Returns the instance's price rank among all products | ||
productType | ||
.Function("GetPriceRank") | ||
.Returns<int>(); | ||
|
||
// Function bound to a single entity | ||
// Accept a string as parameter and return a double | ||
// This function calculate the general sales tax base on the | ||
// state | ||
productType | ||
.Function("CalculateGeneralSalesTax") | ||
.Returns<double>() | ||
.Parameter<string>("state"); | ||
|
||
// Unbound Function | ||
builder.Function("GetSalesTaxRate") | ||
.Returns<double>() | ||
.Parameter<string>("state"); | ||
|
||
return builder.GetEdmModel(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
Swashbuckle.OData/Descriptions/ApiDescriptionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System.Web.Http.Description; | ||
using Swashbuckle.Swagger; | ||
|
||
namespace Swashbuckle.OData.Descriptions | ||
{ | ||
public static class ApiDescriptionExtensions | ||
{ | ||
public static string GetRelativePathWithQuotedStringParams(this ApiDescription apiDescription) | ||
{ | ||
var parameters = apiDescription.ParameterDescriptions; | ||
|
||
var newRelativePathSansQueryString = apiDescription.RelativePathSansQueryString(); | ||
|
||
foreach (var parameter in parameters) | ||
{ | ||
if (newRelativePathSansQueryString.Contains("{" + parameter.Name + "}") && parameter.ParameterDescriptor.ParameterType == typeof(string)) | ||
{ | ||
newRelativePathSansQueryString = newRelativePathSansQueryString.Replace("{" + parameter.Name + "}", "\'{" + parameter.Name + "}\'"); | ||
} | ||
} | ||
|
||
return apiDescription.RelativePath.Replace(apiDescription.RelativePathSansQueryString(), newRelativePathSansQueryString); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters