Skip to content

Commit

Permalink
Merge pull request #16 from rbeauchamp/feature/9
Browse files Browse the repository at this point in the history
Implemented explicit support for RESTier
  • Loading branch information
Richard Beauchamp committed Dec 10, 2015
2 parents b7b3184 + f69559c commit 712bc4e
Show file tree
Hide file tree
Showing 46 changed files with 2,579 additions and 186 deletions.
4 changes: 2 additions & 2 deletions Swashbuckle.OData.Nuget/Swashbuckle.OData.NuGet.nuproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
<Owners>Richard Beauchamp</Owners>
<Summary>Extends Swashbuckle with WebApi OData v4 support!</Summary>
<Description>Extends Swashbuckle with WebApi OData v4 support!</Description>
<ReleaseNotes>Supports versioned OData routes, supports customization of generated swagger docs via SwaggerConfig, Verifies entity data model against the OData API, Customize output with ApiExplorer-supported attributes</ReleaseNotes>
<ReleaseNotes>Supports RESTier</ReleaseNotes>
<ProjectUrl>https://github.com/rbeauchamp/Swashbuckle.OData</ProjectUrl>
<LicenseUrl>https://github.com/rbeauchamp/Swashbuckle.OData/blob/master/License.txt</LicenseUrl>
<Copyright>Copyright 2015</Copyright>
<Tags>Swashbuckle Swagger SwaggerUi OData Documentation Discovery Help WebApi AspNet AspNetWebApi Docs WebHost IIS</Tags>
<Version>2.2.2</Version>
<Version>2.3.0</Version>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Swashbuckle.OData\Swashbuckle.OData.csproj" />
Expand Down
2 changes: 1 addition & 1 deletion Swashbuckle.OData.Sample/ApiControllers/Client.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Collections.Generic;

namespace SwashbuckleODataSample.Models
namespace SwashbuckleODataSample.ApiControllers
{
public class Client
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Web.Http;
using System.Web.Http.Description;
using SwashbuckleODataSample.Models;
using SwashbuckleODataSample.Repositories;

namespace SwashbuckleODataSample.ApiControllers
{
Expand Down
2 changes: 1 addition & 1 deletion Swashbuckle.OData.Sample/ApiControllers/Project.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Web.OData.Builder;
using Microsoft.OData.Edm;

namespace SwashbuckleODataSample.Models
namespace SwashbuckleODataSample.ApiControllers
{
public class Project
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Web.Http;
using System.Web.Http.Description;
using SwashbuckleODataSample.Models;
using SwashbuckleODataSample.Repositories;

namespace SwashbuckleODataSample.ApiControllers
{
Expand Down
17 changes: 16 additions & 1 deletion Swashbuckle.OData.Sample/App_Start/ODataConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
using System.Web.OData.Builder;
using System.Web.OData.Extensions;
using Microsoft.OData.Edm;
using Microsoft.Restier.EntityFramework;
using Microsoft.Restier.WebApi;
using Microsoft.Restier.WebApi.Batch;
using SwashbuckleODataSample.Models;
using SwashbuckleODataSample.Repositories;
using SwashbuckleODataSample.Versioning;

namespace SwashbuckleODataSample
Expand All @@ -13,9 +17,20 @@ public static class ODataConfig
public const string ODataRoutePrefix = "odata";

public static void Register(HttpConfiguration config)
{
ConfigureWebApiOData(config);
ConfigureRestierOData(config);
}

private static async void ConfigureRestierOData(HttpConfiguration config)
{
await config.MapRestierRoute<DbApi<RestierODataContext>>("RESTierRoute", "restier", new RestierBatchHandler(GlobalConfiguration.DefaultServer));
}

private static void ConfigureWebApiOData(HttpConfiguration config)
{
var controllerSelector = new ODataVersionControllerSelector(config);
config.Services.Replace(typeof(IHttpControllerSelector), controllerSelector);
config.Services.Replace(typeof (IHttpControllerSelector), controllerSelector);

// Define a versioned route
config.MapODataServiceRoute("V1RouteVersioning", "odata/v1", GetModel());
Expand Down
File renamed without changes.
File renamed without changes.
9 changes: 9 additions & 0 deletions Swashbuckle.OData.Sample/Models/User.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace SwashbuckleODataSample.Models
{
public class User
{
public int Id { get; set; }

public string Name { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
using System.Web.Http.Description;
using System.Web.OData;
using SwashbuckleODataSample.Models;
using SwashbuckleODataSample.Repositories;

namespace SwashbuckleODataSample.Controllers
namespace SwashbuckleODataSample.ODataControllers
{
public class CustomersController : ODataController
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
using System.Web.Http;
using System.Web.OData;
using SwashbuckleODataSample.Models;
using SwashbuckleODataSample.Repositories;

namespace SwashbuckleODataSample.Controllers
namespace SwashbuckleODataSample.ODataControllers
{
public class CustomersV1Controller : ODataController
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
using System.Web.Http.Description;
using System.Web.OData;
using SwashbuckleODataSample.Models;
using SwashbuckleODataSample.Repositories;
using SwashbuckleODataSample.Utils;

namespace SwashbuckleODataSample.Controllers
namespace SwashbuckleODataSample.ODataControllers
{
public class OrdersController : ODataController
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
using System.Web.Http;
using System.Web.OData;
using SwashbuckleODataSample.Models;
using SwashbuckleODataSample.Repositories;

namespace SwashbuckleODataSample.Controllers
namespace SwashbuckleODataSample.ODataControllers
{
public class OrdersV1Controller : ODataController
{
Expand Down
31 changes: 31 additions & 0 deletions Swashbuckle.OData.Sample/Repositories/RestierODataContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Data.Entity;
using SwashbuckleODataSample.Models;

namespace SwashbuckleODataSample.Repositories
{
public class RestierODataContext : DbContext
{
static RestierODataContext()
{
Database.SetInitializer(new RestierODataInitializer());
}


public RestierODataContext() : base("name=RestierODataContext")
{
}

public DbSet<User> Users { get; set; }
}

public class RestierODataInitializer : DropCreateDatabaseAlways<RestierODataContext>
{
protected override void Seed(RestierODataContext context)
{
context.Users.Add(new User { Name = "UserOne" });
context.Users.Add(new User { Name = "UserTwo" });

base.Seed(context);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System.Data.Entity;
using SwashbuckleODataSample.ApiControllers;
using SwashbuckleODataSample.Models;
using SwashbuckleODataSample.Utils;

namespace SwashbuckleODataSample.Models
namespace SwashbuckleODataSample.Repositories
{
public class SwashbuckleODataContext : DbContext
{
Expand Down
22 changes: 18 additions & 4 deletions Swashbuckle.OData.Sample/Swashbuckle.OData.Sample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@
<HintPath>..\packages\Microsoft.OData.Edm.6.13.0\lib\portable-net40+sl5+wp8+win8+wpa\Microsoft.OData.Edm.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Restier.Core, Version=0.4.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Restier.Core.0.4.0-rc2\lib\net45\Microsoft.Restier.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Restier.EntityFramework, Version=0.4.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Restier.EntityFramework.0.4.0-rc2\lib\net45\Microsoft.Restier.EntityFramework.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Restier.WebApi, Version=0.4.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Restier.WebApi.0.4.0-rc2\lib\net45\Microsoft.Restier.WebApi.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Spatial, Version=6.13.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Spatial.6.13.0\lib\portable-net40+sl5+wp8+win8+wpa\Microsoft.Spatial.dll</HintPath>
<Private>True</Private>
Expand Down Expand Up @@ -127,18 +139,20 @@
<Compile Include="App_Start\SwaggerConfig.cs" />
<Compile Include="App_Start\ODataConfig.cs" />
<Compile Include="App_Start\WebApiConfig.cs" />
<Compile Include="ODataControllers\Customer.cs" />
<Compile Include="Repositories\RestierODataContext.cs" />
<Compile Include="Models\User.cs" />
<Compile Include="Models\Customer.cs" />
<Compile Include="ODataControllers\CustomersV1Controller.cs" />
<Compile Include="ODataControllers\CustomersController.cs" />
<Compile Include="ODataControllers\Order.cs" />
<Compile Include="Models\Order.cs" />
<Compile Include="ODataControllers\OrdersV1Controller.cs" />
<Compile Include="ODataControllers\OrdersController.cs" />
<Compile Include="Global.asax.cs">
<DependentUpon>Global.asax</DependentUpon>
</Compile>
<Compile Include="ODataControllers\SwashbuckleODataContext.cs" />
<Compile Include="Repositories\SwashbuckleODataContext.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SequentialGuidGenerator.cs" />
<Compile Include="Utils\SequentialGuidGenerator.cs" />
<Compile Include="Versioning\ModelValidationFilterAttribute.cs" />
<Compile Include="Versioning\ODataHelper.cs" />
<Compile Include="Versioning\ODataVersionControllerSelector.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Security.Cryptography;

namespace SwashbuckleODataSample
namespace SwashbuckleODataSample.Utils
{
/// <summary>
/// Generates <see cref="System.Guid" /> values using strategy from Jeremy Todd.
Expand Down
4 changes: 3 additions & 1 deletion Swashbuckle.OData.Sample/Web.config
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --></configSections>
<connectionStrings>
<add name="SwashbuckleODataContext" connectionString="Data Source=(localdb)\MSSQLLocalDB; Initial Catalog=SwashbuckleODataContext-20151117164947; Integrated Security=True; MultipleActiveResultSets=True; AttachDbFilename=|DataDirectory|SwashbuckleODataContext-20151117164947.mdf" providerName="System.Data.SqlClient" />
<add name="RestierODataContext" connectionString="Data Source=(localdb)\MSSQLLocalDB; Initial Catalog=RestierODataContext-20151117164947; Integrated Security=True; MultipleActiveResultSets=True; AttachDbFilename=|DataDirectory|RestierODataContext-20151117164947.mdf" providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.5.2" />
Expand Down Expand Up @@ -45,4 +46,5 @@
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer></configuration>
</system.webServer>
</configuration>
4 changes: 4 additions & 0 deletions Swashbuckle.OData.Sample/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
<package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net452" />
<package id="Microsoft.OData.Core" version="6.13.0" targetFramework="net452" />
<package id="Microsoft.OData.Edm" version="6.13.0" targetFramework="net452" />
<package id="Microsoft.Restier" version="0.4.0-rc2" targetFramework="net452" />
<package id="Microsoft.Restier.Core" version="0.4.0-rc2" targetFramework="net452" />
<package id="Microsoft.Restier.EntityFramework" version="0.4.0-rc2" targetFramework="net452" />
<package id="Microsoft.Restier.WebApi" version="0.4.0-rc2" targetFramework="net452" />
<package id="Microsoft.Spatial" version="6.13.0" targetFramework="net452" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net452" />
<package id="Newtonsoft.Json" version="7.0.1" targetFramework="net452" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Swashbuckle.OData.Tests
{
public class ApplyDocumentVendorExtensions : IDocumentFilter
public class ApplyNewHostName : IDocumentFilter
{
public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer)
{
Expand Down
34 changes: 33 additions & 1 deletion Swashbuckle.OData.Tests/Fixtures/ODataSwaggerProviderTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.Owin.Hosting;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Schema;
using NUnit.Framework;
using Swashbuckle.Application;
using Swashbuckle.OData.Tests.WebHost;
Expand All @@ -16,7 +20,7 @@ public class ODataSwaggerProviderTests
public async Task It_applies_document_filters()
{
// Arrange
Action<SwaggerDocsConfig> config = c => c.DocumentFilter<ApplyDocumentVendorExtensions>();
Action<SwaggerDocsConfig> config = c => c.DocumentFilter<ApplyNewHostName>();
var httpClient = HttpClientUtils.GetHttpClient();

using (WebApp.Start(TestWebApiStartup.BaseAddress, appBuilder => new TestWebApiStartup().Configuration(appBuilder, config)))
Expand Down Expand Up @@ -84,5 +88,33 @@ public async Task It_explores_the_correct_controller()
defaultCustomerController.put.Should().NotBeNull();
}
}

[Test]
public async Task It_generates_valid_swagger_2_0_json()
{
using (WebApp.Start(TestWebApiStartup.BaseAddress, appBuilder => new TestWebApiStartup().Configuration(appBuilder)))
{
// Arrange
var httpClient = HttpClientUtils.GetHttpClient();

// Act
var response = await httpClient.GetAsync("swagger/docs/v1");

// Assert
await response.ValidateSuccessAsync();
var swaggerJson = await response.Content.ReadAsStringAsync();

var resolver = new JSchemaPreloadedResolver();
resolver.Add(new Uri("http://json-schema.org/draft-04/schema"), File.ReadAllText(@"schema-draft-v4.json"));

var swaggerSchema = File.ReadAllText(@"swagger-2.0-schema.json");
var schema = JSchema.Parse(swaggerSchema, resolver);

var swaggerJObject = JObject.Parse(swaggerJson);
IList<string> messages;
var isValid = swaggerJObject.IsValid(schema, out messages);
isValid.Should().BeTrue();
}
}
}
}
Loading

0 comments on commit 712bc4e

Please sign in to comment.