Skip to content

Commit

Permalink
Merge pull request #132 from cibergarri/master
Browse files Browse the repository at this point in the history
Add support for xmlComments and for using swaggerResponseAttributes a…
  • Loading branch information
rbeauchamp authored Mar 15, 2017
2 parents ae5053b + 12966c5 commit 50e8caa
Show file tree
Hide file tree
Showing 27 changed files with 472 additions and 124 deletions.
4 changes: 2 additions & 2 deletions License.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Certain portions of this code is Copyright (c) 2013, Richard Morris - All rights reserved.

For those portions, the following license applies:
For those portions, the following license applies:

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

Expand All @@ -34,4 +34,4 @@ Redistribution and use in source and binary forms, with or without modification,

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHA
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
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.1.0</Version>
<Version>3.1.1</Version>
<Title>Swashbuckle.OData</Title>
<Authors>Richard Beauchamp</Authors>
<Owners>Richard Beauchamp</Owners>
Expand Down
12 changes: 12 additions & 0 deletions Swashbuckle.OData.Sample/ApiControllers/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,24 @@

namespace SwashbuckleODataSample.ApiControllers
{
/// <summary>
/// Client Comment
/// </summary>
public class Client
{
/// <summary>
/// Client ID
/// </summary>
public int Id { get; set; }

/// <summary>
/// Client Name
/// </summary>
public string Name { get; set; }

/// <summary>
/// Client Projects
/// </summary>
public IList<Project> Projects { get; set; }
}
}
2 changes: 1 addition & 1 deletion Swashbuckle.OData.Sample/App_Start/SwaggerConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public static void Register()
{
// Set this flag to include navigation properties in your entity swagger models
//
//odataConfig.IncludeNavigationProperties();
odataConfig.IncludeNavigationProperties();
}));
})
.EnableSwaggerUi(c =>
Expand Down
19 changes: 18 additions & 1 deletion Swashbuckle.OData.Sample/Models/Customer.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;

namespace SwashbuckleODataSample.Models
{
/// <summary>
/// Customer comment
/// </summary>
public class Customer
{
public Customer()
{
Orders = new HashSet<Order>();
}
/// <summary>
/// Customer Id
/// </summary>
public int Id { get; set; }

/// <summary>
/// customer Name
/// </summary>
public string Name { get; set; }

public IList<Order> Orders { get; set; }
/// <summary>
/// Customer Orders
/// </summary>
public virtual ICollection<Order> Orders { get; }
}
}
5 changes: 2 additions & 3 deletions Swashbuckle.OData.Sample/Models/Order.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ public class Order

public double UnitPrice { get; set; }

[ForeignKey("Customer")]
public int CustomerId { get; set; }

[ForeignKey("CustomerId")]
[ActionOnDelete(EdmOnDeleteAction.Cascade)]
public Customer Customer { get; set; }
public virtual Customer Customer { get; set; }
}
}
15 changes: 10 additions & 5 deletions Swashbuckle.OData.Sample/ODataControllers/CustomersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
using System.Web.OData;
using SwashbuckleODataSample.Models;
using SwashbuckleODataSample.Repositories;
using System.Data.Entity;
using Swashbuckle.Swagger.Annotations;
using Swashbuckle.OData;
using System.Web.OData.Routing;

namespace SwashbuckleODataSample.ODataControllers
{
Expand All @@ -17,10 +21,12 @@ public class CustomersController : ODataController
/// <summary>
/// Query customers
/// </summary>
[EnableQuery]
public IQueryable<Customer> GetCustomers()
[EnableQuery(PageSize = 5)]
[SwaggerResponse(HttpStatusCode.OK, Type= typeof(ODataListResponse<Customer>))]
public async Task<IHttpActionResult> GetCustomers()
{
return _db.Customers;
var customers = await _db.Customers.ToListAsync();
return Ok(customers);
}

/// <summary>
Expand Down Expand Up @@ -152,8 +158,7 @@ public async Task<IHttpActionResult> Delete([FromODataUri] int key)
[EnableQuery]
public IQueryable<Order> GetOrders([FromODataUri] int key)
{
return _db.Customers.Where(m => m.Id == key)
.SelectMany(m => m.Orders);
return _db.Orders.Where(o => o.CustomerId == key);
}

/// <summary>
Expand Down
23 changes: 19 additions & 4 deletions Swashbuckle.OData.Sample/ODataControllers/OrdersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using SwashbuckleODataSample.Models;
using SwashbuckleODataSample.Repositories;
using SwashbuckleODataSample.Utils;
using System.Collections.Generic;

namespace SwashbuckleODataSample.ODataControllers
{
Expand All @@ -20,11 +21,22 @@ public class OrdersController : ODataController
/// Query orders
/// </summary>
[EnableQuery]
public IQueryable<Order> GetOrders()
public IQueryable<Order> Get()
{
return _db.Orders;
}


/// <summary>
/// Query orders by Customer Id
/// </summary>
[EnableQuery]
public ICollection<Order> Get([FromODataUri]int customerId)
{
return _db.Orders.Where(o => o.CustomerId == customerId).ToList();
}


/// <summary>
/// An example of a custom route. Create a new order for the customer with the given id
/// </summary>
Expand Down Expand Up @@ -54,7 +66,7 @@ public async Task<IHttpActionResult> Post([FromODataUri] int customerId, Order o
[EnableQuery]
public SingleResult<Order> GetOrder([FromODataUri] Guid key)
{
return SingleResult.Create(_db.Orders.Where(order => order.OrderId == key));
return SingleResult.Create(_db.Orders.Where(order => order.OrderId ==key));
}

/// <summary>
Expand Down Expand Up @@ -139,8 +151,11 @@ public async Task<IHttpActionResult> Delete([FromODataUri] Guid key)
[EnableQuery]
public SingleResult<Customer> GetCustomer([FromODataUri] Guid key)
{
return SingleResult.Create(_db.Orders.Where(m => m.OrderId == key)
.Select(m => m.Customer));
var custId = _db.Orders.Where(m => m.OrderId == key)
.Select(m => m.CustomerId).First();
return SingleResult.Create(_db.Customers.Where(c => c.Id == custId));
//return SingleResult.Create(_db.Orders.Where(m => m.OrderId == key)
// .Select(m => m.Customer));
}

protected override void Dispose(bool disposing)
Expand Down
92 changes: 88 additions & 4 deletions Swashbuckle.OData.Sample/Repositories/SwashbuckleODataContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ static SwashbuckleODataContext()
Database.SetInitializer<SwashbuckleODataContext>(null);
}


public SwashbuckleODataContext() : base("name=SwashbuckleODataContext")
{
Customers = new TestDbSet<Customer>();
Expand All @@ -34,10 +33,95 @@ private static void Seed(SwashbuckleODataContext context)

var customerOne = new Customer { Id = 1, Name = "CustomerOne" };
context.Customers.Add(customerOne);
context.Customers.Add(new Customer { Id = 2, Name = "CustomerTwo" });
context.Orders.Add(new Order {
OrderId = new System.Guid("ce37ae8d-4efe-2d5f-10a0-39ddd2436a52"),
OrderName = "OrderOne",
CustomerId = 1,
UnitPrice = 4.0
});
context.Orders.Add(new Order {
OrderId = new System.Guid("03b20510-a693-9504-b040-39ddd2436a52"),
OrderName = "OrderTwo",
CustomerId = 1,
UnitPrice = 3.5
});

var customerTwo = new Customer { Id = 2, Name = "CustomerTwo" };
context.Customers.Add(customerTwo);
context.Orders.Add(new Order {
OrderId = SequentialGuidGenerator.Generate(SequentialGuidType.SequentialAtEnd),
OrderName = "OrderOne",
CustomerId = customerTwo.Id,
UnitPrice = 4.0
});
context.Orders.Add(new Order {
OrderId = SequentialGuidGenerator.Generate(SequentialGuidType.SequentialAtEnd),
OrderName = "OrderTwo",
CustomerId = customerTwo.Id,
UnitPrice = 3.5
});

var customerThree = new Customer { Id = 3, Name = "CustomerThree" };
context.Customers.Add(customerThree);
context.Orders.Add(new Order {
OrderId = SequentialGuidGenerator.Generate(SequentialGuidType.SequentialAtEnd),
OrderName = "OrderOne",
CustomerId = customerThree.Id,
UnitPrice = 4.0
});
context.Orders.Add(new Order {
OrderId = SequentialGuidGenerator.Generate(SequentialGuidType.SequentialAtEnd),
OrderName = "OrderTwo",
CustomerId = customerThree.Id,
UnitPrice = 3.5
});

var customerFour = new Customer { Id = 4, Name = "CustomerFour" };
context.Customers.Add(customerFour);
context.Orders.Add(new Order {
OrderId = SequentialGuidGenerator.Generate(SequentialGuidType.SequentialAtEnd),
OrderName = "OrderOne",
CustomerId = customerFour.Id,
UnitPrice = 4.0
});
context.Orders.Add(new Order {
OrderId = SequentialGuidGenerator.Generate(SequentialGuidType.SequentialAtEnd),
OrderName = "OrderTwo",
CustomerId = customerFour.Id,
UnitPrice = 3.5
});


var customerFive = new Customer { Id = 5, Name = "CustomerFive" };
context.Customers.Add(customerFive);
context.Orders.Add(new Order {
OrderId = SequentialGuidGenerator.Generate(SequentialGuidType.SequentialAtEnd),
OrderName = "OrderOne",
CustomerId = customerFive.Id,
UnitPrice = 4.0
});
context.Orders.Add(new Order {
OrderId = SequentialGuidGenerator.Generate(SequentialGuidType.SequentialAtEnd),
OrderName = "OrderTwo",
CustomerId = customerFive.Id,
UnitPrice = 3.5
});


context.Orders.Add(new Order { OrderId = SequentialGuidGenerator.Generate(SequentialGuidType.SequentialAtEnd), OrderName = "OrderOne", Customer = customerOne, UnitPrice = 4.0 });
context.Orders.Add(new Order { OrderId = SequentialGuidGenerator.Generate(SequentialGuidType.SequentialAtEnd), OrderName = "OrderTwo", Customer = customerOne, UnitPrice = 3.5 });
var customerSix = new Customer { Id = 6, Name = "CustomerSix" };
context.Customers.Add(customerSix);
context.Orders.Add(new Order {
OrderId = SequentialGuidGenerator.Generate(SequentialGuidType.SequentialAtEnd),
OrderName = "OrderOne",
CustomerId = customerSix.Id,
UnitPrice = 4.0
});
context.Orders.Add(new Order {
OrderId = SequentialGuidGenerator.Generate(SequentialGuidType.SequentialAtEnd),
OrderName = "OrderTwo",
CustomerId = customerSix.Id,
UnitPrice = 3.5
});
}

public DbSet<Customer> Customers { get; set; }
Expand Down
8 changes: 6 additions & 2 deletions Swashbuckle.OData.Sample/Swashbuckle.OData.Sample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,9 @@
</ItemGroup>
<ItemGroup>
<Content Include="Global.asax" />
<Content Include="Web.config" />
<Content Include="Web.config">
<SubType>Designer</SubType>
</Content>
<Content Include="Web.Debug.config">
<DependentUpon>Web.config</DependentUpon>
</Content>
Expand All @@ -195,7 +197,9 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Content Include="packages.config" />
<Content Include="packages.config">
<SubType>Designer</SubType>
</Content>
</ItemGroup>
<ItemGroup>
<None Include="Properties\PublishProfiles\swashbuckleodata.pubxml" />
Expand Down
Loading

0 comments on commit 50e8caa

Please sign in to comment.