Skip to content

Commit

Permalink
Merge pull request #113 from will-chan-sage/master
Browse files Browse the repository at this point in the history
Support for Enums types as keys. Closes #108
  • Loading branch information
rbeauchamp authored Oct 17, 2016
2 parents f8c055e + 4fdc92e commit e544b4f
Show file tree
Hide file tree
Showing 15 changed files with 676 additions and 18 deletions.
33 changes: 33 additions & 0 deletions Swashbuckle.OData.Sample/App_Start/ODataConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ private static void ConfigureWebApiOData(HttpConfiguration config)

// Define a default non- versioned route(default route should be at the end as a last catch-all)
config.MapODataServiceRoute("DefaultODataRoute", ODataRoutePrefix, GetDefaultModel());

// Define a route with an enum as a key
config.MapODataServiceRoute("EnumODataRoute",
ODataRoutePrefix,
GetProductWithEnumKeyModel());

// Define a route with an enum/int composite key
config.MapODataServiceRoute("EnumIntCompositeODataRoute",
ODataRoutePrefix,
GetProductWithCompositeEnumIntKeyModel());
}

private static IEdmModel GetDefaultModel()
Expand Down Expand Up @@ -174,5 +184,28 @@ private static IEdmModel GetFunctionsEdmModel()

return builder.GetEdmModel();
}

#region Enum Routes
private static IEdmModel GetProductWithEnumKeyModel()
{
var builder = new ODataConventionModelBuilder();
builder.EnableLowerCamelCase();

builder.EntitySet<ProductWithEnumKey>("ProductWithEnumKeys");

return builder.GetEdmModel();
}

private static IEdmModel GetProductWithCompositeEnumIntKeyModel()
{
var builder = new ODataConventionModelBuilder();
builder.EnableLowerCamelCase();

builder.EntitySet<ProductWithCompositeEnumIntKey>
("ProductWithCompositeEnumIntKeys");

return builder.GetEdmModel();
}
#endregion
}
}
7 changes: 6 additions & 1 deletion Swashbuckle.OData.Sample/App_Start/WebApiConfig.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
using System.Web.Http;
using System.Web.Configuration;
using System.Web.Http;
using System.Web.OData.Extensions;

namespace SwashbuckleODataSample
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
bool isPrefixFreeEnabled = System.Convert.ToBoolean(
WebConfigurationManager.AppSettings["EnableEnumPrefixFree"]);
config.EnableEnumPrefixFree(isPrefixFreeEnabled);
config.MapHttpAttributeRoutes();

config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}", new { id = RouteParameter.Optional });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IAp
new Tag { name = "Orders", description = "an ODataController resource" },
new Tag { name = "CustomersV1", description = "a versioned ODataController resource" },
new Tag { name = "Users", description = "a RESTier resource" },
new Tag { name = "Products", description = "demonstrates OData functions and actions" }
new Tag { name = "Products", description = "demonstrates OData functions and actions" },
new Tag { name = "ProductWithCompositeEnumIntKeys", description = "demonstrates composite keys with an enum as a key" },
new Tag { name = "ProductWithEnumKeys", description = "demonstrates use of enum as a key" },
};
}
}
Expand Down
27 changes: 27 additions & 0 deletions Swashbuckle.OData.Sample/Models/ProductWithEnumKey.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.ComponentModel.DataAnnotations;

namespace SwashbuckleODataSample.Models
{
public class ProductWithEnumKey
{
[Key]
public MyEnum EnumValue { get; set; }

public string Name { get; set; }

public double Price { get; set; }
}

public class ProductWithCompositeEnumIntKey
{
[Key]
public MyEnum EnumValue { get; set; }

[Key]
public int Id { get; set; }

public string Name { get; set; }

public double Price { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using System.Web.OData;
using SwashbuckleODataSample.Models;
using System;

namespace SwashbuckleODataSample.ODataControllers
{
public class ProductWithCompositeEnumIntKeysController : ODataController
{
private static readonly List<ProductWithCompositeEnumIntKey> DataCompositeKey;

static ProductWithCompositeEnumIntKeysController()
{
DataCompositeKey = new List<ProductWithCompositeEnumIntKey>()
{
{
new ProductWithCompositeEnumIntKey
{
EnumValue = MyEnum.ValueOne,
Id = 1,
Name = "ValueOneName",
Price = 101
}
},
{
new ProductWithCompositeEnumIntKey
{
EnumValue = MyEnum.ValueTwo,
Id = 2,
Name = "ValueTwoName",
Price = 102
}
}
};
}

/// <summary>
/// Query products
/// </summary>
[EnableQuery]
public IQueryable<ProductWithCompositeEnumIntKey> Get()
{
return DataCompositeKey.AsQueryable();
}

/// <summary>
/// Query products by keys
/// </summary>
/// <param name="keyenumValue">key enum value</param>
/// <param name="keyid">key id</param>
/// <returns>composite enum-int key model</returns>
[EnableQuery]
public IHttpActionResult Get([FromODataUri]MyEnum keyenumValue, [FromODataUri]int keyid)
{
return Ok(DataCompositeKey
.Where(x => x.EnumValue == keyenumValue
&& x.Id == keyid));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using System.Web.OData;
using SwashbuckleODataSample.Models;

namespace SwashbuckleODataSample.ODataControllers
{
public class ProductWithEnumKeysController : ODataController
{
private static readonly Dictionary<MyEnum,
ProductWithEnumKey> DataEnumAsKey;

static ProductWithEnumKeysController()
{
DataEnumAsKey = new Dictionary<MyEnum, ProductWithEnumKey>()
{
{
MyEnum.ValueOne,
new ProductWithEnumKey {
EnumValue = MyEnum.ValueOne,
Name = "ValueOneName",
Price = 101
}
},
{
MyEnum.ValueTwo,
new ProductWithEnumKey
{
EnumValue = MyEnum.ValueTwo,
Name = "ValueTwoName",
Price = 102
}
}
};
}

/// <summary>
/// Query products
/// </summary>
[HttpGet]
[EnableQuery]
public IQueryable<ProductWithEnumKey> Get()
{
return DataEnumAsKey.Values.AsQueryable();
}

/// <summary>
/// Query product by enum key
/// </summary>
/// <param name="Key">key enum value</param>
/// <returns>project enum model</returns>
[HttpGet]
public IHttpActionResult Get([FromODataUri]MyEnum Key)
{
return Ok(DataEnumAsKey[Key]);
}
}
}
3 changes: 3 additions & 0 deletions Swashbuckle.OData.Sample/Swashbuckle.OData.Sample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@
<Compile Include="DocumentFilters\ApplyResourceDocumentation.cs" />
<Compile Include="Models\MyEnum.cs" />
<Compile Include="Models\Product.cs" />
<Compile Include="Models\ProductWithEnumKey.cs" />
<Compile Include="ODataControllers\ProductWithCompositeEnumIntKeysController.cs" />
<Compile Include="ODataControllers\ProductWithEnumKeysController.cs" />
<Compile Include="ODataControllers\ProductsController.cs" />
<Compile Include="Repositories\RestierODataContext.cs" />
<Compile Include="Models\User.cs" />
Expand Down
3 changes: 3 additions & 0 deletions Swashbuckle.OData.Sample/Web.config
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,7 @@
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
<appSettings>
<add key="EnableEnumPrefixFree" value="false" />
</appSettings>
</configuration>
Loading

0 comments on commit e544b4f

Please sign in to comment.